2016-08-16 54 views
0

我是Android Studio中的新成員,我剛開始使用SQLite 我創建了一個SQLite文件(db),並創建了一個將表中的一些數據放入的函數:從SQLite獲取信息(Android Studio)

public String FirstInsert() 
    { 
     String meassage = new String(); 

     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 

     cv.put(COL_2,"hi how are you?"); 
     cv.put(COL_3,"blabla"); 
     cv.put(COL_4,"blabla"); 
     cv.put(COL_5,"blabla"); 
     cv.put(COL_6,"blabla"); 
     cv.put(COL_7,"blabla"); 
     cv.put(COL_8,"blabla"); 
     cv.put(COL_9,"blabla"); 
     cv.put(COL_10,"blabla"); 
     cv.put(COL_11,"blabla"); 


     long result = db.insert(TABLE_NAME,null,cv); 
     if(result == -1) 
     { 
      meassage = new String("Bad"); 
     } 
     else 
     { 
      meassage = new String("Good"); 
     } 
     return meassage; 

    } 

我希望做更多的spesific選擇只需要一個項目,該COL_2而行的是,我得到所以我寫了這個碼數:

public Cursor GetQuestions(int num) 
    { 
     //num++; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor res = db.rawQuery("SELECT QUESTION FROM " + TABLE_NAME + "ORDER BY " + num + "ASC LIMIT 1",null); 
     return res; 
    } 

這是正確的代碼?如果這也是我爲什麼在這裏得到一個錯誤?

非常感謝您的幫助!

回答

0

是,這是因爲在sqlite代碼 的單詞之間沒有空格,並且最好在每次查詢後關閉您的光標和您的數據庫 像這樣

res.close(); 
db.close(); 
0

我剛看到,我已在SQLite的代碼這樣做的空間。

Cursor res = db.rawQuery("SELECT QUESTION FROM " + TABLE_NAME + "**&**ORDER BY " + num + "**&**ASC LIMIT 1",null); 

& =空間(我寫&顯示,我添加了空間