我已经创建了一个数据库,用于存储来自有互联网连接的internet.When的数据,我调用createEntry并将数据写入数据库。我的问题是,第二次使用有互联网,新的数据没有覆盖旧的,但创建新的entries.So,我认为解决方案将是更新我的数据库从第二次,用户有互联网连接,所以每次有相同数量的data.My问题是,我不知道如何做it.This是我的第一次尝试在SQLite和数据库。
这是我的createEntry:
public void createEntry(String title, String getagonistiki, String getskor,
String getgipedo, String date, String getgoal1, String getgoal2,
String teliko_skor) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(DBHelper.TITLE, title);
cv.put(DBHelper.AGONISTIKI, getagonistiki);
cv.put(DBHelper.SKOR, getskor);
cv.put(DBHelper.GIPEDO, getgipedo);
cv.put(DBHelper.DATE, date);
cv.put(DBHelper.GOALA, getgoal1);
cv.put(DBHelper.GOALB, getgoal2);
cv.put(DBHelper.DESCRIPTION, teliko_skor);
try {
ourDatabase.insert("osfpDB", null, cv);
} // ourDatabase.update("osfpDB",cv,DBHelper.ROWID,null);
catch (Exception e) {
Log.e("DB ERROR ON .INSERT", e.toString()); // prints the error
// message to the log
e.printStackTrace(); // prints the stack trace to the log
}
}这就是当有互联网连接时我所说的:
HotOrNot entry = new HotOrNot(agones.this);
entry.open();
entry.createEntry(msg.getTitle(), msg.getagonistiki(), msg
.getskor(), msg.getgipedo(), msg.getDate(),msg.getgoal1(),msg.getgoal2(),msg.getDescription());
// entry.update(msg.getTitle(),msg.getagonistiki(),msg.getskor(),msg.getgipedo(),msg.getDate());
entry.close();发布于 2012-01-21 07:23:52
尝试此函数::
public void createEntry(String title, String getagonistiki, String getskor, String getgipedo, String date, String getgoal1, String getgoal2,
String teliko_skor) {
Cursor c = null;
boolean isInserted = false;
try {
c = ourDatabase.rawQuery("select "+DBHelper.TITLE+" from osfpDB", null);
if(c != null){
for(int i=0; i<c.getCount();i++){
c.moveToPosition(i);
if(c.getString(0).equals(title)){
Log.e("same title text means duplicate :",title+" : "+c.getString(0));
isInserted = true;
break;
}
}
}
ContentValues cv = new ContentValues();
cv.put(DBHelper.TITLE, title);
cv.put(DBHelper.AGONISTIKI, getagonistiki);
cv.put(DBHelper.SKOR, getskor);
cv.put(DBHelper.GIPEDO, getgipedo);
cv.put(DBHelper.DATE, date);
cv.put(DBHelper.GOALA, getgoal1);
cv.put(DBHelper.GOALB, getgoal2);
cv.put(DBHelper.DESCRIPTION, teliko_skor);
Log.e("ourDatabase", "" + ourDatabase);
if (ourDatabase == null) {
ourDatabase = getWritableDatabase();
}
if(isInserted){
ourDatabase.update("osfpDB", cv, null, null);
}else{
ourDatabase.insert("osfpDB", null, cv);
}
} catch (Exception e) {
Log.e("Exception in insert :", e.toString());
e.printStackTrace();
}
}在您的问题中,我注意到您只想丢弃重复的记录,
为此,您不需要更新记录或不更改您的代码函数,只需将主键设置为您的表'title‘列-这将不允许插入重复记录。(如果您有其他查询,请告诉我)
参考:Naming conventions
https://stackoverflow.com/questions/8948985
复制相似问题