如何查询特定数据

如何查询特定数据

本文介绍了Android SQLite数据库:如何查询特定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public String getDatatwo() {
    // TODO Auto-generated method stub
    String arg = "lol";
    String query = "select * from DATABASE_TABLE where title=\""+ arg +"\"";
    Cursor c = ourDatabase.rawQuery(query, null);
    int iName = c.getColumnIndex(KEY_NAME);
    String resultsearch = "";
    resultsearch = resultsearch + c.getString(iName);
    return resultsearch;
    }

基本上,它在DATABASE_TABLE中搜索arg("lol")然后将其作为结果搜索返回,稍后显示.我正在尝试使其在KEY_NAME行中搜索整个数据库,并返回包含名称"lol"的ALL.

Basically, it searches DATABASE_TABLE for arg("lol")Then returns it as result search which is later displayed.I am trying to make it search the whole database in the row KEY_NAME and return ALL that contain the name "lol".

如果可能的话,由于类扩展,我还希望能够输入一个编辑arg的TextEdit,我认为我无法操纵arg.如果有人愿意提供帮助,只要有人可以看一下,我就可以发送所有代码.

If possible I also want to be able to input a TextEdit which edits arg, due to class extending, I think I am unable to manipulate arg. If anyone is willing to help, I can send over all code if someone can take a look at it.

但是我得到一个错误.

02-18 12:16:02.201: D/gralloc_goldfish(645): Emulator without GPU emulation detected.
02-18 12:16:24.111: E/SQLiteLog(645): (1) no such table: DATABASE_TABLE
02-18 12:16:24.111: D/AndroidRuntime(645): Shutting down VM
02-18 12:16:24.111: W/dalvikvm(645): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-18 12:16:24.151: E/AndroidRuntime(645): FATAL EXCEPTION: main
02-18 12:16:24.151: E/AndroidRuntime(645): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.sql/com.example.sql.SQLView}:   android.database.sqlite.SQLiteException: no such table: DATABASE_TABLE (code 1): , while   compiling: select * from DATABASE_TABLE where title="lol"
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-18 12:16:24.151: E/AndroidRuntime(645):  at  android.os.Handler.dispatchMessage(Handler.java:99)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.os.Looper.loop(Looper.java:137)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-18 12:16:24.151: E/AndroidRuntime(645):  at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645):  at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:16:24.151: E/AndroidRuntime(645):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-18 12:16:24.151: E/AndroidRuntime(645):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 12:16:24.151: E/AndroidRuntime(645):  at dalvik.system.NativeStart.main(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645): Caused by: android.database.sqlite.SQLiteException: no such table: DATABASE_TABLE (code 1): , while compiling: select * from DATABASE_TABLE where title="lol"
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-18 12:16:24.151: E/AndroidRuntime(645):  at   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-18 12:16:24.151: E/AndroidRuntime(645):  at  com.example.sql.DbMain.getDatatwo(DbMain.java:94)
02-18 12:16:24.151: E/AndroidRuntime(645):  at com.example.sql.SQLView.onCreate(SQLView.java:19)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.Activity.performCreate(Activity.java:5008)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-18 12:16:24.151: E/AndroidRuntime(645):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-18 12:16:24.151: E/AndroidRuntime(645):  ... 11 more
02-18 12:16:24.381: D/dalvikvm(645): GC_CONCURRENT freed 196K, 9% free 7469K/8199K, paused 19ms+33ms, total 192ms
02-18 12:16:26.741: I/Process(645): Sending signal. PID: 645 SIG: 9

修改后的代码,仍然有错误.

Revised code, still errors.

    public String getDatatwo() {
    // TODO Auto-generated method stub
    String arg = "lol";
    Cursor c = ourDatabase.rawQuery("select * from " + DATABASE_TABLE + " where title=?"+ arg +"\"", new String[] { arg });
    int iName = c.getColumnIndex(KEY_NAME);
    String resulttwo = "";
    resulttwo = resulttwo + c.getString(iName);
    return resulttwo;
    }

推荐答案

我假定 DATABASE_TABLE 应该是变量:

String query = "select * from " + DATABASE_TABLE + " where title=\""+ arg +"\"";


还考虑使用参数化查询来保护自己免受SQL注入攻击.


Also consider using parameterized queries to protect yourself from SQL Injection Attacks.

String query = "select * from " + DATABASE_TABLE + " where title=?";
Cursor c = ourDatabase.rawQuery(query, new String[] {arg});

或使用内置方法,例如 SQLiteDatabase.query()

Or use the built-in methods like SQLiteDatabase.query()

添加

当您的应用崩溃时,您应该始终发布LogCat错误以及相关代码,但是我可以立即看到错误.游标可以容纳多个记录或为空,您必须告诉游标您要从中读取的行,并检查该行是否存在.为此,只需添加:

You should always post the LogCat errors along with the relevant code when your app crashes, but I can see an error right away. Cursors can hold more than record or be empty, you must tell the Cursor which row you want to read from and check if this row exists. To do this, simply add:

if (c.moveToFirst()) { /* return true if row exist, false if it doesn't */
    resulttwo = resulttwo + c.getString(iName);
}

如果您想读取多行,请使用循环:

If you want to read more than one row, use a loop:

while (c.moveToFirst()) {
    resulttwo = resulttwo + c.getString(iName);
}

还可以考虑直接使用StringBuilder或仅使用 + = 运算符.

Also consider using a StringBuilder directly or just the += operator.

这篇关于Android SQLite数据库:如何查询特定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 00:10