2011-12-24 126 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); 
     } 
    }