SQLite数据库问题

时间:2013-05-11 17:44:55

标签: android sqlite

我正在尝试插入此数据库,我将收到错误。关于我做错了什么的任何想法?当我尝试加载此片段时,这是一个空的异常错误。

助手班级:

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
                                    KEY_ROWID + " INTEGER AUTOINCREMENT, " +
                                    KEY_CHARTING_DATE + " TEXT NOT NULL, " +
                                    KEY_NAME + " TEXT, " +
                                    KEY_CHARTING_TEMPERATURE + " INTEGER, " +
                                    KEY_CHARTING_STAMPS  + " INTEGER, " +
                                    KEY_CHARTING_FERTILE + " TEXT, " +
                                    KEY_CHARTING_NOTES + " TEXT, " +
                                    KEY_CHARTING_PROC + ", " +
                                    "PRIMARY KEY (" + KEY_ROWID + ", " + KEY_CHARTING_DATE + ") ); "
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE + ";");
        onCreate(db);
    }

public long createEntry(String date, String temperature, String fertile, String notes) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_CHARTING_DATE, date);
    cv.put(KEY_CHARTING_TEMPERATURE, temperature);
    cv.put(KEY_CHARTING_FERTILE, fertile);
    cv.put(KEY_CHARTING_NOTES, notes);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

另一个班级中的更新按钮:

@Override
public void onClick(View v) {
    sqlDate = (EditText) getView().findViewById(R.id.dateselected);
    sqlTemperature = (EditText) getView().findViewById(R.id.tempvalue);
    sqlFertile = (Switch) getView().findViewById(R.id.fertileswitch);
    sqlNotes = (EditText) getView().findViewById(R.id.chartingnote);

    switch (v.getId()) {
        case R.id.chartingupdate:

            boolean success = true;

            try{
            String date = sqlDate.getText().toString();
            String temperature = sqlTemperature.getText().toString();
            String fertile = sqlFertile.getText().toString();
            String notes = sqlNotes.getText().toString();

            SQLHelper entry = new SQLHelper(getActivity());

            entry.open();
            entry.createEntry(date, temperature, fertile, notes);
            entry.close();
            } catch (Exception e ) {
                success = false;

                String err_msg = e.toString();
                Dialog d = new Dialog(getActivity());
                d.setTitle("Error Inserting");
                TextView tv = new TextView(getActivity());
                tv.setText(err_msg);
                d.setContentView(tv);
                d.show();

            } finally {
                if (success) {
                    Context context = getActivity();
                    Toast.makeText(context, "Charting Successful", Toast.LENGTH_LONG).show();
                } else {
                    Context context = getActivity();
                    Toast.makeText(context, "Charting Unsuccessful", Toast.LENGTH_LONG).show();
                }
            }

            break;
        case R.id.pickdate:
            Context context2 = getActivity();
            Toast.makeText(context2, "Hello toast 2!", Toast.LENGTH_LONG).show();
            break;
    }

}

编辑:

public SQLHelper open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

错误说: 05-12 19:56:23.218:E / SQLiteLog(791):( 1)“AUTOINCREMENT”附近:语法错误

问题是我的表创建.... PK在两列上。一个是AUTOINCREMENT,另一个是日期字段。由于某种原因,它不喜欢它。如果我将PK更改为只有一个AUTOINCREMENT列它可以工作...猜猜我只需要将它设置为日期字段。

2 个答案:

答案 0 :(得分:1)

您需要将KEY_ROWID声明为主要

KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "

答案 1 :(得分:0)

您没有提供初始化ourDatabase的代码,但如果您没有正确执行,则必须是:

SQLiteDatabse ourDatabase = getWritableDatabase();

另请与调试器核实您发送到createEntry的所有字段均不为空(date, temperature, fertile, notes