How do I update a sqlite row entry by string?

2019-09-12 14:58发布

I am a noob to android and I am trying to update a sqlite row entry by string as opposed to row number. I have no problems creating an entry and then deleting an entry by string. However, when i attempt to update an entry by string i get a null pointer exception? Any help is greatly appreciated.

My code for deleting an entry that works fine:

public void deleteEntry(String coin) throws SQLException{
    String[] whereArgs = new String[] { coin };
    ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=?", whereArgs); 

My code for updating an entry that doesn't work:

public void updateEntry(String mCoin, String mQuantity, String mValue) throws SQLException{
    // TODO Auto-generated method stub
    String[] whereArgs = new String[] { mCoin };
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, mCoin);
    cvUpdate.put(KEY_QUANTITY, mQuantity);
    cvUpdate.put(KEY_VALUE, mValue);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=?", whereArgs);   
}

My LogCat:

08-27 01:16:13.734: E/Cursor(1886): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.example.portfolio/databases/PortfolioDatabase, table = cointypeTable, query = SELECT _id, cointype_name, cointype_quantity, cointype_value FROM cointypeTable
08-27 01:16:13.734: E/Cursor(1886): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-27 01:16:13.734: E/Cursor(1886):     at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
08-27 01:16:13.734: E/Cursor(1886):     at  android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
08-27 01:16:13.734: E/Cursor(1886):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
08-27 01:16:13.734: E/Cursor(1886):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
08-27 01:16:13.734: E/Cursor(1886):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
08-27 01:16:13.734: E/Cursor(1886):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
08-27 01:16:13.734: E/Cursor(1886):     at com.example.portfolio.PortfolioDatabase.getData(PortfolioDatabase.java:76)
08-27 01:16:13.734: E/Cursor(1886):     at com.example.portfolio.Portfolio.add(Portfolio.java:1305)
08-27 01:16:13.734: E/Cursor(1886):     at com.example.portfolio.Portfolio.onItemSelected(Portfolio.java:836)
08-27 01:16:13.734: E/Cursor(1886):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
08-27 01:16:13.734: E/Cursor(1886):     at android.widget.AdapterView.access$200(AdapterView.java:42)
08-27 01:16:13.734: E/Cursor(1886):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
08-27 01:16:13.734: E/Cursor(1886):     at android.os.Handler.handleCallback(Handler.java:587)
08-27 01:16:13.734: E/Cursor(1886):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 01:16:13.734: E/Cursor(1886):     at android.os.Looper.loop(Looper.java:123)
08-27 01:16:13.734: E/Cursor(1886):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 01:16:13.734: E/Cursor(1886):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 01:16:13.734: E/Cursor(1886):     at java.lang.reflect.Method.invoke(Method.java:521)
08-27 01:16:13.734: E/Cursor(1886):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 01:16:13.734: E/Cursor(1886):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 01:16:13.734: E/Cursor(1886):     at dalvik.system.NativeStart.main(Native Method)

2条回答
放我归山
2楼-- · 2019-09-12 15:25

From your logcat, it seems you forgot to close the cursor or database somewhere.
Please can you post the code where you are calling these methods.

查看更多
小情绪 Triste *
3楼-- · 2019-09-12 15:25

Do you have a stack trace that can show more information about the error? You can get one using the Eclipse debugger or through adb logcat. It might show you what variable is null.

If you are getting a NullPointerException, probably one of your arguments to ourDatabase.update is null when it shouldn't be. Throws in some asserts to check your assumptions (or just use the debugger, it will probably show the problem).

查看更多
登录 后发表回答