我正在开发一个费用跟踪器,我想在应用程序首次启动时用一些记录填充数据库。我试图在我的splash活动中调用该方法并添加数据。但是当我清除我的应用程序数据并第一次启动应用程序时,会发生错误,说没有这样的列。
日志cat错误如下所示。
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";
答案 0 :(得分:3)
养成很好地格式化代码的习惯。最后一列定义没有逗号:
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 "
+ ")";
答案 1 :(得分:2)
android.database.sqlite.SQLiteException:没有这样的列:exp_type (代码1):,编译时:SELECT * FROM expense_type Order BY exp_type DESC
这是你的错误:
String CREATE_EXPENSE_TYPE_TABLE = "CREATE TABLE " + TABLE_EXPENSE_TYPE
+ " (" + KEY_EXPENSE_ID + " INTEGER AUTOINCREMENT" +
KEY_EXPENSE_TYPE + " TEXT PRIMARY KEY" + ")
INTEGER AUTOINCREMENT
KEY_EXPENSE_ID + " INTEGER AUTOINCREMENT" +
KEY_EXPENSE_TYPE
评估为:
“id integer autoincrementexp_type”
答案 2 :(得分:2)
尝试在user preferences
/ shared preferences
中添加一个标记来检查首次运行的应用,然后在返回时,您可以决定是要创建数据库还是阅读它!
你看看http://www.vogella.com/articles/AndroidSQLite/article.html
编辑
或者只是检查是否有dbexists而不是用户pref标志
答案 3 :(得分:1)
private final String SAMPLE_DB_NAME = "TheftData";
private final String SAMPLE_TABLE_NAME = "UserDetails";
String CreateTable = "CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (Username VARCHAR, Password VARCHAR," +
" );";
然后在onCreate
protected void onCreate(Bundle savedInstanceState) {
try {
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL(CreateTable);
sampleDB.close();
}
catch(Exception e)
{Toast.makeText(getApplicationContext(), " Error: "+e.getMessage(), Toast.LENGTH_LONG).show();
sampleDB.close();
}
并添加数据
public void insertData(String username,String password)
{
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL(CreateTable);
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('"+username+"','"+ password+"');");
sampleDB.close();
}