2014-01-22 177 views
1

我想用默認值創建一行表格,然後它會更新。使用默認值創建表格

我無法顯示圖像,這是因爲: 「你至少需要10聲譽發表圖片」,我很抱歉:

來自:

enter image description here

到:

enter image description here

public class DataBaseHelper extends SQLiteOpenHelper { 
. 
. 
. 

private static final String CREATE_TABLE_SETTING = "CREATE TABLE " + 
MYSETTING + "(" + ID_SETTING + " INTEGER 0," + 
     PIC_SETTING + " INTEGER DEFAULT 1," + WITH_PIC_SETTING + " INTEGER DEFAULT 7," + 
     WITHOUT_PIC_SETTING + " INTEGER DEFAULT 10" + ")"; 

//my update function 
public void updateSetting(int key,int pic , int number_with_pic , int number_without_pic){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(ID_SETTING, key); 
    values.put(PIC_SETTING, pic); 
    values.put(WITH_PIC_SETTING, number_with_pic); 
    values.put(WITHOUT_PIC_SETTING, number_without_pic); 
    db.update(MYSETTING, values, ID_SETTING + " = ? ", new String[] {String.valueOf(key)}); 
} 

public class Settings extends Fragment { 
DataBaseHelper db ; 
ArrayList<Integer> arraysetting ; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.frag_2,container,false); 
    db = new DataBaseHelper(getActivity()); 
    db.updateSetting(0, 4, 3, 2); 
    //arraysetting = new ArrayList<Integer>(); 
    //arraysetting.addAll(db.getAllSetting()); 

    return view; 
} 
} 

我的錯誤:

01-23 01:32:51.201: E/AndroidRuntime(632): FATAL EXCEPTION: main 

01-23 01:32:51.201: E/AndroidRuntime(632): android.database.sqlite.SQLiteException: no such table: mysetting: , while compiling: UPDATE mysetting SET pic_setting=?,without_setting=?,with_setting=?,id_setting=? WHERE id_setting = ? 

01-23 01:32:51.201: E/AndroidRuntime(632): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
+0

您的'DataBaseHelper'重寫'onCreate(SQLiteDatabase)'嗎? –

+0

yes,db.execSQL(CREATE_TABLE_SETTING); – user3103823

+0

您的logcat是否顯示與創建數據庫相關的任何錯誤?您發佈的錯誤表明該表不存在,表示創建時存在問題。 –

回答

0

我相信,該表不獲取創建由於SQL語法錯誤。

具體而言,我認爲您在ID_SETTING列定義中缺少「DEFAULT」。

ID_SETTING + " INTEGER 0,"應該ID_SETTING + " INTEGER DEFAULT 0,"

的其他問題,我看到的是,你只onUpgrade()下降不會重新創建的表 - 。當數據庫更新時,只有onUpgrade()被調用 - onCreate()不會再被調用。

雖然onUpgrade()不應該被調用,但可能有一個錯誤導致它被調用,從而刪除您的表。

我會嘗試更正您的onUpgrade(),然後卸載並重新安裝您的應用程序。

+0

我改變了它,但我得到相同的錯誤 – user3103823

+0

我已經更新了我的回答,並提供了一些關於'onUpgrade()'的建議。 –

+0

好的,但我沒有任何數據時調用getAllSetting()函數。 http://paste.ubuntu.com/6801496/我的錯誤:http://paste.ubuntu.com/6801535/ – user3103823