在数据库 SQLite 中过滤查询

发布于 2024-11-06 07:38:28 字数 213 浏览 0 评论 0原文

我在数据库中有一个表,我只想显示该表的一行。该表有 3 个字段(ID、标题和说明)。 我想根据标题过滤行。

我有这样的代码:

Cursor 游标 = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);

其中第三个字段是选择字段(字符串)。但我不知道必须输入什么才能仅选择我想显示的行。谢谢

I have a table in a database and I would like to show only one row of this table. The table has 3 fields (ID, Title and Description).
I want to filter the rows depending on the Title.

I have this code:

Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);

where the third field is the selection one (a String). But I don't know what I have to put exactly to select only the row that I want to show. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-11-13 07:38:28
String[] FROM = { // ID of the column(s) you want to get in the cursor
        ID,
        Title,
        Description 
};

String where = "Title=?"; // the condition for the row(s) you want returned.

String[] whereArgs = new String[] { // The value of the column specified above for the rows to be included in the response
        "0" 
    };

return db.query(TABLE_NAME, FROM, where, whereArgs, null, null, null);

这将为您提供一个包含所有列的游标,但仅包含“标题”列的值等于 0 的行。

String[] FROM = { // ID of the column(s) you want to get in the cursor
        ID,
        Title,
        Description 
};

String where = "Title=?"; // the condition for the row(s) you want returned.

String[] whereArgs = new String[] { // The value of the column specified above for the rows to be included in the response
        "0" 
    };

return db.query(TABLE_NAME, FROM, where, whereArgs, null, null, null);

This should give you a cursor with all your columns but only containing the rows where the value of the Title column is equal to 0.

计㈡愣 2024-11-13 07:38:28

试试这个

Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null);

try this

Cursor cursor = db.query("TABLE_NAME",new String[]{"ColumnName"}, "ColumnName=?",new String[]{"value"}, null, null, null);
橘香 2024-11-13 07:38:28

您可以在SQLite中通过以下代码进行搜索;

在主活动中;

search.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());
    }
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        return 
//Here you can filter data by any row , just change text replace of "subject"
dbManager.fetchdatabyfilter(constraint.toString(),"subject");
    }
});

数据库助手.java

public Cursor fetchdatabyfilter(String inputText,String filtercolumn) throws SQLException {
Cursor row = null;
String query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME;
if (inputText == null  ||  inputText.length () == 0)  {
    row = database.rawQuery(query, null);
}else {
    query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME+" WHERE "+filtercolumn+" like '%"+inputText+"%'";
    row = database.rawQuery(query, null);
}
if (row != null) {
    row.moveToFirst();
}
return row;
 }

You can search by following code in SQLite;

In MainActivity;

search.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());
    }
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        return 
//Here you can filter data by any row , just change text replace of "subject"
dbManager.fetchdatabyfilter(constraint.toString(),"subject");
    }
});

DatabaseHelper.java

public Cursor fetchdatabyfilter(String inputText,String filtercolumn) throws SQLException {
Cursor row = null;
String query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME;
if (inputText == null  ||  inputText.length () == 0)  {
    row = database.rawQuery(query, null);
}else {
    query = "SELECT * FROM "+DatabaseHelper.TABLE_NAME+" WHERE "+filtercolumn+" like '%"+inputText+"%'";
    row = database.rawQuery(query, null);
}
if (row != null) {
    row.moveToFirst();
}
return row;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文