0

我需要从 SQLite 数据库(存储它的位置)以数组的形式导入名称列表。这是正确的方法吗

数据库代码

public String queryAll() {
    // TODO Auto-generated method stub
    String [] columns = new String [] {KEY_NAME};
    Cursor point = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iName = point.getColumnIndex(KEY_NAME);

    for(point.moveToFirst();!point.isAfterLast();point.moveToNext()){
        result = result + point.getString(iName);
    }
    return result;

代码到我应该将数据导入到的位置

    DBContact info = new DBContact (this);
    info.open();
    String data[] = info.queryAll();
    info.close();
    NewContact = (Button) findViewById(R.id.bAddContact);

我是初学者,任何事情都会有很大帮助。

谢谢

4

1 回答 1

1
public String[] queryAll() {
    String [] columns = new String [] {KEY_NAME};
    Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    if (cursor != null) {
        try {
            final int nameColumnIndex = cursor.getColumnIndex(KEY_NAME);
            List<String> names = new ArrayList<String>();
            while (cursor.moveToNext()) {
                names.add(cursor.getString(nameColumnIndex));
            }
            return names.toArray(new String[names.size()]);
        } finally {
            cursor.close();
        }
    }
    return null;
}
于 2012-08-15T21:24:18.497 回答