我们如何能在我们自己的SQLite的搜索? [英] How we can search on our own SQLite ?

查看:123
本文介绍了我们如何能在我们自己的SQLite的搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和很多桌的SQLite和我想打一个文本框这是能够在SQL查询,我该怎么办呢?

I've a Sqlite with many tables and I want to make a TextBox which is able to search on SQL , How can I do this?

推荐答案

首先:这个问题是太大的范围之内。第二:你通常不希望别人搜索你的整个数据库......原因很明显:内容,他们不应该看到,像其他用户的密码。
所以,你必须编译的全部内容,应该是先搜索列表。在每个在你的数据库的表中可以通过例如搜索栏:

First of all: That Question is too big of a scope. Second: You usually don't want people to search your entire DB... for obvious reasons: Content they should not see, like Passwords of other users. So you have to compile a List of all contents that should be searchable first. On each of those tables in your DB you can search the columns by for example:

SELECT linkToResource FROM tableName WHERE allowedContent1 like '%searchphrase%' or allowedContent2 like '%searchphrase%';

这是假设你有一个表形如 ID,linkToResource,allowedContent1,allowedContent2,secretContent1,secretContent2

This is assuming you have a Table shaped like ID, linkToResource, allowedContent1, allowedContent2, secretContent1, secretContent2

在Android的这一声明是这样的:

In Android that statement looks like this:

    public List<String> getLinksToSearchResults(String searchphrase) {
    List<String> result = new ArrayList<String>();

    Cursor c = yourDatabase.query(
            "yourTableName", 
            new String[] { "linkToResource" }, // The column you want as a result of the Query
            "allowedContent1 like '%?%' OR allowedContent2 like '%?%'", // The where-Clause of the statement with placeholders (?)
            new String[] { searchphrase, searchphrase }, // One String for every Placeholder-? in the where-Clause
            null, // if you like a order by put it here
            null, // group by here
            null  // having here
            );
    while (c.moveToNext()) {
        result.add(c.getString(0));
    }
    c.close();
    return result;
}

您从中检索的EditText这样的字符串:

You retrieve a String from a EditText like this:

String searchphrase = searchphraseEditText.getText();

下面是希望你得到它的一个想法:D结果
下一次,请提供一些code具有更precise问题(例如为什么不会以下code返回四位INT?)

Here is hoping you got an idea of it :D
Next time please provide some code with a more precise problem (e.g. "Why won't the following code return a four digit int?")

享受!

这篇关于我们如何能在我们自己的SQLite的搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆