gpt4 book ai didi

android - android 中正确的 SQLite 查询语法

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

我必须根据传递的参数获取数据库的行

例如,选择所有按姓名排序的 38 岁且为女性艺术家的人,传递的参数是

String age=38;
String gender=female;
String job=artist;

我试过写这个,但我的 rawQuery 肯定有语法错误

public Cursor getData(String age, String gender, String job) {
try {
Cursor c = mDb.rawQuery("SELECT * FROM mytable order by Name asc WHERE Age='"+age+"'" WHERE Gender='"+gender+"'" WHERE Job='"+job+"'", null);
if (c != null) {
c.moveToNext();
}
return c;
} catch (SQLException mSQLException) {
Log.e(TAG, "getData>>" + mSQLException.toString());
throw mSQLException;
}

}

有人可以帮我解决吗?

最佳答案

如果年龄是字符串类型尝试:

Cursor c = mDb.rawQuery("SELECT * FROM mytable WHERE Age LIKE \""+age+"\" AND Gender LIKE \""+gender+"\" AND Job LIKE \""+job+"\" order by Name asc", null);

如果 Age 是 Integer 类型,请尝试:

Cursor c = mDb.rawQuery("SELECT * FROM mytable WHERE Age = \""+age+"\" AND Gender LIKE \""+gender+"\" AND Job LIKE \""+job+"\" order by Name asc", null);

问候。

关于android - android 中正确的 SQLite 查询语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637567/

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