gpt4 book ai didi

android - 简单查询 SQLite android

转载 作者:行者123 更新时间:2023-11-29 19:44:05 25 4
gpt4 key购买 nike

我创建了一个数据库,其中包含一个包含 3 列的表。[ID 日期 FCode ZCode].

    // Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CODES_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_DATE + " TEXT,"
+ KEY_FCODE + " TEXT,"
+ KEY_ZCODE + " TEXT" + ")";
db.execSQL(CREATE_CODES_TABLE);
}

我已经设法与 SQLite 数据库通信,但我在从数据库返回一些值时遇到问题。更准确地说,我在数据库中插入一些信息,例如:

    db.addSQLinfo(new SQLCodes("01-07-2016", "100", "200"));
db.addSQLinfo(new SQLCodes("02-07-2016", "101", "201"));
db.addSQLinfo(new SQLCodes("03-07-2016", "102", "202"));
db.addSQLinfo(new SQLCodes("04-07-2016", "103", "203"));
db.addSQLinfo(new SQLCodes("05-07-2016", "104", "204"));
db.addSQLinfo(new SQLCodes("06-07-2016", "105", "205"));
db.addSQLinfo(new SQLCodes("07-07-2016", "106", "206"));

后来我使用这个查询来检索数据。我的目标是根据我设置的日期返回所有值。因此,例如,如果我执行此操作:

        SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(
TABLE_NAME, new String[]{KEY_ID, KEY_DATE, KEY_FCODE, KEY_ZCODE},
"05-07-2016", null, null, null, null);

if (cursor != null)
cursor.moveToNext();

String[] infos = {
cursor.getString(0), // return KEY_ID
cursor.getString(1), // return KEY_DATE
cursor.getString(2), // return KEY_FCODE
cursor.getString(3) // return KEY_ZCODE
};

infos String[] 数组总是 [1, 01-07-2016, 100, 200] 无论我在 db.query 中设置的日期如何。我尝试了其他几种查询方法,但没有结果。

最佳答案

我认为您遗漏了一个参数。

第三个参数是您要检查的列名?第 4 个是您要检查的值。

它应该是:

Cursor cursor = db.query(
TABLE_NAME, new String[]{KEY_ID, KEY_DATE, KEY_FCODE, KEY_ZCODE},
KEY_DATE + "= ?", new String[] {"05-07-2016"}, null, null, null);

关于android - 简单查询 SQLite android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140305/

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