充分利用SQLite数据库的数据为String的Android [英] Getting data from SQLite database to String Android
问题描述
我想从我的数据库获取信息,并把它变成一个字符串打印在屏幕上。我想我可以用下面的code做,但它给出了关于光标,而不是光标中的信息的一些信息。
数据源=新DataBaseHelper(本);
datasource.open();
光标C = datasource.getAllGoals();
startManagingCursor(C);
字符串G = c.toString();
goal.setText(G);
datasource.close();
光标可以被看作是一个指针,一些基础数据。运行 c.toString()
光标对象将打印默认的光标
类的实现它的字符串再$ P上$ psentation(at符号字符 @
和对象的哈希code的无符号十六进制再presentation),这是不是你想要的。
要检索基础数据库的数据,您需要调用 c.getString(参数:columnIndex)
(来源),或任何列数据类型,你需要特定的列索引。
下面是一个例子从源头修改:
假设你已经创建了一个表
私有静态最后弦乐DATABASE_CREATE =
创建表的评论(
+_id整数主键自动增量
+的评论文字NOT NULL);;
和您的 getAllGoals
函数返回一个指向有关数据光标的两个 _id
和注释
。现在你只想要显示有关注释
列的细节。所以,你必须运行 c.getString(1)
。假设你的 getAllGoals
函数返回一个指针只指向有关注释
列数据。现在,你必须运行 c.getString(0)
。
我建议您下载源$ C $ C所提供的例子,通过数据是如何从游标检索。
编辑:
公开名单<&评论GT; getAllComments(){
清单<&评论GT;评论=新的ArrayList<&评论GT;(); 光标光标= database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns,NULL,NULL,NULL,NULL,NULL); cursor.moveToFirst();
而(!cursor.isAfterLast()){//检索来自多个行的数据
注释注释= cursorToComment(光标);
comments.add(注解);
cursor.moveToNext();
}
//确保关闭游标
cursor.close();
返回意见;
} 私人留言cursorToComment(光标光标){
评论评论=新评论();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
返回评论;
}
I am trying to get information from my database and put it into a string to print it on screen. i thought i could use the code below to do it but it gives out some information about the cursor instead of the information inside the cursor.
datasource = new DataBaseHelper(this);
datasource.open();
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String g = c.toString();
goal.setText(g);
datasource.close();
The cursor can be thought of as a pointer to some underlying data. Running c.toString()
on the cursor object would print the default Cursor
class' implementation of its string representation (an at-sign character @
and the unsigned hexadecimal representation of the hash code of the object) which is not what you want.
To retrieve underlying database data, you will need to call c.getString(columnIndex)
(source), or whatever column datatype you require for that particular column index.
Here's an example modified from source:
Say you have created a table
private static final String DATABASE_CREATE =
"create table comments ( "
+ "_id integer primary key autoincrement, "
+ "comment text not null);";
and your getAllGoals
function returns a cursor which points to data about both _id
and comment
. Now you only want to show the details about the comment
column. So you have to run c.getString(1)
. Suppose your getAllGoals
function returns a cursor which only points to data about the comment
column. Now you have to run c.getString(0)
.
I would recommend that you download the source code in the provided example and go through how data is retrieved from a cursor.
EDIT:
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {//retrieve data from multiple rows
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
这篇关于充分利用SQLite数据库的数据为String的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!