Wini the Pooh
Wini the Pooh

Reputation: 423

SQLite database creation openOrCreateDatabase, what Is the correct path to application database

I'm currently trying out features of Java and Android and I wanted to try out creating of the database. Currently, my code looks like this. The thing is, that it crashes on creating the database. I get this error:

E/SQLiteDatabase: Failed to open database 'fieldgame'. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

also I have another error

03-15 18:39:30.406 32465-32486/com.example.game.fieldgame E/SQLiteLog: (14) cannot open file at line 35652 of [8201f4e1c5] (14) os_unix.c:35652: (2) open(//fieldgame) -

I think the problem is with the path I specified because I only passed the name of the database. My question is how can I simply pass the correct path to my application database so I will be able to read from it

 JSONObject json = jsonParser.makeHttpRequest(urlSendCode, "GET", params);
        SQLiteDatabase fieldgameDB = SQLiteDatabase.openOrCreateDatabase("fieldgame", null);
        fieldgameDB.execSQL("CREATE TABLE IF NOT EXISTS assignments(title VARCHAR,description VARCHAR,assignmentId INT, created_at TIMESTAMP);");
        fieldgameDB.execSQL("INSERT INTO assignments VALUES(json.get(title), json.get(description),json.get(id), json.get(created_at))");
        Cursor cursor;
        cursor = (Cursor) fieldgameDB.rawQuery("SELECT * FROM assignments WHERE assignmentId = 1", null);
        cursor.moveToFirst();
        System.out.print(cursor.getString(cursor.getColumnIndex("description")));

Upvotes: 1

Views: 1616

Answers (2)

MikeT
MikeT

Reputation: 56953

You can get the path to the default/recommended location using the Context's getDatabasePath method, you may need to create the databases directory.

e.g. using your code, adapted for simplicity, within an activity's onCreate method, then :-

    mContext = this;

    // Create the database directory if it doesn't exist 
    File dbpath = mContext.getDatabasePath("fieldgames");
    if (!dbpath.getParentFile().exists()) {
        dbpath.getParentFile().mkdirs();
    }
    SQLiteDatabase fieldgameDB = SQLiteDatabase.openOrCreateDatabase(dbpath,null);
    fieldgameDB.execSQL("CREATE TABLE IF NOT EXISTS assignments(title VARCHAR,description VARCHAR,assignmentId INT, created_at TIMESTAMP);");
    fieldgameDB.execSQL("INSERT INTO assignments VALUES('title', 'description',1, 'created_at')");
    Cursor cursor;
    cursor = (Cursor) fieldgameDB.rawQuery("SELECT * FROM assignments", null);
    cursor.moveToFirst();
    DatabaseUtils.dumpCursor(cursor);
    System.out.print(cursor.getString(cursor.getColumnIndex("description")));
    cursor.close();

Results in :-

03-16 08:01:23.606 13478-13478/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@2337112b
03-16 08:01:23.606 13478-13478/? I/System.out: 0 {
03-16 08:01:23.606 13478-13478/? I/System.out:    title=title
03-16 08:01:23.606 13478-13478/? I/System.out:    description=description
03-16 08:01:23.606 13478-13478/? I/System.out:    assignmentId=1
03-16 08:01:23.606 13478-13478/? I/System.out:    created_at=created_at
03-16 08:01:23.606 13478-13478/? I/System.out: }
03-16 08:01:23.606 13478-13478/? I/System.out: <<<<<

Upvotes: 1

Boe-Dev
Boe-Dev

Reputation: 1595

Have you add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />to your Manifest?

Upvotes: 0

Related Questions