引起的:android.database.sqlite。SQLiteException:没有这样的表

时间:2022-08-31 23:02:05

I get this error when I start my activity in which I present results from my database. I have tried to remove old databases and to uninstall the game, but no luck. Here's my OffDBHelper code:

当我开始显示数据库结果的活动时,就会出现这个错误。我试图删除旧的数据库和卸载游戏,但没有运气。这是我的OffDBHelper代码:

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;


    }
}

And here's my dialog.theme activity where I insert my name:

这是我的对话框。主题活动,我在其中插入我的名字:

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;
}
    }
}

And my activity in which I show results:

我在活动中展示结果:

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);



    }

}

And here is LogCat:

这里是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

4 个解决方案

#1


1  

the table name is : highscorestable and not highscores so here you should have

表名是:highscorestable,而不是highscore,所以这里应该有

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#2


2  

change this line:

改变这条线:

Cursor c = ourDatabase.query(DATABASE_NAME, columns, null, null, null, null, null);

to this

这个

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#3


1  

In getData() function, replace DATABASE_NAME by DATABASE_TABLE!

在getData()函数中,用DATABASE_TABLE替换DATABASE_NAME !

    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#4


-1  

you are getting these android/sqlite exception

您将获得这些android/sqlite异常

SQLiteException: no such table: highscores: , while compiling: SELECT _id, imeosobe, skor FROM highscores

SQLiteException:没有此类表:highscore:,编译时:选择_id、imeosobe、skor FROM highscore

android.database.sqlite.SQLiteException: table highscorestable has no column named score: , while compiling: INSERT INTO highscorestable(score, name) VALUES(?, ?);

android.database.sqlite。SQLiteException: table highscorestable没有命名为score:的列,而编译:插入到highscorestable(score, name)值(??);

i would double check you table and column definitions and also how you are constructing you insert into statement

我将再次检查表和列定义以及如何构造插入语句

#1


1  

the table name is : highscorestable and not highscores so here you should have

表名是:highscorestable,而不是highscore,所以这里应该有

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#2


2  

change this line:

改变这条线:

Cursor c = ourDatabase.query(DATABASE_NAME, columns, null, null, null, null, null);

to this

这个

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#3


1  

In getData() function, replace DATABASE_NAME by DATABASE_TABLE!

在getData()函数中,用DATABASE_TABLE替换DATABASE_NAME !

    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

#4


-1  

you are getting these android/sqlite exception

您将获得这些android/sqlite异常

SQLiteException: no such table: highscores: , while compiling: SELECT _id, imeosobe, skor FROM highscores

SQLiteException:没有此类表:highscore:,编译时:选择_id、imeosobe、skor FROM highscore

android.database.sqlite.SQLiteException: table highscorestable has no column named score: , while compiling: INSERT INTO highscorestable(score, name) VALUES(?, ?);

android.database.sqlite。SQLiteException: table highscorestable没有命名为score:的列,而编译:插入到highscorestable(score, name)值(??);

i would double check you table and column definitions and also how you are constructing you insert into statement

我将再次检查表和列定义以及如何构造插入语句