Sqlite数据库出错

时间:2013-05-18 10:24:32

标签: android sqlite

使用Sqlite运行我的应用程序时,我收到以下错误。

SQLiteLog(779): (1) near "SQXC31DLT": syntax error
System.out(779): DATABASE ERROR android.database.sqlite.SQLiteException: near "SQXC31DLT": syntax error (code 1): , while compiling: SELECT  count(*) FROM MessageTable  WHERE userID =? SQXC31DLT

代码:

这是我的查询

   String query = "INSERT INTO MessageTable (user_message_id,message,title,message_time,mp3,userID) values ('" + 

              umsermessgid + "','" + Messages +"','" + Title +"','" + timedate+ "','" + musicmp3 + "','" + userid + "')";


                     sqlHandler.executeQuery(query);
                        showList();

这是剩下的showlist()方法:

 private void showList() {
            // TODO Auto-generated method stub
            contactList = new ArrayList<ContactListItems>();
             contactList.clear();
             userid=    mDataStorage.getToken(FirstActivity.this);

String query = "SELECT  count(*) FROM MessageTable  WHERE userID = '" + userid +"'";              Cursor c1 = sqlHandler.selectQuery(query);
              if (c1 != null && c1.getCount() != 0) {
               if (c1.moveToFirst()) {
              //  do {
                 ContactListItems contactListItems = new ContactListItems();

                 contactListItems.setSlno(c1.getString(c1.getColumnIndex("ID")));
                 contactListItems.setmessage(c1.getString(c1.getColumnIndex("message")));
                 contactListItems.settitle(c1.getString(c1.getColumnIndex("title")));
                 contactListItems.setmp3(c1.getString(c1.getColumnIndex("mp3")));
                 contactListItems.setusermessageid(c1.getString(c1.getColumnIndex("user_message_id")));
                 contactList.add(contactListItems);

               // } while (c1.moveToNext());
               }
              }
              c1.close();


        }

我检查过所有内容但仍然收到错误

CursorWindow(1211): Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.



   05-18 10:37:11.022: E/AndroidRuntime(1211): FATAL EXCEPTION: main
05-18 10:37:11.022: E/AndroidRuntime(1211): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.database.CursorWindow.nativeGetString(Native Method)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.database.CursorWindow.getString(CursorWindow.java:434)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.example.universal.FirstActivity.showList(FirstActivity.java:330)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.example.universal.FirstActivity.access$1(FirstActivity.java:317)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.example.universal.FirstActivity$GetTask.onPostExecute(FirstActivity.java:137)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.example.universal.FirstActivity$GetTask.onPostExecute(FirstActivity.java:1)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.os.AsyncTask.finish(AsyncTask.java:631)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.os.Looper.loop(Looper.java:137)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-18 10:37:11.022: E/AndroidRuntime(1211):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮我解决这个问题!! @Thanks

3 个答案:

答案 0 :(得分:1)

您的查询错误。

使用:

String query = "SELECT  count(*) FROM MessageTable  WHERE userID =" + userid ;

在你的showList()函数中。

答案 1 :(得分:0)

更改您的查询,如下所示

String query = "SELECT  count(*) FROM MessageTable  WHERE userID = '" + userid +"'";

或尝试以下内容。

Cursor c = db.rawQuery("SELECT count(*) FROM MessageTable where userID = '"+ userid + "'", null);

答案 2 :(得分:0)

你想用你的代码做什么?

这个SQL查询应该是您第一个问题的正确查询:

  

String query =“SELECT count(*)FROM MessageTable WHERE userID ='”   + userid +“'”;    (作者Gunaseelan)

此错误:

  

无法从包含1行的CursorWindow读取第0行第-1列,   1列。

是因为您使用了错误的查询。您正在使用的查询仅返回一个数字 - 计数。 (一列一行)

此代码的正确查询:

  

Cursor c1 = sqlHandler.selectQuery(query);                 if(c1!= null&amp;&amp; c1.getCount()!= 0){                  if(c1.moveToFirst()){                 //做{                    ContactListItems contactListItems = new ContactListItems();

             contactListItems.setSlno(c1.getString(c1.getColumnIndex("ID")));
             contactListItems.setmessage(c1.getString(c1.getColumnIndex("message")));
             contactListItems.settitle(c1.getString(c1.getColumnIndex("title")));
             contactListItems.setmp3(c1.getString(c1.getColumnIndex("mp3")));
             contactListItems.setusermessageid(c1.getString(c1.getColumnIndex("user_message_id")));
             contactList.add(contactListItems);

           // } while (c1.moveToNext());
           }
          }
          c1.close();

应该是:

  

String query =“SELECT * FROM MessageTable WHERE userID ='”+ userid   + “'”;

而不是:

  

String query =“SELECT count(*)FROM MessageTable WHERE userID ='”   + userid +“'”;