阅读格式行[现场]从数据库中的数据 - >值 [英] Read data from database in format row[field] -> value
本文介绍了阅读格式行[现场]从数据库中的数据 - >值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真是糊涂,在Android上的数据库使用。
I got really confused with the database usage in Android.
我有这个数据库:
表名称:文章
字段: ID 文章 chapterid
目前,我有以下code和使用它里面的片段:
i currently have the below code and use it inside a fragment:
List<Comment> contents = ((MainActivity)getActivity()).connectRawQueryDB("database.sqlite", "SELECT * FROM articles WHERE chapterid = 1");
在我MainActivity
And in my MainActivity
public List<Comment> connectRawQueryDB(String DB_NAME, String RawQuery) {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.rawQuery(RawQuery,null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
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;
}
我需要的是做一个数据库查询,然后在下面的格式输出值:
What i need is to make a query to the database and then output values in the following format:
例如。 行[文章] 并获得价值文章并从同一个查询中获取行[ID]并获得ID字段的值。
e.g. row[article] and get the value of article and from the same query get row[id] and get the value in id field.
推荐答案
添加此方法在您dbhandler ....
Add this method in you dbhandler....
public ArrayList<Comment> getCommentdata()
{
//get db
SQLiteDatabase db = getWritableDatabase();
ArrayList<Comment> resultCommentList = new ArrayList<Comment>();
String resultQuery = "select id, article, chapterid from "+YOUR_TABLE;
Cursor cursor = db.rawQuery(resultQuery, null);
if(cursor.moveToFirst())
{
do
{
Comment commentObject = new Comment();
commentObject.setcommentUID(cursor.getInt(0));
commentObject.setarticle(cursor.getString(1));
commentObject.setChapterID(cursor.getInt(2));
resultCommentList.add(commentObject);
}
while(cursor.moveToNext());
}
//close db
if(db != null)
db.close();
return resultCommentList;
}
而当YOUT要以检索数据。
And when yout want to retrive data
allComent = localDBHelper.getCommentdata();
for(int i = 0; i < allComent .size(); i++)
{
int id=allComment.get(i).getcommentUID();
String =allComment.get(i).getarticle();
int cid=allComment.get(i).getChapteID();
}
这篇关于阅读格式行[现场]从数据库中的数据 - &GT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文