Java 编辑数据库SQLiteDatabase中的内容

Java 编辑数据库SQLiteDatabase中的内容,java,android,sql,Java,Android,Sql,我用SQLiteDatabase创建了一个数据库,一切正常,除了我想更改某些列的内容。。我正在使用db.update,但它似乎不起作用。有人能帮我理解我做错了什么?谢谢大家 public class Note_DBHelper extends SQLiteOpenHelper { private Context ctx; //version of database private static final int version = 1; //database name private sta

我用SQLiteDatabase创建了一个数据库,一切正常,除了我想更改某些列的内容。。我正在使用db.update,但它似乎不起作用。有人能帮我理解我做错了什么?谢谢大家

public class Note_DBHelper extends SQLiteOpenHelper {

private Context ctx;
//version of database
private static final int version = 1;
//database name
private static final String DB_NAME = "notesDB";
//name of table
private static final String TABLE_NAME = "notes";
//column names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "noteTitle";
private static final String KEY_CONTENT = "noteContent";
private static final String KEY_TESTO = "noteTesto";
private static final String KEY_TXT_COLORE = "noteColore";
private static final String KEY_DATE = "date";
//sql query to creating table in database
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_TITLE+" TEXT NOT NULL, "+KEY_CONTENT+" TEXT NOT NULL,"+KEY_TESTO+" TEXT NOT NULL, "+KEY_TXT_COLORE+" TEXT NOT NULL, "+KEY_DATE+" TEXT);";

//contructor of Note_DBHelper
public Note_DBHelper(Context context) {
    super(context, DB_NAME, null, version);
    this.ctx = context;
}

//creating the table in database
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}


//in case of upgrade we're dropping the old table, and create the new one
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);

    onCreate(db);

}

//function for adding the note to database
public void addNote(String title, String content,String testo, String txt_colore) {
    SQLiteDatabase db = this.getWritableDatabase();

    //creating the contentValues object
    //read more here -> http://developer.android.com/reference/android/content/ContentValues.html
    ContentValues cv = new ContentValues();
    cv.put("noteTitle", title);
    cv.put("noteContent", content);
    cv.put("noteTesto", testo);
    cv.put("noteColore", txt_colore);
    cv.put("date", new Date().toString());

    //inserting the note to database
    db.insert(TABLE_NAME, null, cv);

    //closing the database connection
    db.close();

    //see that all database connection stuff is inside this method
    //so we don't need to open and close db connection outside this class

}


//getting all notes
public Cursor getNotes(SQLiteDatabase db) {
    //db.query is like normal sql query
    //cursor contains all notes 
    Cursor c = db.query(TABLE_NAME, new String[] {KEY_TITLE, KEY_CONTENT,KEY_TESTO,KEY_TXT_COLORE}, null, null, null, null, "id DESC");
    //moving to the first note
    c.moveToFirst();
    //and returning Cursor object
    return c;
}

public Cursor getNotes2(SQLiteDatabase db) {
    //db.query is like normal sql query
    //cursor contains all notes 
    Cursor c = db.query(TABLE_NAME, new String[] {KEY_ID, KEY_TITLE}, null, null, null, null, "id DESC");
    //moving to the first note
    c.moveToFirst();
    //and returning Cursor object
    return c;
}

public Cursor getNote(SQLiteDatabase db, int id) {      
    Cursor c = db.query(TABLE_NAME, new String[] {KEY_TITLE, KEY_CONTENT,KEY_TESTO, KEY_TXT_COLORE, KEY_DATE}, KEY_ID + " = ?", new String[] { String.valueOf(id) }, null, null, null);
    c.moveToFirst();
    return c;
}

public void removeNote(int id) {
    SQLiteDatabase db = getWritableDatabase();
    db.delete(TABLE_NAME, KEY_ID + " = ?", new String[] { String.valueOf(id) });
    db.close();
}

public void updateNote(String title, String content,String testo, String txt_colore, String editTitle) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues cv = new ContentValues();
    cv.put("noteTitle", title);
    cv.put("noteContent", content);
    cv.put("noteTesto", testo);
    cv.put("noteColore", txt_colore);
    cv.put("date", new Date().toString());

    db.update(TABLE_NAME, cv, KEY_TITLE + " LIKE '" +  editTitle +  "'", null);

    db.close();


}
用这个

db.update(TABLE_NAME, cv, KEY_ID + "=" + id, null);
然后在
createNote
类中

dbhelper.updateNote(title, content, id);

最好用他的id而不是他的头衔来更新便条。与其使用LIKe,不如使用try with=您能指导我更改此信息吗?理想情况下,这是:
db.update(TABLE_NAME,cv,KEY_TITLE+“LIKe'”+editTitle+“”,null)应该是
db.update(表名称,cv,键标题+“LIKE'?'”,新字符串[]{editTitle})以防止SQL注入,但我不确定这是否也是导致代码出现问题的原因xdeadc0de给我这个错误太多绑定参数。提供了6个参数,但该语句需要5个参数。@Chol感谢您的评论!with=不编辑dp。。。。