简体   繁体   中英

Accessing External SQLite File, Android

I'm a newbie to android development so please bear with me. My question is i have an external SQLite file that I want to access through android, what we normally do in iPhone or windows that we simply add the SQlite file as a resource and access it but I'm unable to do the same. I followed the instruction given here Link but it gives an error " unable to open database file ".

Edited:

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

My DbHelper Class:

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

    }

}

我自己能弄清楚这一点,没有调用SqLiteHelper的OnCreate函数,因此在类构造函数中,我将其称为this.getWritableDatabase()这使创建数据库对象的“ OnCreate”函数得以完成工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM