Android SQLite 查询始终为空
所以现在我在我的应用程序中使用这个解决方案 使用数据库发送应用程序
在我的主要活动中,我只想测试以确保数据库正常工作,因此我所做的只是简单查询以获取一些名称,我所做的就是添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)