2013-02-03 27 views
0

我在我的本地數據庫(sqlite)中插入了一些日期。但是當我想選擇這些數據時,遊標不是構建xml並跳過此塊。我在我的sqlite中有一行。有什麼想法?光標不在SQLite中工作

這是我的代碼:`

public void insertMessages(int msgid,String msgdesc, String date, int createdby,int to) { 
    SQLiteDatabase myDatabase = this.getWritableDatabase(); 

      String query = "INSERT INTO Messages (_id,msgdesc,date,createdby,sendto) "+ 
          " VALUES ("+msgid+",'"+msgdesc+"','"+date+"',"+createdby+","+to+")"; 
      SQLiteStatement st = myDatabase.compileStatement(query); 

    try { 
     st.execute(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    myDatabase.close(); 
    Log.i("insert","insert Messages completed"); 
} 




public String getMessages(int to) { 
     SQLiteDatabase myDatabase = this.getWritableDatabase(); 
     String xml = null; 

     try { 

      String query = "SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto="+to+"";  
      Cursor cs = myDatabase.rawQuery(query, null); 

      xml = "<messagelist>"; 
      *****if (cs.moveToFirst()) {***** 
       do { 
        xml += "<messages>"; 
        xml += "<msgid>"+cs.getInt(cs.getColumnIndex("_id"))+"</msgid>"; 
        xml += "<msgdesc>"+cs.getString(cs.getColumnIndex("msgdesc"))+"</msgdesc>"; 
        xml += "<date>"+cs.getString(cs.getColumnIndex("date"))+"</date>"; 
        xml += "<createdby>"+cs.getInt(cs.getColumnIndex("createdby"))+"</createdby>"; 
        xml += "<to>"+cs.getInt(cs.getColumnIndex("sendto"))+"</to>"; 
        xml += "</messages>"; 
       } while (cs.moveToNext()); 
      } 
      xml += "</messagelist>"; 
      cs.close(); 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     myDatabase.close(); 


     Log.i("select","select completed"); 
     return xml; 
    }` 
+0

什麼是錯誤消息? – smk

+0

你是否收到任何錯誤信息? – GrIsHu

+0

嘗試改變sendto =「+ to +」「>> sendto ='」+加+''「加''比較字符串 – AAnkit

回答

1
"SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto="+to+""; 

您必須分隔單引號的字符串常量。試試這個

"SELECT _id,msgdesc,date,createdby,sendto from Messages Where sendto = '" + to + "'"; 

更好的是使用參數化查詢。

http://sqlite.phxsoftware.com/forums/t/83.aspx

+0

但是爲整數不是串? – DuyguK

+0

啊,好點。一大早在這裏。抱歉。 – Simon