Reputation: 151
I have created a database that stores data from the internet.When there is internet connection,i call createEntry and i write the data into the database. My problem is that the second time that the uses has internet,the new data doesnt overide the old,but the create new entries.So,i think that the solution will be to update my database from the second time that the user has internet connection,so everytime to have the same number of data.My problem is that i dont know how to do it.This is my first try on SQLite and databases.
This is my 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
}
}
and this is what i m calling when there is internet connection:
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();
Upvotes: 0
Views: 529
Reputation: 735
Try this function ::
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();
}
}
In your problem i noticed that you want to discard only duplicate records,
for this you do not need to update record or do not change your code function just set primary key to your table 'title' column - this will not allowed to insert duplicate records..(if you have other query tell me)
ref : Naming conventions
Upvotes: 3