得到SQLite数据库的数据 [英] Get data from sqlite database
问题描述
我有一个活动,并希望从我SQLite数据库中获取数据。它的工作原理,但我的活动只检索的最后一排,而不是整个列。我能做些什么来让整列?
这是我的数据库中code:
公共字符串的getName(){
的String [] =列新的String [] {KEY_ROWID,KEY_NAME};
光标C = db.query(NAME_TABLE,列,NULL,NULL,NULL,NULL,NULL);
c.moveToFirst();
而(!c.isAfterLast()){
字符串名称= c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
}
c.close();
返回名称;
}
这是在我的活动code联系数据库:
数据库的getData =新的数据库(本);
getdata.open();
字符串数据= getdata.getName();
getdata.close();
见,正如我所说的,没有看到code余米的无奈。而看到你的code后..
您只单返回字符串
从功能的getName()
而是返回的String []
。
注意:这仅仅是伪code ..实际的可能是不同的。
公共字符串[]的getName(){ INT I = 0;
的String [] =列新的String [] {KEY_ROWID,KEY_NAME};
光标C = db.query(NAME_TABLE,列,NULL,NULL,NULL,NULL,NULL);
字符串名称[] =新的String [c.getCount()];
c.moveToFirst();
而(!c.isAfterLast()){
名称[I] = c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
我++;
}
c.close();
返回名称;
}
而这其中,
数据库的getData =新的数据库(本);
getdata.open();
字符串数据[] = getdata.getName();
getdata.close();
I have an activity and want to get data from my sqlite database. It works, but my activity only retrieves the last row and not the whole column. What can i do to get the whole column?
This is the code in my database:
public String getName() {
String[] columns = new String[]{ KEY_ROWID, KEY_NAME };
Cursor c = db.query(NAME_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
while(!c.isAfterLast()) {
String name = c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
}
c.close();
return name;
}
This is the code in my activities to contact the database:
Database getdata = new Database(this);
getdata.open();
String data = getdata.getName();
getdata.close();
See, As I said, Without seeing code I m helpless.. And after seeing your code..
You are returning only single String
from that function getName()
instead it return String[]
.
Note: this is only pseudo code.. Actual may be different..
public String[] getName() {
int i=0;
String[] columns = new String[]{ KEY_ROWID, KEY_NAME };
Cursor c = db.query(NAME_TABLE, columns, null, null, null, null, null);
String name[] = new String[c.getCount()];
c.moveToFirst();
while(!c.isAfterLast()) {
name[i] = c.getString(c.getColumnIndex(KEY_NAME));
c.moveToNext();
i++;
}
c.close();
return name;
}
And this one,
Database getdata = new Database(this);
getdata.open();
String data[] = getdata.getName();
getdata.close();
这篇关于得到SQLite数据库的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!