0

all. I want to insert some initial data in db(sqlite) when it was created.

if there is no insert operations, the programe works fine. but when I add it, It cause error.I can't understand.It seems to be android get a read-able db-handle to write it? I am not sure. Or maybe android doesn't allow to insert data at DatabaseHelper's onCreate() method? hope somebody can solve my problem.

  02-16 12:34:20.445: ERROR/AndroidRuntime(24947): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 3: /data/data/com.kbonez.prodo/databases/prodo.db

02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:170)
**02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at com.kbonez.prodo.ProdoProvider.query(ProdoProvider.java:164)**
02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at android.content.ContentProvider$Transport.query(ContentProvider.java:130)
02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at android.content.ContentResolver.query(ContentResolver.java:202)
02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at android.app.Activity.managedQuery(Activity.java:1495)
02-16 12:34:20.445: ERROR/AndroidRuntime(24947):     at com.kbonez.prodo.ProdoList.onCreate(ProdoList.java:129)

I look at line 164 ,it is

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

I just add those code in my Provider.DatabaseHelper.onCreate()

final String insTemplate="insert into %s (%s,%s)  values (%s);";            

        for(String i: ct.getResources().getStringArray(R.array.helpInit)){
            db.execSQL(String.format(insTemplate,PRODO_TABLE_NAME,Structer.TITLE,Structer.NOTE,i)); 
        }

========================================================== solved!

Thanks all guys , I tried it . I use this

    // Get the database and run the query
    try{
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor c = qb.query(db, projection, selection, selectionArgs, null,
            null, orderBy);

    // Tell the cursor what uri to watch, so it knows when its source data
    // changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
    }catch(SQLException e){
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        return null;
    }

but I can't understand ,why android don't do it , and need ourself to do it.

9nix00
  • 3,852
  • 2
  • 21
  • 27

3 Answers3

1

For execute insert query you should open data in writable mode.

SQLiteDatabase db = mOpenHelper.getWritableDatabase();
AlexKorovyansky
  • 4,873
  • 5
  • 37
  • 48
  • I still have issue. the line 164 is in query() method. you mean, if I want to insert those initial data. I should try to get a writeable database handle first even in query method? – 9nix00 Feb 16 '11 at 05:40
  • In any case, onCreate method is used for construct database, so first you should execute CREATE TABLE, and next if you want insert initial values you should execute INSERT. Inside onCreate you have income parameter SQLiteDatabase db, you don't need to get it using mOpenHelper.getReadableDatabase() or mOpenHelper.getWritableDatabase(). – AlexKorovyansky Feb 16 '11 at 05:59
  • please check this example http://thinkandroid.wordpress.com/2010/01/13/writing-your-own-contentprovider/ – AlexKorovyansky Feb 16 '11 at 05:59
  • yes, I know.although this programe works now, But I still can't understand. I paste the whole code at http://paste.pocoo.org/show/339387/ and I add a comment ,you can search it .use "// problem here! if I only use getReadable , the programe will cause error." – 9nix00 Feb 16 '11 at 06:08
  • Sorry, but I don't understand reason of your problem. In your code, I see some bad places. For example, why do you try catch SQLException in query method? (line #173-#177 is strange) I think, the root of problem is in different place. Don't suppress SQLException and may be you know more about real problem (if you return null, system will fall in other place but with strange and weird exception). Good luck! – AlexKorovyansky Feb 16 '11 at 19:20
1

You are opening database in Readable Mode, that's why this is throwing this exception.

try opening database in Writable Mode as like this:

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
0

Make sure this :

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

is NOT in your main thread

Li3ro
  • 1,837
  • 2
  • 27
  • 35