2011-12-24 2 views
1

我有市場上的應用程序。有些人給了我這個錯誤;Android Sqlite沒有這樣的表錯誤

沒有這樣的表:的UserInfo:,在編譯:FROM的UserInfo WHERE鍵= 'GUID'

但是我有此代碼甚至選擇值;

if(!this.dhn.isTableExists("UserInfo")) 
    { 
     updateDB(); 
    } 

更新DB;

public void updateDB() 
{ 
    try { 
     InputStream myInput; 

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

     // Path to the just created empty db 
     String outFileName = "/data/data/ko.tb/databases/" 
       + "example.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; 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 

表存在;

public boolean isTableExists(String tableName) { 

    Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null); 
    if(cursor!=null) { 
     if(cursor.getCount()>0) { 
      return true; 
     } 
    } 
    return false; 
} 

all error;

java.lang.RuntimeException: Unable to start activity ComponentInfo{ko.tb/ko.tb.KOActivity}: android.database.sqlite.SQLiteException: no such table: UserInfo: , while compiling: SELECT Value FROM UserInfo WHERE key = 'guid' 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893) 
at android.app.ActivityThread.access$1500(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:150) 
at android.app.ActivityThread.main(ActivityThread.java:4385) 
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:849) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
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:53) 
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1442) 
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1410) 
at ko.tb.DataHelper.Guid(DataHelper.java:126) 
at ko.tb.KOActivity.onCreate(KOActivity.java:202) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836) 
... 11 more 
+1

你能複製粘貼確切的錯誤? – 2011-12-24 22:59:53

+0

您是否通過adb確認數據庫已通過'updateDB()'正確地複製到應用程序的數據庫區域?應用程序包是否在清單「ko.tb」中列出?您是否嘗試使用'getDatabasePath(「example.db」)'而不是手動構建路徑? – 2011-12-24 23:04:25

+0

@Ted Hopp我只是爲你清楚地嘗試過。所有步驟都正常工作,我測試的是>沒有UserInfo的複製數據庫,推到adb然後istableexist給了我虛假然後複製,並試圖讓guid工作正常。但有些人有困難,我不知道如何解決:(我認爲其中一個人有Htc的感覺,並且我在幾個小時內有3個報告,這就是我所知道的 – Mert 2011-12-24 23:16:20

回答

1

如果您使用多個供應商this link should help。如在描述說:

一個你可能會遇到問題在Android應用具有多發 ContentProviders時進入的是你會發現, SQLiteOpenHelper.onCreate(DB)不要求每一個你 供應商。因此,您最終會丟失幾個 表。

1

我曾經遇到類似的問題,據我記憶,我改變了數據庫幫助類中的數據庫版本以解決問題(請參閱下面的示例,可以將版本從1更改爲2等) 。

private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table titles (_id integer primary key autoincrement, " 
     + "isbn text not null, title text not null, " 
     + "publisher text not null);"; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS titles"); 
      onCreate(db); 
     } 
    }  
相關問題