我在Android应用程序上有一个奇怪的问题,我没有这样的表异常,但我确信数据库存在。对于那些会将我重定向到其他帖子的人我想补充说我正在尝试使用自己的手机而不是模拟器,这是因为我在这里阅读了很多关于模拟器上同样问题的问题
任何想法或提示都将不胜感激。
带有错误的活动代码:
...
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();
...
dbdapter类的代码:
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;
}
}
LogCat og错误:
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
答案 0 :(得分:2)
我认为数据库文件存在,但该表不存在,因为错误说明。您是否尝试删除数据库文件并再次运行?由于文件存在,因此不会调用DatabaseHelper.onCreate()。只有在文件不存在且创建了新数据库时才会调用它。