2013-01-24 89 views
0

如果我提出相同的評論(例如:「www.google.lv」),它會再次創建此評論。我想這種感覺不能被複制,而是被創造出最新的。我包括insertWithOnConflict,但它不工作。 有我的代碼:爲什麼SOlite更換不起作用?

public class SQLiteAdapter { 

public static final String MYDATABASE_NAME = "MY_DATABASE"; 
public static final String MYDATABASE_TABLE = "MY_TABLE"; 
public static final int MYDATABASE_VERSION = 1; 
public static final String KEY_ID = "_id"; 
public static final String KEY_CONTENT = "Content"; 

//create table MY_DATABASE (ID integer primary key, Content text not null); 
private static final String SCRIPT_CREATE_DATABASE = 
"create table " + MYDATABASE_TABLE + " (" 
+ KEY_ID + " integer primary key autoincrement, " 
+ KEY_CONTENT + " text not null);"; 

private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase; 

private Context context; 

public SQLiteAdapter(Context c){ 
context = c; 
} 

public SQLiteAdapter openToRead() throws android.database.SQLException { 
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
return this; 
} 

public SQLiteAdapter openToWrite() throws android.database.SQLException { 
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
return this; 
} 

public void close(){ 
sqLiteHelper.close(); 
} 

public long insert(String content){ 

ContentValues contentValues = new ContentValues(); 
contentValues.put(KEY_CONTENT, content); 
return sqLiteDatabase.insertWithOnConflict(MYDATABASE_TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE); 
} 

public int deleteAll(){ 
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
} 

public List<Comment> getAllComments() { 
    List<Comment> comments = new ArrayList<Comment>(); 
    String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, 
      columns, null, null, null, null, null); 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     Comment comment = cursorToComment(cursor); 
     comments.add(comment); 
     cursor.moveToNext(); 
    } 
    // Make sure to close the cursor 
    cursor.close(); 
    return comments; 

    } 
private Comment cursorToComment(Cursor cursor) { 
    Comment comment = new Comment(); 
    comment.setId(cursor.getLong(0)); 
    comment.setComment(cursor.getString(1)); 
    return comment; 
    } 
public List queueAll(){ 
String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
    null, null, null, null, null); 
cursor.moveToFirst(); 
    cursor.moveToNext(); 


return null; 
} 

public class SQLiteHelper extends SQLiteOpenHelper { 

public SQLiteHelper(Context context, String name, 
    CursorFactory factory, int version) { 
super(context, name, factory, version); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
// TODO Auto-generated method stub 
db.execSQL(SCRIPT_CREATE_DATABASE); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
// TODO Auto-generated method stub 

} 

} 

}

+0

[嘆氣]什麼不工作意味着什麼?我們需要猜測嗎? – Simon

回答

0

其實你濫用insertWithOnConflict。在您的方法中插入您不提供ContentValues的ID列,因此insertWithOnConflict只是添加了具有新ID和現有內容的另一個記錄。 insertWithOnConflict將有助於您提供表中已存在的id(您的主鍵)。 在你的情況下,你應該首先找到現有評論的ID,然後更新它或插入一個新的評論。

+0

如果我理解他的帖子是正確的,他希望最新的插入獲得新的_id;因此指定_id不會這樣做。他可能需要做的是在內容列上設置唯一索引並使用insertwithOnConflict,以便在重複衝突的情況下刪除前一行。但是,我並不熟悉SQLite的語法來顯示正確的示例。 – SylvainL

+0

據我瞭解,他需要避免評論重複,這就是爲什麼他試圖使用insertWithOnConflict。是的,你是對的,在評論領域uniqie索引應該做的伎倆。 – slkorolev