【问题标题】:How do I update a sqlite row entry by string?如何按字符串更新 sqlite 行条目?
【发布时间】:2012-08-27 00:37:52
【问题描述】:

我是 android 的菜鸟,我正在尝试通过字符串而不是行号来更新 sqlite 行条目。我创建一个条目然后按字符串删除一个条目没有问题。但是,当我尝试通过字符串更新条目时,我得到一个空指针异常?非常感谢任何帮助。

我的删除条目的代码可以正常工作:

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

我用于更新无效条目的代码:

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);   
}

我的 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)

【问题讨论】:

  • @Squonk 刚刚发布了我的日志猫。有什么想法吗?
  • 您是否定期关闭数据库?还是使用托管游标?
  • @Squonk 是的。刚刚检查。每次打开后肯定会关闭它。

标签: android database string sqlite nullpointerexception


【解决方案1】:

从您的 logcat 看来,您似乎忘记在某处关闭游标或数据库。
请您在调用这些方法的位置发布代码。

【讨论】:

  • 刚刚找到!它以一种我忽略的方法隐藏在我的代码中。谢谢。
【解决方案2】:

您是否有可以显示有关错误的更多信息的堆栈跟踪?您可以使用 Eclipse 调试器或通过 adb logcat 获得一个。它可能会告诉你什么变量是空的。

如果您收到 NullPointerException,可能您对 ourDatabase.update 的参数之一在不应该为 null 时为 null。抛出一些断言来检查你的假设(或者只使用调试器,它可能会显示问题)。

【讨论】:

  • 我刚刚发布了我的 logcat。有什么想法吗?
猜你喜欢
  • 2012-08-21
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
  • 2023-01-10
  • 2012-03-28
  • 2018-08-06
  • 2014-02-09
  • 2021-05-07
相关资源
最近更新 更多