gpt4 book ai didi

java - 在 Android 中从 SQLite 数据库创建一个字符串数组

转载 作者:行者123 更新时间:2023-11-29 20:59:26 26 4
gpt4 key购买 nike

我需要生成一个如下所示的数组:

String[] testlist = new String[] {"First Note", "Second Note", "Third Note"};

我需要的信息在 SQLite 数据库中。我正在使用以下代码访问:

db = new notesDBHelper(this);

//notesDBHelper is a standard SQLiteHelper, the DB has a table called notes with fields 'title' and 'note_text'

String query = String.format("SELECT * FROM notes");
db.getReadableDatabase().rawQuery(query, null);

这是我卡住的地方 - 查询返回时没有错误,但我不确定构建字符串的语法。

最佳答案

您应该在查询中使用游标。像这样的函数(来自头顶的未经测试的代码)可以在您的数据库助手类中工作。

public ArrayList<String> getStrings() {

ArrayList<String> strings = new ArrayList<String>();
String query = String.format("SELECT * FROM notes");
Cursor c = db.getReadableDatabase().rawQuery(query, null);

if (c.moveToFirst())
do {
strings.add(sessionCursor.getString(1)); // 1 = columnindex
} while (sessionCursor.moveToNext());

return strings;

}

关于java - 在 Android 中从 SQLite 数据库创建一个字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26598155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com