如何将所有列中的特定数据导入单个数组

时间:2012-08-15 21:02:45

标签: android android-sqlite

我需要以数组的形式从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);

我是初学者,任何事都会有所帮助。

谢谢

1 个答案:

答案 0 :(得分: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;
}