Insert method does not save data

Asked

Viewed 114 times

1

I have an application where the goal (at the moment) is to take the data typed in a form and save in the Database. And the Insert method of my Datasource always returns me false I do not understand why. The process of saving by Controller returns right, but when checking the Insert, it returns false, as in the image below. Everything returns true until I get to that part. I’m more than a week trying to understand why it doesn’t work, I already did, I redid the application, I reviewed source codes to check if everything was okay, nothing seems out of the ordinary (id returns 0 always, data returns certain but not saved). I’ll even send you my gets and sets and the Datamodel.

Gets and sets:

public class Formulario {

    private int id;
    private String titulo;
    private String data;
    private String info;
    private double valor;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public double getValor() {
        return valor;
    }

    public void setValor(double valor) {
        this.valor = valor;
    }
}

Datamodel:

public class FormularioDataModel {

    private final static String TABELA = "financas";

    private final static String id = "id";
    private final static String titulo = "titulo";
    private final static String valor = "valor";
    private final static String data = "data";
    private final static String info = "info";

    private static String queryCriarTabela = "";

    public static String criarTabela() {

        queryCriarTabela = "CREATE TABLE " + TABELA;
        queryCriarTabela += "(";
        queryCriarTabela += id + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
        queryCriarTabela += titulo + " TEXT, ";
        queryCriarTabela += valor + " REAL, ";
        queryCriarTabela += data + " TEXT, ";
        queryCriarTabela += info + " TEXT, ";
        queryCriarTabela += ")";

        return queryCriarTabela;
    }

    public static String getTABELA() {
        return TABELA;
    }

    public static String getId() {
        return id;
    }

    public static String getTitulo() {
        return titulo;
    }

    public static String getValor() {
        return valor;
    }

    public static String getData() {
        return data;
    }

    public static String getInfo() {
        return info;
    }

    public static String getQueryCriarTabela() {
        return queryCriarTabela;
    }

    public static void setQueryCriarTabela(String queryCriarTabela) {
        FormularioDataModel.queryCriarTabela = queryCriarTabela;
    }
}

Datasource:

public class Datasource extends Sqliteopenhelper {

private static final String DB_NAME = "financas.sqlite";
private static final int DB_VERSION = 1;

SQLiteDatabase db;
Cursor cursor;

public DataSource(Context context) {
    super(context, DB_NAME, null, DB_VERSION);

    db = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {

    try{

        db.execSQL(FormularioDataModel.criarTabela());

    }catch (Exception e){
        Log.e("Media", "Log ---> ERRO "+e.getMessage());
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}

inserir a descrição da imagem aqui

  • What is shown in the error line? Data tag "DS".

  • 2019-03-29 22:30:55.046 28076-28076/com.dz.finanas E/Sqlitedatabase: Error inserting title=Account info=Pay logo value=250.0 data=29/03/2019 android.database.sqlite.Sqliteexception: no such table: financas (code 1): while compiling: INSERT INTO financas(title,info,value,data) VALUES (?,?,??) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 2019-03-29 22:33:19.388 845-1018/? E/Wifistatemachine: mIsFullScanOngoing: false, mSendScanResultsBroadcast: false

  • But what about Sqliteopenhelper? You created the table there?

  • Or was it just this class?

  • Yes, I extended Sqliteopenhelper on Datasource, then I put the constructor, onCreate and onUpgrade. I put the Insert method there and the save method in the Controller. The controller returns everything true, when it comes time to check the Insert, this is where it returns false, as in img.

  • So.. the error says that this table does not exist! That’s it! no such table: financas better you insert more codes!

  • That’s what I don’t understand. I posted the gets and sets and the Datamodel, the table has to exist

  • Not necessarily... put the SQLiteOpenHelper also.

  • I updated the question already

  • It worked out David?

  • I solved it. The problem was not the project, but the APK that was installed. I reinstalled the APK and solved it. But thanks for the help :)

  • The line I put in the answer was wrong too! The last comma generates error!

  • Check it out > https://sqliteonline.com/#fiddle-5ca7c4d71f1ba54xju4kfo1t

  • My answer is apparently correct, because on top of everything, I told you to re-install the application.

Show 9 more comments

1 answer

1

The error I noticed is in the table creation. The string concatenated (queryCriarTabela) of the method criarTabela() returns this value:

    CREATE TABLE financas (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      titulo TEXT,
      valor REAL,
      data TEXT,
      info TEXT, // <-- erro
    ); // <-- coloque o ponto e vírgula no final por segurança

This comma at the end does not let the table be created.

REMEMBER

Whenever you make a change to some bank table using the helper, you need to perform one of the actions below:

  • Create a drop table if exists from the old table

  • Or uninstall and install the application again

I have not found other reasons for failure in table creation. But there may be.

Including, his DataSource in the method onCreate must have caught that exception on this line:

Log.e("Media", "Log ---> ERRO "+e.getMessage());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.