使用游标从 android SQLite3 数据库读取时出现问题

发布于 2024-11-30 08:15:47 字数 1041 浏览 0 评论 0原文

我一生都无法弄清楚为什么这段代码不起作用。它遵循与我的应用程序的其他部分完全相同的格式,只是我将两个属性传递给辅助方法,这应该不是问题,并且我只返回一行,这可能是问题,但如果是的话,我不会理解!

我有一个活动在我调用此命令之前一直有效。

Log.d("Testing","Testing: " + cardID + " " + cardTypeID);
//LogCat displays the right values 11 1

Cursor c = mDbHelper.fetchCard(cardID,cardTypeID);  // <--COMMAND

辅助方法看起来像这样,

public Cursor fetchCard(String cardID,String cardTypeID) {  
    String sql = "SELECT teamName FROM tbl_team JOIN tbl_card ON tbl_team.teamID = tbl_card.teamID WHERE cardID=" + cardID;
    //This returns 1 row when run in a SQLite Database Browser

    Log.d("Testing","Testing :" + cardID + " " + cardTypeID );
    // Program has crashed before this Log
    // Caused by: java.lang.NullPointerException

    Cursor c;
    c =  mDb.rawQuery(sql, null);

    return c;

我倾向于这种方式,因为只有一行已被读取,我需要执行 c.moveFirst() 或其他操作。希望能帮助解决这个问题。另外为什么我没有向 LogCat 写入任何内容。好像直接就失败了???

附言。我已经尝试过 返回 mDb.rawQuery(sql, null); 我相信这与上面的作用相同。

非常感谢 标记

I can't for the life of me figure why this code is not working. It is following exactly the same format as other parts of my application save that I am passing two attributes to the helper method which should not be a problem and I am only returning one row which could be the problem but if it is I don't understand!

I have an activity that works until I call this command.

Log.d("Testing","Testing: " + cardID + " " + cardTypeID);
//LogCat displays the right values 11 1

Cursor c = mDbHelper.fetchCard(cardID,cardTypeID);  // <--COMMAND

The helper method looks like this

public Cursor fetchCard(String cardID,String cardTypeID) {  
    String sql = "SELECT teamName FROM tbl_team JOIN tbl_card ON tbl_team.teamID = tbl_card.teamID WHERE cardID=" + cardID;
    //This returns 1 row when run in a SQLite Database Browser

    Log.d("Testing","Testing :" + cardID + " " + cardTypeID );
    // Program has crashed before this Log
    // Caused by: java.lang.NullPointerException

    Cursor c;
    c =  mDb.rawQuery(sql, null);

    return c;

I'm leaning towards this being because there is only one row which has been read and I need to do a c.moveFirst() or something. Would apriciate help in working this out. Also why don't I get any write out to LogCat. It seems to fail straight away???

PS. I have tried
return mDb.rawQuery(sql, null);
Which I believe does the same as above.

Many Thanks
Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-12-07 08:15:47

您的 mDbHelper 为空。初始化它。学习使用 LogCat,而不是通过日志猜测导致应用程序崩溃的行,这确实很有帮助。

Your mDbHelper is null. Initialize it. And learn to use LogCat instead of guessing with the logs the row that crashes the app, it really helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文