如何从SQLite查询数据

时间:2011-02-10 13:57:28

标签: android sqlite

public Cursor getSen_List(long id_thsen ) {
    String strTmp = "select "
                    +SENS_TABLE+"."+KEY_SEN_ID+","
                    +SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
                    +SENS_TABLE+"."+KEY_SEN_ID_LANG+","
                    +SENS_TABLE+"."+KEY_SEN+","
                    +SENS_TABLE+"."+KEY_SEN_READING+","
                    +" from "+SENS_TABLE+ ","
                    +" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" +id_thsen+","
                    +null+"," +null+"," +null+"," +null;
                    return db.rawQuery(strTmp,null);}

这是我获得Sentence列表的方式 但如果我想改变这样的写作方式

public Cursor getSen_List(long id_thsen ) {
    String strTmp = "select "
                    +SENS_TABLE+"."+KEY_SEN_ID+","
                    +SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
                    +SENS_TABLE+"."+KEY_SEN_ID_LANG+","
                    +SENS_TABLE+"."+KEY_SEN+","
                    +SENS_TABLE+"."+KEY_SEN_READING+","                     
                    +" from "+SENS_TABLE+","
                    +" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + id_thsen+","
                    +null+"," +null+"," +null+"," +null+",";
                    return db.rawQuery(strTmp,null);}

有什么不对吗?因为它不起作用

请帮助........

这是我的logcat ........

  

02-10 20:48:18.050:ERROR / AndroidRuntime(818):java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.proj / com.android.proj.Sens_Show}:android.database。 sqlite.SQLiteException:near“from”:语法错误:,同时编译:select Sentences._id,Sentences.id_thsen,Sentences.id_lang,Sentences.sen,Sentences.reading,来自Sentences,其中Sentences.id_thsen1,null,null,null ,空值,   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.os.Handler.dispatchMessage(Handler.java:99)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.os.Looper.loop(Looper.java:123)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread.main(ActivityThread.java:4627)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at java.lang.reflect.Method.invokeNative(Native Method)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at java.lang.reflect.Method.invoke(Method.java:521)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at dalvik.system.NativeStart.main(Native Method)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):引起:android.database.sqlite.SQLiteException:near“from”:语法错误:,同时编译:select Sentences._id,Sentences.id_thsen,Sentences。 id_lang,Sentences.sen,Sentences.reading,来自Sentences,其中Sentences.id_thsen1,null,null,null,null,   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:64)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:80)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:46)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at com.android.proj.DBAdapter.getSen_List(DBAdapter.java:875)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at com.android.proj.Sens_Show.listData(Sens_Show.java:91)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):at com.android.proj.Sens_Show.onCreate(Sens_Show.java:63)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   02-10 20:48:18.050:ERROR / AndroidRuntime(818):... 11更多

2 个答案:

答案 0 :(得分:2)

+" from "+SENS_TABLE"
+" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" +id_thsen+","

试试。

编辑:

+" from "+SENS_TABLE+","不应该有最后一个+"," 和 您错过了+ "="KEY_SEN_ID_TH_SEN

之间的+id_thsen+","

答案 1 :(得分:2)

您当前的查询strTmp会生成此字符串(带有测试值):

“从tblname中选择tblname.key_1,tblname.key_2,tblname.key_3,tblname.key_4,tblname.key_5,其中tblname.key_24,null,null,null,null,”

因此,为了使其成为有效的SQLite查询,您应该使用此代码

String strTmp = "select "
        +SENS_TABLE+"."+KEY_SEN_ID+","
        +SENS_TABLE+"."+KEY_SEN_ID_TH_SEN+","
        +SENS_TABLE+"."+KEY_SEN_ID_LANG+","
        +SENS_TABLE+"."+KEY_SEN+","
        +SENS_TABLE+"."+KEY_SEN_READING                     
        +" from "+SENS_TABLE
        +" where "+SENS_TABLE+"."+KEY_SEN_ID_TH_SEN + "=" + id_thsen;

导致: “从tblname中选择tblname.key_1,tblname.key_2,tblname.key_3,tblname.key_4,tblname.key_5,其中tblname.key_2 = 4”