Thursday, April 18, 2013

[android help] Before inserting a record in the database to validate that there is

android - Before inserting a record in the database to validate that there is - Stack Overflow








Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















Before inserting a record in the database to validate that there is, for example I have a table that has two fields, table fields VA with Customer, First, I want to validate the field if there is no customer registration and if there insert


That i want to do in android using Sqlite



























String sql = "SELECT * FROM TABLE WHERE id = '" + id + "'";
Cursor data = database.rawQuery(sql, null);


After this check by following code



if (cursor.moveToFirst()) {
// record exists
} else {
// record not found
}






















Look at SQLite constraints. This really isn't Android-specific; it's a feature of SQLite.






















Try this, it works for me.



String[] args = { myDataToCheck };
c = ourDatabase.query(DATABASE_TABLE, columns,
myColumnToCheck + "=?", args, null, null, null);
if (c.getCount() == 0) {
// doesn't exists
}else{
//exists
}





















You have 2 possibilities :


1)make a select to check if there is the same key in database, then insert or update.


2)make directly an update. update will return the number of row updated, if the number is 0, so you can do an insert.























lang-sql







.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...