0
這真的很頭痛因爲只是一些人有這個麻煩..我嘗試了很多東西,真的很累。我給的代碼,並祈禱:(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
我記得很少有人有這個問題,似乎沒有明確的答案,除此之外,請完成卸載並將表名改爲別的。仍然沒有保證,但爲數不多的工作。 – kosa 2012-01-29 12:19:48
@Mert:'String outFileName =「/data/data/ko.tb/databases/」' - 嚴重的......不這樣做。這可能不是你的問題的原因,但是使用硬編碼的路徑在某些時候只會導致新的問題。 – Squonk 2012-01-29 12:29:42
你確定KelimeDB1.db包含該表嗎?也因爲你是從外部注入數據庫你確保所有的表都以_id列開頭?你的更新過程看起來很奇怪。爲什麼不使用默認的SQLITEHELPER更新/創建過程? – masi 2012-01-29 12:33:24