我正在开发一个费用跟踪器,我想在应用程序首次启动时用一些记录填充数据库。我试图在我的启动活动中调用该方法并添加数据。!但是当我第一次清除我的应用程序数据并启动应用程序时,会出现一个错误,说没有这样的列。
日志猫错误如下图。
06-30 18:09:58.834: E/SQLiteLog(18380): (1) no such column: exp_type
06-30 18:10:00.826: D/AndroidRuntime(18380): Shutting down VM
06-30 18:10:00.826: W/dalvikvm(18380): threadid=1: thread exiting with uncaught exception (group=0x416b8318)
06-30 18:10:00.856: E/AndroidRuntime(18380): FATAL EXCEPTION: main
06-30 18:10:00.856: E/AndroidRuntime(18380): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sliit.expenseanalyzer/com.sliit.expenseanalyzer.SplashActivity}: android.database.sqlite.SQLiteException: no such column: exp_type (code 1): , while compiling: SELECT * FROM expense_type Order BY exp_type DESC
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2088)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread.access$600(ActivityThread.java:134)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.os.Looper.loop(Looper.java:137)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread.main(ActivityThread.java:4744)
06-30 18:10:00.856: E/AndroidRuntime(18380): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 18:10:00.856: E/AndroidRuntime(18380): at java.lang.reflect.Method.invoke(Method.java:511)
06-30 18:10:00.856: E/AndroidRuntime(18380): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-30 18:10:00.856: E/AndroidRuntime(18380): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-30 18:10:00.856: E/AndroidRuntime(18380): at dalvik.system.NativeStart.main(Native Method)
06-30 18:10:00.856: E/AndroidRuntime(18380): Caused by: android.database.sqlite.SQLiteException: no such column: exp_type (code 1): , while compiling: SELECT * FROM expense_type Order BY exp_type DESC
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
06-30 18:10:00.856: E/AndroidRuntime(18380): at com.sliit.expenseanalyzer.util.DatabaseHandler.getAllExpenseTypes(DatabaseHandler.java:218)
06-30 18:10:00.856: E/AndroidRuntime(18380): at com.sliit.expenseanalyzer.SplashActivity.onCreate(SplashActivity.java:29)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.Activity.performCreate(Activity.java:5008)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-30 18:10:00.856: E/AndroidRuntime(18380): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
06-30 18:10:00.856: E/AndroidRuntime(18380): ... 11 more
应用程序第一次启动时如何填充数据!这只是几张唱片,所以我不担心性能。
这将是一个很大的帮助!
谢谢你。
数据库代码
public void onCreate(SQLiteDatabase db) {
// Create user table
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT," + KEY_PW + " TEXT," + KEY_SESSION
+ " INTEGER" + ")";
String CREATE_EXPENSE_TYPE_TABLE = "CREATE TABLE " + TABLE_EXPENSE_TYPE
+ " (" + KEY_EXPENSE_ID + " INTEGER AUTOINCREMENT" + KEY_EXPENSE_TYPE + " TEXT PRIMARY KEY" + ")";
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_EXPENSE_TYPE_TABLE);
}
我用来添加记录的方法
public Long addExpenseType(String expense) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EXPENSE_TYPE, expense);
Long row = db.insert(TABLE_EXPENSE_TYPE, null, values);
db.close();
return row;
}
字符串值:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "expenseManager";
// Database Tables
private static final String TABLE_USER = "user";
private static final String TABLE_EXPENSE_TYPE = "expense_type";
// User table columns
private static final String KEY_ID = "id";
private static final String KEY_NAME = "full_name";
private static final String KEY_PW = "password";
private static final String KEY_EMAIL = "email";
private static final String KEY_SESSION = "session";
// Expense type table columns
private static final String KEY_EXPENSE_ID = "id";
private static final String KEY_EXPENSE_TYPE = "exp_type";