0

i encountered a little problem with my current project. I am doing an android application which needs to connect to a SQLite database to work through some statements. I believe the statements etc are fine, my only problem is the fact that the connection to the database is not succesfull.

LogCat Error:

04-18 08:20:30.688: E/Database(304): sqlite3_open_v2("jdbc:sqlite:res/raw/randomdb.db", &handle, 1, NULL) failed

So my code so far for connecting to the database is like this:

String url = "jdbc:sqlite:res/raw/randomdb.db";
            SQLiteDatabase db;
            db = SQLiteDatabase.openDatabase(url, null, SQLiteDatabase.OPEN_READONLY);

As you can see, i am trying to acces a database which is located in my project/res/raw folder. Does anyone see the mistake?

!!!UPDATE!!! *I tried to go the way with SQLiteOpenHelper, but still encouner an error i cannot seem to solver. Here is my new code:*

public class DatabaseAdapter extends SQLiteOpenHelper {
    private static String dbPath= "data/data/com.YourPackageName/applicationDb/"; 
    private static String dbName = "YourDBName"; 
    private SQLiteDatabase applicationDatabase;  
    private final Context applicationContext;

    private boolean checkDataBase(){  
        File dbFile = new File( dbPath +  dbName);
        return dbFile.exists();
    }

    public void openDataBase() throws SQLException{
        String fullDbPath= dbPath + dbName;
        applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath,null,SQLiteDatabase.OPEN_READONLY);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    }
}

I get this error: Implicit super constructor SQLiteOpenHelper() is undefined for default constructor. Must define an explicit constructor Any ideas? Would be great!

dave.c
  • 10,862
  • 5
  • 36
  • 61

3 Answers3

0

if you want to connect your android application with SQLite database then you need to extends SQLiteOpenHelper

 public class AbcClass extends SQLiteOpenHelper

after that you need to Override these method: onCreate and onUpgrade and constructor of AbcClass looks like:

public AbcClass(Context context) {
    super(context, DB_NAME, null, VERSION); //public static final String DB_NAME = "test.sqlite";
}
Vinit ...
  • 1,288
  • 9
  • 32
  • 61
0
    public boolean databaseExist()
    {
        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

This is the solution...

OR-----------------------

private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}
0

I think the path you specify is not the correct one and so as mentioned in one of the other answers the database is not found. However I had never used the method openDatabase to read from the raw folder so I do not know which is the correct path.

However once upon a time I had shipped the database along with my application. It resided in the assets folder and once the application started I copied it in the private application storage (much like any database created with SqliteOpenHelper). From then on I used the usual way with SqliteOpenHelper to access the database.

Basically for this I followed the blog mentioned in this thread and because my database file was bigger than 1MB I used the technique described in this thread. Hopefully combining those two you will get your database running!

EDIT Btw you are wrong, openDatabase does not accept url, but path.

Community
  • 1
  • 1
Boris Strandjev
  • 43,262
  • 13
  • 98
  • 123