當我開始我從我的數據庫中呈現結果的活動時,出現此錯誤。我試圖刪除舊數據庫並卸載遊戲,但沒有運氣。這裏是我的OffDBHelper代碼:引起:android.database.sqlite.SQLiteException:沒有這樣的表
public class OffDBHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SCORE = "score";
private static final String DATABASE_NAME = "highscores";
private static final String DATABASE_TABLE = "highscorestable";
public static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_SCORE + " INTEGER NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public OffDBHelper(Context c){
ourContext = c;
}
public OffDBHelper open() throws Exception{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, double score) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCORE, score);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_SCORE};
Cursor c = ourDatabase.query(DATABASE_NAME, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iScore = c.getColumnIndex(KEY_SCORE);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iScore) + "\n";
}
return result;
}
}
這是我的dialog.theme活動,我插入我的名字:
public class ImePopup extends Activity implements OnClickListener{
EditText ime;
Button ok, odustani;
double score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imepopup);
ok = (Button) findViewById(R.id.btOK);
odustani = (Button) findViewById(R.id.btOdustani);
ime = (EditText) findViewById(R.id.etIme);
ok.setOnClickListener(this);
odustani.setOnClickListener(this);
}
public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.btOK:
boolean didItWork = true;
try{
String name = ime.getText().toString();
OffDBHelper entry = new OffDBHelper(ImePopup.this);
entry.open();
entry.createEntry(name, score);
entry.close();
break;
}catch(Exception e){
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Greska!");
TextView tv = new TextView(this);
tv.setText("Greska u upisu");
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Uspesno!");
TextView tv = new TextView(this);
tv.setText("Upisali ste se!");
d.setContentView(tv);
d.show();
}
}
case R.id.btOdustani:
break;
}
}
}
而且我的活動中,我展示成果:
public class OffRezultati extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.offrezultati);
TextView tvSQLinfo;
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
OffDBHelper info = new OffDBHelper(this);
try {
info.open();
} catch (Exception e) {
e.printStackTrace();
}
String data = info.getData();
info.close();
tv.setText(data);
}
}
這裏是LogCat:
03-08 13:40:29.737: I/Database(24888): sqlite returned: error code = 1, msg = no such table: highscores
03-08 13:40:29.747: D/AndroidRuntime(24888): Shutting down VM
03-08 13:40:29.747: W/dalvikvm(24888): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-08 13:40:29.768: E/AndroidRuntime(24888): FATAL EXCEPTION: main
03-08 13:40:29.768: E/AndroidRuntime(24888): java.lang.RuntimeException: Unable to start activity ComponentInfo{rs.androidaplikacijekvizopstekulture/rs.androidaplikacijekvizopstekulture.OffRezultati}: android.database.sqlite.SQLiteException: no such table: highscores: , while compiling: SELECT _id, imeosobe, skor FROM highscores
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.os.Looper.loop(Looper.java:130)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-08 13:40:29.768: E/AndroidRuntime(24888): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:40:29.768: E/AndroidRuntime(24888): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 13:40:29.768: E/AndroidRuntime(24888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-08 13:40:29.768: E/AndroidRuntime(24888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-08 13:40:29.768: E/AndroidRuntime(24888): at dalvik.system.NativeStart.main(Native Method)
03-08 13:40:29.768: E/AndroidRuntime(24888): Caused by: android.database.sqlite.SQLiteException: no such table: highscores: , while compiling: SELECT _id, imeosobe, skor FROM highscores
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
03-08 13:40:29.768: E/AndroidRuntime(24888): at rs.androidaplikacijekvizopstekulture.OffDBHelper.getData(OffDBHelper.java:72)
03-08 13:40:29.768: E/AndroidRuntime(24888): at rs.androidaplikacijekvizopstekulture.OffRezultati.onCreate(OffRezultati.java:29)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 13:40:29.768: E/AndroidRuntime(24888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-08 13:40:29.768: E/AndroidRuntime(24888): ... 11 more
03-08 13:40:31.307: I/Process(24888): Sending signal. PID: 24888 SIG: 9
03-08 13:57:41.437: I/Database(25128): sqlite returned: error code = 1, msg = table highscorestable has no column named score
03-08 13:57:41.617: E/Database(25128): Error inserting score=0.0 name=marjan
03-08 13:57:41.617: E/Database(25128): android.database.sqlite.SQLiteException: table highscorestable has no column named score: , while compiling: INSERT INTO highscorestable(score, name) VALUES(?, ?);
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
03-08 13:57:41.617: E/Database(25128): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-08 13:57:41.617: E/Database(25128): at rs.androidaplikacijekvizopstekulture.OffDBHelper.createEntry(OffDBHelper.java:64)
03-08 13:57:41.617: E/Database(25128): at rs.androidaplikacijekvizopstekulture.ImePopup.onClick(ImePopup.java:42)
03-08 13:57:41.617: E/Database(25128): at android.view.View.performClick(View.java:2506)
03-08 13:57:41.617: E/Database(25128): at android.view.View$PerformClick.run(View.java:9112)
03-08 13:57:41.617: E/Database(25128): at android.os.Handler.handleCallback(Handler.java:587)
03-08 13:57:41.617: E/Database(25128): at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 13:57:41.617: E/Database(25128): at android.os.Looper.loop(Looper.java:130)
03-08 13:57:41.617: E/Database(25128): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-08 13:57:41.617: E/Database(25128): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:57:41.617: E/Database(25128): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 13:57:41.617: E/Database(25128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-08 13:57:41.617: E/Database(25128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-08 13:57:41.617: E/Database(25128): at dalvik.system.NativeStart.main(Native Method)
03-08 13:57:47.377: I/Database(25128): sqlite returned: error code = 1, msg = no such table: highscores
03-08 13:57:47.437: D/AndroidRuntime(25128): Shutting down VM
03-08 13:57:47.437: W/dalvikvm(25128): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-08 13:57:47.497: E/AndroidRuntime(25128): FATAL EXCEPTION: main
03-08 13:57:47.497: E/AndroidRuntime(25128): java.lang.RuntimeException: Unable to start activity ComponentInfo{rs.androidaplikacijekvizopstekulture/rs.androidaplikacijekvizopstekulture.OffRezultati}: android.database.sqlite.SQLiteException: no such table: highscores: , while compiling: SELECT _id, name, score FROM highscores
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.os.Looper.loop(Looper.java:130)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-08 13:57:47.497: E/AndroidRuntime(25128): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:57:47.497: E/AndroidRuntime(25128): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 13:57:47.497: E/AndroidRuntime(25128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-08 13:57:47.497: E/AndroidRuntime(25128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-08 13:57:47.497: E/AndroidRuntime(25128): at dalvik.system.NativeStart.main(Native Method)
03-08 13:57:47.497: E/AndroidRuntime(25128): Caused by: android.database.sqlite.SQLiteException: no such table: highscores: , while compiling: SELECT _id, name, score FROM highscores
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1235)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1189)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
03-08 13:57:47.497: E/AndroidRuntime(25128): at rs.androidaplikacijekvizopstekulture.OffDBHelper.getData(OffDBHelper.java:69)
03-08 13:57:47.497: E/AndroidRuntime(25128): at rs.androidaplikacijekvizopstekulture.OffRezultati.onCreate(OffRezultati.java:29)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 13:57:47.497: E/AndroidRuntime(25128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-08 13:57:47.497: E/AndroidRuntime(25128): ... 11 more
03-08 13:57:47.597: D/dalvikvm(25128): GC_CONCURRENT freed 260K, 49% free 2893K/5639K, external 0K/0K, paused 6ms+16ms
03-08 13:57:47.677: E/Database(25128): close() was never explicitly called on database '/data/data/rs.androidaplikacijekvizopstekulture/databases/highscores'
03-08 13:57:47.677: E/Database(25128): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-08 13:57:47.677: E/Database(25128): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
03-08 13:57:47.677: E/Database(25128): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
03-08 13:57:47.677: E/Database(25128): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
03-08 13:57:47.677: E/Database(25128): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
03-08 13:57:47.677: E/Database(25128): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:572)
03-08 13:57:47.677: E/Database(25128): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
03-08 13:57:47.677: E/Database(25128): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
03-08 13:57:47.677: E/Database(25128): at rs.androidaplikacijekvizopstekulture.OffDBHelper.open(OffDBHelper.java:53)
03-08 13:57:47.677: E/Database(25128): at rs.androidaplikacijekvizopstekulture.OffRezultati.onCreate(OffRezultati.java:25)
03-08 13:57:47.677: E/Database(25128): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 13:57:47.677: E/Database(25128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
03-08 13:57:47.677: E/Database(25128): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
03-08 13:57:47.677: E/Database(25128): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-08 13:57:47.677: E/Database(25128): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
03-08 13:57:47.677: E/Database(25128): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 13:57:47.677: E/Database(25128): at android.os.Looper.loop(Looper.java:130)
03-08 13:57:47.677: E/Database(25128): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-08 13:57:47.677: E/Database(25128): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:57:47.677: E/Database(25128): at java.lang.reflect.Method.invoke(Method.java:507)
03-08 13:57:47.677: E/Database(25128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-08 13:57:47.677: E/Database(25128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-08 13:57:47.677: E/Database(25128): at dalvik.system.NativeStart.main(Native Method)
03-08 13:57:49.217: I/Process(25128): Sending signal. PID: 25128 SIG: 9
嘗試使用Google這個「Android應用程序使用自己的SQLite數據庫」,並設置數據庫外部您的Android應用如本文所述 – Hector 2013-03-08 13:18:42
在此處的郵政編碼後正確檢查您的日誌。這只是一個有趣的錯誤。 – 2013-03-08 13:21:56
@Dhaval Sodha Parmar:提供了正確的答案。 – 2013-03-08 13:44:33