I have a strange problem on an android application, I have no such table exception, but I’m sure that the database exists. For some that will redirect me to the other posts here I would like to add that I’m trying the app on my own cell phone not a an emulator, this because I read a lot of questions here that talk about the same problem on emulator.
Any ideas or hint will be greatly appreciated.
the code of activity with the error:
...
public class chartView extends Activity{
Button editbtm;
Button cleanbtm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
editbtm = (Button) findViewById(R.id.editBasketbtm);
cleanbtm = (Button) findViewById(R.id.cleanBasketbtm);
//fletch all goods that should be presented on the text View
dbAdapter goodsfletcher = new dbAdapter(chartView.this);
// goodsfletcher.createGoodsTable();
goodsfletcher.open();
...
the code of teh dbADapter class:
public class dbAdapter {
public static final String KEY_ARTI = "ARTI";
public static final String KEY_PROV = "PROV";
public static final String KEY_CODART = "CODART";
public static final String KEY_PRECBOL ="PRECBOL";
public static final String KEY_QUANT ="QUANT";
private static final String TAG = "DbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "create table goods ( CODART VARCHAR(10) primary key, ARTI varchar(50) not null, PROV varchar(15) not null, PRECBOL FLOAT(6,2) not null, QUANT int not null);";
private static final String DATABASE_NAME = "ketanoBase";
private static final String DATABASE_TABLE = "goods";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS goods");
onCreate(db);
}
}
public dbAdapter(Context ctx) {
this.mCtx = ctx;
}
public dbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public void addĜood(String code, String title, String source, String quantity, String price ) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ARTI, title);
initialValues.put(KEY_CODART, code);
initialValues.put(KEY_PROV, source);
initialValues.put(KEY_PRECBOL, price);
initialValues.put(KEY_QUANT, quantity);
mDb.insert(DATABASE_TABLE, code, initialValues);
}
public boolean deleteGood(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_CODART + "=" + rowId, null) > 0;
}
public void eraseGoods(){
String sql = "drop table if exists goods";
mDb.execSQL(sql);
}
public Cursor fetchAllGoods() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_CODART, KEY_ARTI,
KEY_PROV, KEY_PRECBOL, KEY_QUANT}, null, null, null, null, null);
}
public Cursor fetchGood(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_CODART,
KEY_ARTI, KEY_PROV,KEY_PRECBOL, KEY_QUANT}, KEY_CODART + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateGood(long rowId, String quantity) {
ContentValues args = new ContentValues();
args.put(KEY_QUANT,quantity);
return mDb.update(DATABASE_TABLE, args, KEY_CODART + "=" + rowId, null) > 0;
}
}
The LogCat og the error:
03-14 09:52:05.668: W/KeyCharacterMap(30808): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-14 09:52:06.384: D/AndroidRuntime(30808): Shutting down VM
03-14 09:52:06.384: W/dalvikvm(30808): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-14 09:52:06.394: E/AndroidRuntime(30808): FATAL EXCEPTION: main
03-14 09:52:06.394: E/AndroidRuntime(30808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.chartView}: android.database.sqlite.SQLiteException: no such table: goods: , while compiling: SELECT CODART, ARTI, PROV, PRECBOL, QUANT FROM goods
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.os.Looper.loop(Looper.java:130)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-14 09:52:06.394: E/AndroidRuntime(30808): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 09:52:06.394: E/AndroidRuntime(30808): at java.lang.reflect.Method.invoke(Method.java:507)
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-14 09:52:06.394: E/AndroidRuntime(30808): at dalvik.system.NativeStart.main(Native Method)
03-14 09:52:06.394: E/AndroidRuntime(30808): Caused by: android.database.sqlite.SQLiteException: no such table: goods: , while compiling: SELECT CODART, ARTI, PROV, PRECBOL, QUANT FROM goods
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.remotedata.firstapp.dbAdapter.fetchAllGoods(dbAdapter.java:138)
03-14 09:52:06.394: E/AndroidRuntime(30808): at com.remotedata.firstapp.chartView.onCreate(chartView.java:31)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-14 09:52:06.394: E/AndroidRuntime(30808): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-14 09:52:06.394: E/AndroidRuntime(30808): ... 11 more
I believe that the database file exists, but the table doesn’t exist as the error says. Have you tried deleting the database file and running again? Since the file exists, DatabaseHelper.onCreate() won’t be called. It will only be called if the file doesn’t exist and a new database is created.