创建一个默认值的表

时间:2014-01-22 22:05:31

标签: android sqlite

我想创建一个包含默认值的行的表,然后它将更新。

我无法显示图片,因为: “发布图片需要至少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)

1 个答案:

答案 0 :(得分:0)

我认为由于SQL语法错误而无法创建表。

具体来说,我认为您在ID_SETTING列定义中错过了“默认”。

ID_SETTING + " INTEGER 0,"应为ID_SETTING + " INTEGER DEFAULT 0,"

我看到的另一个问题是你的onUpgrade()只删除表 - 它不会重新创建它们。更新数据库后,只会调用 onUpgrade() - onCreate()将不再被调用。

虽然不应该调用onUpgrade(),但可能存在导致调用它的错误,从而丢弃了表。

我会尝试更正您的onUpgrade(),然后卸载并重新安装您的应用程序。