问题来自阅读从SQLite数据库 [英] Issue From Reading from a SQLite Database
问题描述
我没有太多的知识,使用光标读SQLite数据库的数据,但使用一些参考和示例我编了几个设置这个我不为什么它会从数据库中读取,我检查,如果数据被输入到使用数据库浏览器和present数据库可以有一个人告诉我如何使这项工作?
I dont have much knowledge in using the "Cursor" to read data of SQLite database but using some references and examples i compiled a few to setup this i dont why it would read from the database, i check if data is entered into the database using a database browser and its present can some one tell me how to make this work?
我有一个单独的类持有数据库连接和查询,在我有一个函数来获取其中有一个specifed值的long值的所有字段
I have a separate class holding the db connections and queries, in that i have a function to get all fields which have a long value of a specifed value
public Cursor getAppointments(long date) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
APP_TIME,
APP_TITLE,
},
APP_DATE + "=" + date,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
这是什么code是应该做的是让所有具有相应的日期作为查询值和返回的记录。
what this code is supposed to do is get all the records which have the corresponding date as the queried value and return.
现在在主类我有这个code捕捉和显示一个TextView,逐行。
Now in the main class i have this code to catch and display in a Textview, row by row.
db.open();
Display.setText("");
Cursor c = db.getAppointments(SDate);
if (c.moveToFirst())
Display.setText(DisplayTitle(c));
else
Toast.makeText(this, "No title found",
Toast.LENGTH_LONG).show();
db.close();
和显示标题的方法是
private String DisplayTitle(Cursor c) {
String Final ="";
Final = c.getString(1) + " " +c.getString(2) + " " + c.getString(3) + "\n" ;
return Final;
}
有人能告诉我什么是必要的序检索具有相同的long值的日期和在TextView中显示它的记录,可以有多个记录与同一长值,因此必须检索所有的变化与日期相同的长值的记录,并在TextView中显示。
Can someone tell me what are the changes necessary inorder to retrieve the records which have the same long value as the date and display it in the textview, there can be multiple records with the same long value, so have to retrieve all the records with the same long value for date and display it in the textview.
干杯,Looing一个快速的答复。 :)
Cheers, Looing for a fast reply. :)
推荐答案
使用这个简单的使用游标的方法:
Use this simple approach of using cursor:
String q = "SELECT * FROM DATABASE_TABLE where(APP_DATE like '"+date+"')";//your query
Cursor c = db.rawQuery(q, null); // db is a object of SQLiteDatabase type
然后,使用列名和检索相应列的值 getColumnIndex()方式,如果你不能确定列索引,如:
Then retrieve value of respective column using column name with getColumnIndex() method if you are not sure about column index like:
if(c.getCount() == 0)
{
//No entry found
}
else {
c.moveToFirst();
do {
String mail = c.getString(c.getColumnIndex("KEY_ROWID"))+c.getString(c.getColumnIndex("APP_TIME"))+c.getString(c.getColumnIndex("APP_TITLE"));
// do whatever you like with each record
} while (c.moveToNext());
c.close();
db.close();
这篇关于问题来自阅读从SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!