[Android] Sqlite Database Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [Android] Sqlite Database Problem

  1. #1
    Join Date
    Jun 2011
    Posts
    9

    [Android] Sqlite Database Problem

    I keep getting an error in my DDMS:

    Code:
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944): java.lang.IllegalStateException: database not open
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1319)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1279)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1359)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.laytproducts.aresponder.SmsDatabase.selectAllReceive(SmsDatabase.java:62)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.laytproducts.aresponder.MainAct.update(MainAct.java:279)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.laytproducts.aresponder.MainAct.onContextItemSelected(MainAct.java:271)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2819)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:877)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.widget.ListView.performItemClick(ListView.java:3444)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1719)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.os.Handler.handleCallback(Handler.java:587)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.os.Handler.dispatchMessage(Handler.java:92)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.os.Looper.loop(Looper.java:143)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at android.app.ActivityThread.main(ActivityThread.java:4701)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-02 13:44:41.039: ERROR/AndroidRuntime(20944):     at dalvik.system.NativeStart.main(Native Method)
    and this is my code:
    Code:
    public void open(){
    		if(!db.isOpen()){
    			SQLiteDatabase.openDatabase(db.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
    		}
    	}
    
    public List<String> selectAllReceive(){
    		open();
    		List<String> list = new ArrayList<String>();
    		Cursor c = this.db.query(TABLE_NAME, new String[] {"receive"}, null, null, null,
    				null, null); //ERROR HERE
    		if (c.moveToFirst()){
    			while(c.moveToNext()){
    				list.add(c.getString(0));
    			}
    		}
    		c.close();
    		return list;
    	}
    What am I doing wrong?

    Thanks

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: [Android] Sqlite Database Problem

    I've never used SQLiteDatabase but a quick check of a tutorial suggests the call to openDatabase(..) returns an instance of the database which I guess you should then use to access the db.

    You are using a variable db to acces the database but haven't shown where is it assigned an object. You should probably be assigning the value returned by openDatabase(..) to db.

    Not sure why you are testing if !db.isOpen() before opening the database - how does this ever get past the initial state when db is null? Maybe your test should be
    Code:
    if ( db == null || !db.isOpen() ) ...
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jul 2012
    Posts
    3

    Re: [Android] Sqlite Database Problem

    good article on the topic http://www.enterra-inc.com/techzone/...ng_sql_issues/ probably will be useful.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center