2013-01-05 38 views
0

答案是在這個線程:Sqlite issues with HTC Desire HD,感謝@Vikalp帕特爾打開SQLite數據庫 - 沒有這樣的文件或目錄

我這樣做真的簡單的事情 - 從SD卡打開SQLite數據庫和閱讀一些東西從它。例如表的列表:

這是我DataBaseHelper類(真的簡單):

public class DataBaseHelper extends SQLiteOpenHelper{ 
    static Context mContext; 
    private static SQLiteDatabase mDb; 

    public DataBaseHelper(Context context, String name, CursorFactory factory, int version) { 
     super(context, name, factory, version); 
     mContext = context; 
    }   

//skip the overriden methods 

    public void OpenDataBase(String path, String name){      
     mDb = SQLiteDatabase.openDatabase(path + name, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);  

    } 

    public Cursor GetTables(){  
     return mDb.rawQuery("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name", null); 
    } 

} 

當我把這個代碼

mDbPath = Environment.getExternalStorageDirectory() + "/test_db/"; 
      mDbName = "db.sqlite"; 
      mDbHelper = new DataBaseHelper(mContext,mDbPath+mDbName,null,1); 
      mDbHelper.OpenDataBase(mDbPath, mDbName); 
      Toast.makeText(mContext, String.format("Database %s%s successfully opened", new Object[]{mDbPath, mDbName}), Toast.LENGTH_LONG).show(); 
      Cursor cursor = mDbHelper.GetTables(); 

返回遊標包含表的列表。有用。這讓我瘋狂的唯一事情是在日誌中的錯誤消息:

01-05 17:09:44.250: D/Database(21131): dbopen(): path = /mnt/sdcard/test_db/db.sqlite, flag = 2, file size = 3072 
01-05 17:09:44.260: D/Database(21131): dbopen(): path = /mnt/sdcard/test_db/db.sqlite, mode: delete, disk free size: 3106 M, handle: 0x359260 
01-05 17:09:44.260: I/Database(21131): sqlite returned: error code = 8, msg = statement aborts at 1: [pragma journal_mode = WAL;] 
01-05 17:09:44.260: W/Database(21131): dbopen(): sqlite3_exec to set journal_mode to WAL for /mnt/sdcard/test_db/db.sqlite failed 
01-05 17:09:44.260: E/Database(21131): dbopen(): errno = 2, error message = No such file or directory 

錯誤的OpenDatabase方法發生。

有人可以幫我理解爲什麼會出現這個錯誤,我做錯了什麼。 謝謝。

編輯:我測試的是Android 2.3,HTC Desire HD的

+0

變更後試'Environment.getExternalStorageDirectory()+ 「/ TEST_DB /」''到Environment.getExternalStorageDirectory()。getAbsolutePath()+ 「/ TEST_DB /」' –

+0

並不能改變什麼。路徑是正確的,查詢工作正常,但是這個錯誤仍然出現... – mihail

+1

與你的問題相同。可能是值得一拍.http://stackoverflow.com/questions/4718934/sqlite-issues-with-htc-desire-hd –

回答

0

首先,不要在Android上使用硬編碼路徑的任何東西 - 他們不能保證在所有設備上的相同。

嘗試使用getDatabasePath("db_sqlite.db").getAbsolutePath();

相關問題