2
After following some examples, I am doubtful about the following error:
11-24 20:49:14.803 18215-18215/tiburski.rg.cadastrocarro E/SQLiteDatabase: Error inserting nome=ggggg modelo=ggggg placa=hhhh
android.database.sqlite.SQLiteException: no such table: tbcarro (code 1): , while compiling: INSERT INTO tbcarro(nome,modelo,placa) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
According to some examples already created, follow the classes responsible for the database.
public class DataModal {
private static final String DB_NOME = "carro.db";
private static final int DB_VERSAO = 4;
private static final String TBL_CARRO = "tbcarro";
private static final String COLUNA_ID = "_id";
private static final String COLUNA_NOME = "nome";
private static final String COLUNA_PLACA = "placa";
private static final String COLUNA_MODELO = "modelo";
public static final String[] COLUNAS = {COLUNA_ID, COLUNA_NOME, COLUNA_PLACA, COLUNA_MODELO};
public static String criarTabela() {
String CREATE_TABLE = " CREATE TABLE IF NOT EXISTS "+ getTblCarro() +" ( "
+ getColunaId() +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ getColunaNome() +" VARCHAR NOT NULL, "
+ getColunaPlaca() +" VARCHAR UNSIGNED NULL, "
+ getColunaModelo() +" VARCHAR UNSIGNED NULL ); ";
return CREATE_TABLE;
}
public static String getDbNome() {
return DB_NOME;
}
public static int getDbVersao() {
return DB_VERSAO;
}
public static String getTblCarro() {
return TBL_CARRO;
}
public static String getColunaId() {
return COLUNA_ID;
}
public static String getColunaNome() {
return COLUNA_NOME;
}
public static String getColunaPlaca() {
return COLUNA_PLACA;
}
public static String getColunaModelo() {
return COLUNA_MODELO;
}
public class DataSource extends SQLiteOpenHelper {
private SQLiteDatabase mDb;
public DataSource(Context context) {
super(context, DataModal.getDbNome(), null, DataModal.getDbVersao());
mDb = this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DataModal.criarTabela());
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
onCreate(sqLiteDatabase);
}
}
Other examples with basically similar code work correctly. However, in this example the mentioned error is occurring. Could you point me to the fault or the flaws in the code?
Another important point is that in this example, in the persistence class, I inserted a new method, which I had not yet used.
private Context mContext;
private static Persistencia mInstance;
private static SQLiteDatabase mDb;
public static Persistencia getmInstance(Context context) {
if (mInstance == null) {
synchronized (Persistencia.class) {
if (mInstance == null) {
mInstance = new Persistencia(context);
mDb = context.openOrCreateDatabase(DataModal.getDbNome(), Context.MODE_PRIVATE, null);
}
}
}
return mInstance;
}
private Persistencia(Context context) {
this.mContext = context;
}
Try using a log. i() after generating the SQL and see exactly what is being executed and put here, sometimes you have some concatenation error that in the naked eye is passing beaten. Can post here exactly what SQL ta being executed?
– leofontes