android sqlite找不到表“No Such Table”

时间:2012-01-29 12:15:29

标签: android sqlite

真的很头疼,因为有些人遇到了这个麻烦。我尝试过很多东西,真的很累。我会给出代码并祈祷:(

如果有人能真正解决,真的非常感谢!!

StartActivity:

this.dhn = DataHelper.getDataHelper(this);

    File directory = new File(Environment.getDataDirectory() + File.separator + "data" + File.separator + "ko.tb" + File.separator + "databases");
    if(directory.exists())
    {
    }
    else
    {
        directory.mkdirs();
        updateDB();
    }
    directory = null;

    try {
        guid = this.dhn.Guid();

        if(this.dhn.getSettings("dbVersion") == null || Integer.parseInt(this.dhn.getSettings("dbVersion")) != Version || !this.dhn.isTableExists("UserInfo"))
        {
            updateDB(); 
        }
    }
    catch (SQLiteException e)
    {   
        updateDB();     
        guid = this.dhn.Guid();
    }


  public void updateDB()
    {
      this.dhn.close();

        try {
            InputStream myInput;

                myInput = getAssets().open("KelimeDB1.db");

            // Path to the just created empty db
            String outFileName = "/data/data/ko.tb/databases/"
                    + "KelimeDB1.db";

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
            buffer = null;
            outFileName = null;
            this.dhn.close();
            this.dhn = null;
            this.dhn = DataHelper.getDataHelper(this);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

DataHelper:

   private static DataHelper singleton;

public static DataHelper getDataHelper(Context context) {
        if (singleton == null) {
                singleton = new DataHelper(context);
                OpenHelper openHelper = new OpenHelper(singleton.context);
                singleton.db = openHelper.getWritableDatabase();
        }
        if(!singleton.db.isOpen()){
                OpenHelper openHelper = new OpenHelper(singleton.context);
                singleton.db = openHelper.getWritableDatabase();
        }
        singleton.context = context;
        return singleton;
}

private DataHelper(Context context) {
        this.context = context;
}

public void close() {
    if (singleton != null) 
    { 
        singleton.db.close();
        singleton.db = null;
        singleton = null;
        //this.db.close();
        this.db = null;
    }
}


public String Guid() {

    String SqlQuery = "SELECT Value FROM UserInfo WHERE key = 'guid'";
    String guid = null;
    Cursor cursor = this.db.rawQuery(SqlQuery, null);
    if (cursor.moveToFirst()) {
        guid = cursor.getString(0);
    }
    cursor.close();
    return guid;
}

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ko.tb/ko.tb.StartScreen}: android.database.sqlite.SQLiteException: no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid'
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1364)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1332)
at ko.tb.DataHelper.Guid(DataHelper.java:171)
at ko.tb.StartScreen.onCreate(StartScreen.java:76)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
... 11 more

1 个答案:

答案 0 :(得分:0)

您可能需要查看this library out for managing your database 。它旨在帮助开发人员使用预先创建的数据库发布应用程序。