我在程序中添加了一个数据库。虽然构建成功,但我的应用程序崩溃了。这是我的数据处理程序;

时间:2017-02-16 17:29:54

标签: sqlite

我收到的错误是:

Error Code : 1 (SQLITE_ERROR)
                  Caused By : SQL(query) error or missing database.
                    (near "EXISTSnotes": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTSnotes(idINTEGER PRIMARY KEY,titleTEXT,descriptionTEXT)). 

需要帮助。

public DatabaseHandler(Context context) {
    super(context, DatabaseValues.DATABASE_NAME, null, DatabaseValues.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { db.execSQL(DatabaseValues.TABLE_NOTES_CREATE); }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DatabaseValues.TABLE_NOTES_DROP);
    onCreate(db);
}
public void addNote(Note note) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues noteValues = new ContentValues();
    noteValues.put(DatabaseValues.NOTES_TITLE, note.getTitle());
    noteValues.put(DatabaseValues.NOTES_DESCRIPTION, note.getDescription());

    db.insert(DatabaseValues.TABLE_NOTES, null, noteValues);
    db.close();
}

public void updateNote(Note note) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues noteValues = new ContentValues();
    noteValues.put(DatabaseValues.NOTES_TITLE, note.getTitle());
    noteValues.put(DatabaseValues.NOTES_DESCRIPTION, note.getDescription());

    db.update(DatabaseValues.TABLE_NOTES, noteValues, DatabaseValues.NOTES_ID + "= ?",
            new String[]{String.valueOf(note.getId())});
    db.close();
}

public void deleteNote(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    String deleteQuery = "DELETE FROM" + DatabaseValues.TABLE_NOTES + "WHERE" + DatabaseValues.NOTES_ID + "= '" + "id" + "'";

    db.execSQL(deleteQuery);
    db.close();
}

public List<Note> getAllNotes() {
    List<Note> notes = new ArrayList<>();
    SQLiteDatabase db=this.getReadableDatabase();

    String selectQuery = "SELECT * FROM" + DatabaseValues.TABLE_NOTES;
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Note note = new Note();
            note.setId(Integer.parseInt(cursor.getString(0)));
            note.setTitle(cursor.getString(1));
            note.setDescription(cursor.getString(2));

            notes.add(note);
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return notes;
}

}

1 个答案:

答案 0 :(得分:1)

错误似乎很清楚,看看发生了什么:

编译时

:CREATE TABLE IF NOT NOT EXISTSnotes(idINTEGER PRIMARY KEY,titleTEXT,descriptionTEXT))。

您创建Db的查询似乎缺少“EXISTS”和表名“note”之间的空格。

<强> 更新

你实际上错过了更多的空白。像这样格式化您的查询,并确保您构建的所有其他查询具有正确的whitespacing,否则它们也将失败。

public static final String TABLE_NOTES_CREATE =“CREATE TABLE IF NOT NOT EXISTS”+ TABLE_NOTES +“(”+ NOTES_ID +“INTEGER PRIMARY KEY,”+ NOTES_TITLE +“TEXT,”+ NOTES_DESCRIPTION +“TEXT)”;