2015-03-02 74 views
0

我想在我的應用程序中添加sqlite我已經創建了數據庫和表,但是SELECT和INSERT查詢不起作用我不知道爲什麼? 在這裏,我已經加了我的代碼:Android SQLITE查詢異常錯誤?

static Cursor c; 
SQLiteDatabase db; 

db=openOrCreateDatabase("weatherregisterDB", Context.MODE_PRIVATE, null); 
     db.execSQL("CREATE TABLE IF NOT EXISTS weatherRegister(id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR,username VARCHAR,password VARCHAR,confirmpassword VARCHAR);"); 


     register.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       db.execSQL("INSERT INTO weatherRegister(id,name,username,password,confirmpassword) VALUES('',sample,sample,sample,sample);"); 

       c=db.rawQuery("SELECT username FROM weatherRegister WHERE username=sample", null); 

我的錯誤是:

android.database.sqlite.SQLiteException: no such column: sample (code 1): , while compiling: INSERT INTO weatherRegister(id,name,username,password,confirmpassword) VALUES('',sample,sample,sample,sample); 
+1

也顯示你的logcat – 2015-03-02 06:03:58

+0

什麼錯誤你得到pls分享到 – eLemEnt 2015-03-02 06:04:34

+0

CREATE TABLE查詢是否工作?可能不是因爲*列名和類型之間缺少空格*請分享崩潰日誌 – 2015-03-02 06:04:52

回答

3

你能提供的錯誤?

,但在第一次看這條線

db.execSQL("INSERT INTO weatherRegister(id,name,username,password,confirmpassword) VALUES('',sample,sample,sample,sample);"); 

應該是:

db.execSQL("INSERT INTO weatherRegister(id,name,username,password,confirmpassword) VALUES('sample','sample','sample','sample');"); 

你不必線( '')爲(ID),如果是自動遞增,也(字符串)值應介於( '')

和相同的選擇查詢

原件:

SELECT username FROM weatherRegister WHERE username=sample 

更新到:

SELECT username FROM weatherRegister WHERE username='sample' 

希望這將幫助你告訴我們,錯誤面前。

+0

現在錯誤是這樣的::引起:android.database.sqlite。 SQLiteDatatypeMismatchException:數據類型不匹配(代碼20) – Imran 2015-03-02 06:20:07

+0

謝謝你的工作正常:) – Imran 2015-03-02 07:00:23

0

而不是使用VARCHAR數據類型使用TEXT作爲您的屬性的數據類型。

+0

謝謝你的回答:) – Imran 2015-03-02 07:00:38

1

Ahmad Okaily的回答是正確的。還有一件事是你不需要在你的insert語句中寫入_id,因爲它是自動遞增的。
我試過它在SQLite命令行中,似乎它工作正常。
無法爲您提供Android代碼版本,因爲我公司的電腦在eclipse或android studio中速度太慢。
SQLite version 3.8.7.4 2014-12-09 01:34:36 Enter ".help" for usage hints. sqlite> INSERT INTO weatherRegister(id,name,username,password,confirmpassword) VALUES('sample','sample','sample','sample'); Error: 4 values for 5 columns sqlite> INSERT INTO weatherRegister(name,username,password,confirmpassword) VALUES('sample','sample','sample','sample'); sqlite> select * from weatherRegister; 1|sample|sample|sample|sample sqlite>

1

嘗試改變這一行

c=db.rawQuery("SELECT username FROM weatherRegister WHERE username=sample", null); 

c=db.rawQuery("SELECT username FROM weatherRegister WHERE username='sample'", null); 

請注意附近的 「樣本」 倒立逗號。希望它會help.Thanks。