希望这是允许的 - 我有两个无关的问题。
这是主要问题。由于某种原因,我的数据库中的最后一行没有被创建。我收到了SQLiteException。 LogCat和代码如下。已对错误代码行进行了注释。
有没有办法获取AUTOINCREMENT列的值?我无法使用此Cursor c = db.rawQuery("SELECT " + PERCENTAGE + " FROM " + TABLE + " WHERE " + RANK + " = " + rank, null);
访问它,其中RANK
是AUTOINCREMENT列标题。此光标始终返回0.
DatabaseHelper.java
// Table columns names.
private static final String RANK = "_id";
private static final String SCORE = "score";
private static final String PERCENTAGE = "percentage";
private static final String SUM = "sum";
//Check if new record makes the top 10.
public boolean check(long score, int percentage, long sum) {
Cursor c = db.rawQuery("SELECT " + SUM + " FROM " + TABLE, null); //Line 82
int z = c.getCount();
c.moveToLast();
int x = c.getInt(c.getColumnIndex(SUM));
long y = calculateSum(score, percentage);
if(z != 10) {
insert(score, percentage, sum);
return true;
} else {
if(y > x) {
delete(10);
insert(score, percentage, sum);
sort();
return true;
} else {
return false;
}
}
}
//Insert new record.
public long insert(long score, int percentage, long sum) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
values.put(SUM, sum);
return db.insert(TABLE, null, values);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " ("
+ RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ SCORE + " LONG,"
+ PERCENTAGE + " INTEGER,"
+ SUM + " LONG"
+ ");");
}
LogCat输出
01-04 02:00:24.635: E/AndroidRuntime(6224): FATAL EXCEPTION: main
01-04 02:00:24.635: E/AndroidRuntime(6224): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: android.database.sqlite.SQLiteException: no such column: sum (code 1): , while compiling: SELECT sum FROM HighscoresList
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.os.Looper.loop(Looper.java:137)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-04 02:00:24.635: E/AndroidRuntime(6224): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 02:00:24.635: E/AndroidRuntime(6224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-04 02:00:24.635: E/AndroidRuntime(6224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-04 02:00:24.635: E/AndroidRuntime(6224): at dalvik.system.NativeStart.main(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224): Caused by: android.database.sqlite.SQLiteException: no such column: sum (code 1): , while compiling: SELECT sum FROM HighscoresList
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-04 02:00:24.635: E/AndroidRuntime(6224): at com.example.test.DatabaseHelper.check(DatabaseHelper.java:82)
01-04 02:00:24.635: E/AndroidRuntime(6224): at com.example.test.Results.showResults(Results.java:111)
01-04 02:00:24.635: E/AndroidRuntime(6224): at com.example.test.Results.onCreate(Results.java:60)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.Activity.performCreate(Activity.java:5104)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-04 02:00:24.635: E/AndroidRuntime(6224): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-04 02:00:24.635: E/AndroidRuntime(6224): ... 11 more
答案 0 :(得分:1)
创建表格有问题。从“sum”列中检索值时,stacktrace会显示错误。此错误表示没有名为“sum”的列。您是否有可能在创建此表之前尝试获取值?
您可以使用for循环检索数据:
for(int i=0;i<z;i++){
int ID = yourCursor.getInt(0);
if(ID==YOUR_NEEDED_ID){
int percantage = yourCursor.getInt(2);
break; //if you got what you want, cursor could be stopped
}
yourCursor.moveToNext();
}
升级数据库使用
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
onCreate(db);
}