1

I think i'm about ready to jump off a cliff. I've been trying to make preloaded sqlite databases to for bulk inserting purposes. No matter what my db files never open and the app crashes. finally i've tried to use the most idiot-proof method here, and i still come up short: https://github.com/jgilfelt/android-sqlite-asset-helper

Here's the pertaining logcat:

09-11 13:41:34.359: I/Database(2913): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-11 13:41:34.359: E/Database(2913): sqlite3_open_v2("/data/data/com.anthonyce.mcathomie/databases/northwind", &handle, 2, NULL) failed
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): could not open database northwind - unable to open database file
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): copying database from assets...
09-11 13:41:34.369: W/SQLiteAssetHelper(2913): extracting file: 'Northwind-v1.db'...
09-11 13:41:34.779: W/SQLiteAssetHelper(2913): database copy complete
09-11 13:41:34.869: I/SQLiteAssetHelper(2913): successfully opened database northwind
09-11 13:41:34.889: D/AndroidRuntime(2913): Shutting down VM
09-11 13:41:34.889: W/dalvikvm(2913): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-11 13:41:34.910: E/AndroidRuntime(2913): FATAL EXCEPTION: main
09-11 13:41:34.910: E/AndroidRuntime(2913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anthonyce.mcathomie/com.anthonyce.mcathomie.GameActivity}: java.lang.ClassCastException: android.database.sqlite.SQLiteCursor
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.os.Looper.loop(Looper.java:123)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invoke(Method.java:507)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at dalvik.system.NativeStart.main(Native Method)
09-11 13:41:34.910: E/AndroidRuntime(2913): Caused by: java.lang.ClassCastException: android.database.sqlite.SQLiteCursor
09-11 13:41:34.910: E/AndroidRuntime(2913):     at com.anthonyce.mcathomie.GameActivity.onCreate(GameActivity.java:25)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-11 13:41:34.910: E/AndroidRuntime(2913):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-11 13:41:34.910: E/AndroidRuntime(2913):     ... 11 more

The code. i kid you not, I am using the same exact example file. I have been brought to my knees copying the sample step by step and i still get these fustrating errors.

public class McatDatabase extends SQLiteAssetHelper{

private static final String DB_NAME = "northwind";
private static final int DB_VERSION = 1;
private static final String[] COLUMNS = {"LastName"};
private static final String TABLE_NAME = "Employees";

public McatDatabase(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

public Cursor getQuestions() {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);
c.moveToFirst();
return c;
}

}

In another class:

public class GameActivity extends Activity {

    private McatDatabase db;
    private Cursor thequestions;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        db = new McatDatabase(this);
        thequestions = db.getQuestions();

        TextView tv = (TextView)findViewById(R.id.TextView1);
        tv.setText((CharSequence) thequestions);
        thequestions.close();
        db.close();
    }

}
mango
  • 5,577
  • 4
  • 29
  • 41
  • do you have the db with the name northwind in your asset file? and note it should be a zip file and should be contained inassets/databases/northwind.zip – Athul Harikumar Sep 11 '12 at 13:55
  • yes i do, i copied and pasted from the sample file. the database transfers. i can look it and read in DDMS. – mango Sep 11 '12 at 14:01
  • 1
    What's the line of code in GameActivity on line 25? – Barak Sep 11 '12 at 14:04
  • 1
    You are trying to cast a cursor to characters (`tv.setText((CharSequence) thequestions);`), thus you are getting a cast exception. – Barak Sep 11 '12 at 14:10
  • http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 – Yaqub Ahmad Sep 11 '12 at 16:05

1 Answers1

0

You are trying to cast your cursor to a CharSequence, and the sytem doesn't like that so is throwing a ClassCast exception at you.

If you put the Cursor into a SimpleCursorAdapter, you can then use the convertToString method to do the conversion. Otherwise, you need to iterate through the cursor yourself doing the conversion.

The literal question answered, it looks to me like you are trying to make a list of questions, so I'm wondering why you are using the method you are rather than using an adapter and creating a list (which is something Android is very good at).

If you are new to the framework and don't understand how to do it there are many tutorials and when you get stuck you can come here and ask form some help getting over the sticking point.

Barak
  • 16,318
  • 9
  • 52
  • 84
  • thanks alot for the input. you're right, i am new to framework. I would eventually try a more conventional method but for now i'm self-teaching and working through tutorials. I was thinking that this was the most basic method i could try before moving up. I commented out my attempt to set text and there were no errors, so i assume that you're right. i apoligize such a trivial question – mango Sep 11 '12 at 15:27