2012-03-30 78 views
0
package nidhin.survey; 

import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

static final String TABLE = "mygrades"; 
static final String NAME = "name"; 
static final String VALUE1 = "value1"; 
static final String VALUE2 = "value2"; 
static final String VALUE3 = "value3"; 
static final String VALUE4 = "value4"; 
static final String VALUE5 = "value5"; 
static final String VALUE6 = "value6"; 
static final String VALUE7 = "value7"; 
static final String VALUE8 = "value8"; 
static final String VALUE9 = "value9"; 
static final String VALUE10 = "value10"; 
static final String VALUE11 = "value11"; 


DatabaseHelper(Context context) 
{ 
    super(context, "grades.sqlite", null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 

    db.execSQL("CREATE TABLE " + TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," + 
      VALUE1 + " TEXT, " + VALUE2 + " TEXT, " +VALUE3 + " TEXT, " + VALUE4 + " TEXT, " +VALUE5 + " TEXT, " +VALUE6 + " TEXT, " +VALUE7 + " TEXT, " +VALUE8 + " TEXT, " +VALUE9 + " TEXT, " +VALUE10 + " TEXT, " + 
      VALUE11 + " REAL);"); 

    ContentValues cv = new ContentValues(); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    // TODO Auto-generated method stub 
    android.util.Log.w("mygrades", "Upgrading database, this will destroy all data"); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE); 
    onCreate(db); 
} 

} 

我遇到問題(可能是語法問題)。我想在SQLite的幫助下在android中創建一個表。我有一個NAME列和VALUE1到VALUE11列。但是,當我嘗試運行代碼時,Logcat中出現以下錯誤:在Android中初始化表格時出現問題

03-30 02:38:16.593: E/AndroidRuntime(693): java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhin.survey/nidhin.survey.Database}: android.database.sqlite.SQLiteException: no such column: value1: , while compiling: SELECT _id, name, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11 FROM mygrades 

任何想法?

+0

請記錄onCreate的輸出字符串。 ... String s =「CREATE TABLE」+ TABLE +「(_id INTEGER PRIMARY KEY AUTOINCREMENT,」+ VALUE1 + ...; android.util.Log.d(「mygrades」,s); – 2012-03-30 03:10:05

回答

1

從第一次運行應用程序開始,您是否修改過表架構?

如果你這樣做,那很可能是你的問題。

看着你給出的代碼,你仍然有DB版本設置爲1,所以這個表格還沒有被重新創建,你的新列添加(其中我會打賭會是「value1」)。

更改super(context, "grades.sqlite", null, 1);super(context, "grades.sqlite", null, 2);,再次運行您的應用程序,我敢打賭它會工作。

+0

讓我試試..... – 2012-03-30 13:40:58

+0

工作!感謝BARAK! – 2012-03-30 14:30:47

0

正如logcat的說:」 android.database.sqlite.SQLiteException:沒有這樣的列:數值「 ,我認爲這個問題是當你創建表,有一個空格該段之後丟失:

" (_id INTEGER PRIMARY KEY AUTOINCREMENT," 

,我認爲這應該是這樣的:

" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ". 

試試吧。

+0

它不起作用.. – 2012-03-30 03:02:15

+0

哦,你的CREATE語句中沒有「NAME」字段,你看到了嗎?你已經在你的選擇語句中使用過它了。 – zionpi 2012-03-30 03:08:50

+0

是啊我現在就修正了這個錯誤,錯誤仍然一樣 – 2012-03-30 03:14:41