Android SQLite 查询始终为空

发布于 2024-10-28 02:16:03 字数 1387 浏览 1 评论 0原文

所以现在我在我的应用程序中使用这个解决方案 使用数据库发送应用程序

在我的主要活动中,我只想测试以确保数据库正常工作,因此我所做的只是简单查询以获取一些名称,我所做的就是添加 3 行(在我添加它们的地方进行了注释):

       DatabaseHelper myDbHelper;
       SQLiteDatabase myDb = null;

       myDbHelper = new DatabaseHelper(this);
       /*
        * Database must be initialized before it can be used. This will ensure
        * that the database exists and is the current version.
        */
        myDbHelper.initializeDataBase();
        Cursor c;
        String s = null;
        try {
           // A reference to the database can be obtained after initialization.
           myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
           c = myDb.rawQuery("select name from breads", null);
           s = c.getString(1);
           c.close();
           /*
            * Place code to use database here.
            */
        } catch (Exception ex) {
           ex.printStackTrace();
        } finally {
           try {
               myDbHelper.close();
           } catch (Exception ex) {
               ex.printStackTrace();
           } finally {
               myDb.close();
           }
       }

但是, s 只是保持为空。如果我在控制台中执行完全相同的查询select name from breads,我将获得数据。有人有什么想法吗?

So right now I'm using this solution in my app Ship an application with a database

In my main activity, I just want to test to make sure that the database is working, so all I'm doing it a simply query to get some names, all I did was add 3 lines(commented where I added them):

       DatabaseHelper myDbHelper;
       SQLiteDatabase myDb = null;

       myDbHelper = new DatabaseHelper(this);
       /*
        * Database must be initialized before it can be used. This will ensure
        * that the database exists and is the current version.
        */
        myDbHelper.initializeDataBase();
        Cursor c;
        String s = null;
        try {
           // A reference to the database can be obtained after initialization.
           myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
           c = myDb.rawQuery("select name from breads", null);
           s = c.getString(1);
           c.close();
           /*
            * Place code to use database here.
            */
        } catch (Exception ex) {
           ex.printStackTrace();
        } finally {
           try {
               myDbHelper.close();
           } catch (Exception ex) {
               ex.printStackTrace();
           } finally {
               myDb.close();
           }
       }

However, s just remains empty. If I do the exact same query select name from breads in the console, I will get data. Does anyone have any ideas?

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

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

发布评论

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

评论(1

愁杀 2024-11-04 02:16:03
myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
c = myDb.rawQuery("select name from breads", null);
c.moveToFirst(); // ADD THIS
s = c.getString(1);
c.close();
myDb = myDbHelper.getWritableDatabase();
//the next 3 lines are all I added
c = myDb.rawQuery("select name from breads", null);
c.moveToFirst(); // ADD THIS
s = c.getString(1);
c.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文