如何让数据库值到字符串数组中的机器人(SQLite数据库) [英] how to get the database value to a String array in android(sqlite Database)
问题描述
我有一个数据库名称为线索(源码安卓)它有一个表 HELLO 包含一列的名称的我可以从该列中的值的字符串。 让我告诉你我的code部分
I have a database name "CUED" (sqlite Android)it have a table HELLO which contain a column NAME I can get the value to String from that column. Let me show you my code section
myDB =hello.this.openOrCreateDatabase("CUED", MODE_PRIVATE, null);
Cursor crs = myDB.rawQuery("SELECT * FROM HELLO", null);
while(crs.moveToNext())
{
String uname = crs.getString(crs.getColumnIndex("NAME"));
System.out.println(uname);
}
这将one.Now我需要的是,我想从数据库,这样我可以将其存储在一个字符串数组的列值打印值之一。
It will print the value one by one.Now what I need is that I want to get the column values from database and so that I can store it in a String Array.
推荐答案
您已经做了最艰难的部分...数组东西是pretty的简单:
You already did the hard part... the array stuff is pretty simple:
String[] array = new String[crs.getCount()];
int i = 0;
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("NAME"));
array[i] = uname;
i++;
}
不管结果如何,我总是建议使用的情况下,集合是这样的:
Whatever, I always recommend to use collections in cases like this:
List<String> array = new ArrayList<String>();
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("NAME"));
array.add(uname);
}
为了比较数组,你可以做这样的事情:
In order to compare the arrays, you can do things like this:
boolean same = true;
for(int i = 0; i < array.length; i++){
if(!array[i].equals(ha[i])){
same = false;
break;
}
}
// same will be false if the arrays do not have the same elements
这篇关于如何让数据库值到字符串数组中的机器人(SQLite数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!