2011-09-13 90 views
2

我是Android開發的新手,所以請耐心等待。我的問題是我有一個外部SQLite文件,我想通過android訪問,我們通常在iPhone或Windows中執行的操作,我們只是將SQlite文件添加爲資源並訪問它,但我無法做到這一點。 我遵循這裏給出的指令Link,但它給出了一個錯誤「無法打開數據庫文件」。訪問外部SQLite文件,Android

編輯:

09-14 23:31:59.008: INFO/Database(335): sqlite returned: error code = 14, msg = cannot open file at source line 25467 
09-14 23:31:59.008: ERROR/Database(335): sqlite3_open_v2("/data/data/com.mobile.socket.server/databases/Database/DictionaryDb.sqlite", &handle, 2, NULL) failed 

我DbHelper類:

package com.dbhandler; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteStatement; 

public class DbHelper extends SQLiteOpenHelper 
{ 
    //The Android's default system path of your application database. 
    private String DB_PATH = "";//"/data/data/com.mobile.socket.server/databases/"; 

    private static String DB_NAME = "DictionaryDb.db"; 

    private SQLiteStatement insertStmt; 

    private SQLiteDatabase myDataBase; 

    /** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
    * @param context 
    */ 
    public DbHelper(Context context) 
    { 
     super(context, DB_NAME, null, 1); 
     //CreateDatabase(); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     myDataBase = db; 
    } 

    private void CreateDatabase() 
    {  
     try 
     { 
      String myPath = DB_PATH + DB_NAME;  
      myDataBase = SQLiteDatabase.openOrCreateDatabase(myPath, null); 
      final String CREATE_TABLE_WORDS = "CREATE TABLE WordsTable (WordId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , Word TEXT NOT NULL)"; 
      final String CREATE_TABLE_MEANINGS = "CREATE TABLE MeaningTable (MeaningId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , WordId INTEGER NOT NULL , Meaning TEXT NOT NULL)"; 
      myDataBase.execSQL(CREATE_TABLE_WORDS); 
      myDataBase.execSQL(CREATE_TABLE_MEANINGS); 
     } 
     catch (Exception e) 
     { 
      e.fillInStackTrace(); 
     } 
    } 

    public boolean AddNewWord(String word) 
    { 
     CreateDatabase(); 
     String myPath = DB_PATH + DB_NAME;  
     //ContentValues newRecord = new ContentValues(); 
     boolean RetVal = false; 
     try 
     { 
      final String INSERT = "Insert into WordsTable(Word) Values('"+word+"')"; 
      myDataBase = myDataBase.openDatabase(DB_NAME, null,SQLiteDatabase.OPEN_READWRITE); 
      insertStmt = myDataBase.compileStatement(INSERT); 
     // myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
      //newRecord.put("Wordstable", word); 
      //long newWordid = myDataBase.insert("Wordstable", null, newRecord); 
     } 
     catch (SQLException e) 
     { 
      e.fillInStackTrace(); 
     } 
     return RetVal; 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

} 
+1

發佈logcat輸出,確保文件存在於您期望的位置。 –

+0

文件存在客棧的位置,我會附上logcat儘快 – KhanZeeshan

+0

如果我創建一個新的數據庫,如下所示這個同樣的錯誤信息被拋出「http://www.higherpass.com/Android/Tutorials/Accessing-Data- With-Android-Cursors /「 – KhanZeeshan

回答

1

我能算出這個我自己,SqLiteHelper的OnCreate中的功能沒有得到調用,所以在類的構造我打電話this.getWritableDatabase()這個強制創建我的數據庫對象的「OnCreate」函數我能完成我的工作。