KhanZeeshan
KhanZeeshan

Reputation: 1410

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

    }

}

Upvotes: 2

Views: 1652

Answers (1)

KhanZeeshan
KhanZeeshan

Reputation: 1410

I was able to figure this out myself, OnCreate function of SqLiteHelper was not getting called so in the class constructor I called this.getWritableDatabase() this forced "OnCreate" function that created my database object I was able to get my work done.

Upvotes: 1

Related Questions