Android SQLite:接近“...”:语法错误(代码1):,同时编译:SELECT“..”FROM“..”WHERE _id =?

时间:2015-08-06 03:31:14

标签: android sqlite syntax-error

在Android Studio中运行代码时出现此错误。

08-06 04:10:00.209  24514-24514/com.example.mk.anotherapp E/SQLiteLog﹕ (1) near "16": syntax error
08-06 04:10:00.269  24514-24514/com.example.mk.anotherapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mk.anotherapp, PID: 24514
android.database.sqlite.SQLiteException: near "16": syntax error (code 1): , while compiling: SELECT 10 FROM 16 WHERE _id=?
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
        at com.example.mk.anotherapp.Irradfrag$1.onClick(Irradfrag.java:108)
        at android.view.View.performClick(View.java:4445)
        at android.view.View$PerformClick.run(View.java:18446)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5146)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)

以下是相关代码:

            //assign actual values
            Integer pitch = pitchsel;
            Integer orientation = orientaationsel * 5;
            Double shadefactor =   shadesel / 100.0;
            String postregion = "1";

            RegionDatabase mDb  = new RegionDatabase(getActivity());
            SQLiteDatabase db = mDb.getReadableDatabase();
            String[] columns = {"Region"};
            String Selection = "_id=?";
            String[] SelectionArgs ={""+postcodesel};

            Cursor cursor = db.query("Region_Key",columns,Selection,SelectionArgs,null,null,null,null);
            while (cursor.moveToNext()) {
                    postregion = cursor.getString(cursor.getColumnIndexOrThrow("Region"));
                    Toast.makeText(getActivity(),"Region:"+ postregion, Toast.LENGTH_SHORT).show();
                }


             String[] columnsx = {""+orientation};
             String[] SelectionArgsx ={""+pitch};
             String ratio ="1";
// This is the query that the error refers to
            cursor = db.query(postregion,columnsx,Selection,SelectionArgsx,null,null,null,null);
            while (cursor.moveToNext()) {
                ratio = cursor.getString(cursor.getColumnIndexOrThrow(""+orientation));
                Toast.makeText(getActivity(),"Ratio:"+ ratio, Toast.LENGTH_SHORT).show();
            }

真正令我难过的是第一个查询完美运行而第二个查询没有。这是语法错误,语法似乎完全相同。查询在同一数据库中的两个不同表中完成。我已经仔细检查了列名和数据值,以确保所有内容都存在为typed。

其他时候发布了这个错误,通常是rawquery中引用错误的情况,但这不是我正在做的事情。

修改

我的数据库是预制的,由25个表组成,每个表有37列。我是从我必须能够访问的数据的电子表格中生成的。如果每列的名称必须是非数字的,任何想法如何轻松更改列名?

编辑2

谢谢大家。这是由于数字表名称。我在下面用2个解决方案提交了答案。

3 个答案:

答案 0 :(得分:0)

cursor = db.query(postregion,columnsx,Selection,SelectionArgsx,null,null,null,null);
        while (cursor.moveToNext()) {
            ratio = cursor.getString(cursor.getColumnIndexOrThrow(""+orientation));
            Toast.makeText(getActivity(),"Ratio:"+ ratio, Toast.LENGTH_SHORT).show();
        }

columnsx应为n字符串,您发送的是Integer orientation = orientaationsel * 5; 并且您应该指定列的名称,而不是值。

所以你的sintax应该是

    cursor = db.query(postregion,"orientacionColumnName",Selection,SelectionArgsx,null,null,null,null);
        while (cursor.moveToNext()) {
            ratio = cursor.getString(cursor.getColumnIndexOrThrow(""+orientation));
            Toast.makeText(getActivity(),"Ratio:"+ ratio, Toast.LENGTH_SHORT).show();
        }

您的选择应该是您的列规格,而不是整数。

问候。

答案 1 :(得分:0)

根据错误提示,将 postregion (第一个查询arg)替换为 tablename

  

android.database.sqlite.SQLiteException:near&#34; 16&#34 ;:语法错误(代码1):,编译时:SELECT 10 FROM 16 WHERE _id =?

 cursor = db.query(postregion,columnsx,Selection,SelectionArgsx,null,null,null,null);

像这样重复

 cursor = db.query("tableName",columnsx,Selection,SelectionArgsx,null,null,null,null);

答案 2 :(得分:0)

事实证明,表名和列名必须以字母(或至少不是数字)

开头

What are valid table names in SQLite?

改变了一切,现在它们起作用了。

但是,添加这样的引号并不需要我更改整个数据库。这是由某人建议然后他们删除了他们的答案

     String[] columnsx = {String.format("\"%s\"", orientation)};
             Selection = "Slope=?";
             String[] SelectionArgsx ={""+pitch};
             String ratio ="1";

                //This is where the error is called.
            cursor = db.query(String.format("\"%s\"", postregion),columnsx,Selection,SelectionArgsx,null,null,null,null);