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) {
}
What is shown in the error line? Data tag "DS".
– Andrei Coelho
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
– David
But what about Sqliteopenhelper? You created the table there?
– Andrei Coelho
Or was it just this class?
– Andrei Coelho
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.
– David
So.. the error says that this table does not exist! That’s it!
no such table: financas
better you insert more codes!– Andrei Coelho
That’s what I don’t understand. I posted the gets and sets and the Datamodel, the table has to exist
– David
Not necessarily... put the
SQLiteOpenHelper
also.– Andrei Coelho
I updated the question already
– David
It worked out David?
– Andrei Coelho
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 :)
– David
The line I put in the answer was wrong too! The last comma generates error!
– Andrei Coelho
Check it out > https://sqliteonline.com/#fiddle-5ca7c4d71f1ba54xju4kfo1t
– Andrei Coelho
My answer is apparently correct, because on top of everything, I told you to re-install the application.
– Andrei Coelho