0
You’re making this mistake, I wonder if my DAO is right.
Error:
08-06 14:19:53.304: E/SQLiteLog(16893): (1) no such table: proposta
08-06 14:19:53.334: E/SQLiteDatabase(16893): Error inserting validadebrinde=x validade=cd idusuario=1 brinde=c
08-06 14:19:53.334: E/SQLiteDatabase(16893): android.database.sqlite.SQLiteException: no such table: proposta (code 1): , while compiling: INSERT INTO proposta(validadebrinde,validade,idusuario,brinde) VALUES (?,?,?,?)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at br.com.android.controledevisitas.dao.PropostaDAO.cadastrar(PropostaDAO.java:43)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at br.com.android.controledevisitas.view.CadastrarPropostaActivity.cadastrarProposta(CadastrarPropostaActivity.java:122)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at br.com.android.controledevisitas.view.CadastrarPropostaActivity$2.onClick(CadastrarPropostaActivity.java:78)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.view.View.performClick(View.java:4091)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.view.View$PerformClick.run(View.java:17036)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.os.Handler.handleCallback(Handler.java:615)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.os.Looper.loop(Looper.java:137)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at android.app.ActivityThread.main(ActivityThread.java:5031)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
08-06 14:19:53.334: E/SQLiteDatabase(16893): at dalvik.system.NativeStart.main(Native Method)
My DAO:
package br.com.android.controledevisitas.dao;
import br.com.android.controledevisitas.model.Proposta;
import br.com.android.controledevisitas.util.Dados;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class PropostaDAO extends SQLiteOpenHelper {
private static final String TABELA = "proposta";
private static final Dados dados = new Dados();
public PropostaDAO(Context ctx) {
super(ctx, dados.DATABASE, null, dados.VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table "
+ TABELA
+ "(id integer primary key, idusuario integer, validade text, brinde text, validadebrinde text);";
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int versaoAntiga, int versaoNova) {
String sql = "drop table if exists " + TABELA;
db.execSQL(sql);
onCreate(db);
}
public Boolean cadastrar(Proposta p) {
try {
ContentValues cv = new ContentValues();
cv.put("idusuario", 1);
cv.put("validade", p.getValidade());
cv.put("brinde", p.getBrinde());
cv.put("validadebrinde", p.getValidadeBrinde());
getWritableDatabase().insert(TABELA, null, cv);
return true;
} catch (Exception e) {
Log.e("DAO", e.getMessage());
return false;
}
}
public int getUltimoId() {
Cursor c = getReadableDatabase().rawQuery(
"select max(id) from " + TABELA, null);
if (c.moveToNext()) {
return c.getInt(c.getColumnIndex("id"));
}
return 0;
}
}
You’re making a mistake in the
INSERT INTO proposta
but in your code you have not shown us any Insert. Your mistake is for sure that you are not doing create before doing Insert.– Math
My input here getWritableDatabase(). Insert(TABLE, null, cv);
– meisterx7
That’s probably the problem, but because he’s not calling onCreate() when I urge the DAO class
– meisterx7
This is a good question. Try debugging your program and see if the onCreate method is running even or not. Are you not making an exception when trying to create?
– Math
I debugged, he is not being called, but I do not understand, because I installed the right class, was to be running onCreate(). Instanciei assim -> Propostadao pDao = new Propostadao(this);
– meisterx7
I think I was mistaken in the previous comment. Try uninstalling the app from your emulator or mobile phone, then try running it again, so I read onCreate() is only called the first time the app runs, which is when your bd is effectively created. Or if you prefer, inform that there is a new version of BD to force the onUpgrade method to be called
– Math
meisterx7, solved your problem?
– user13985