Android Sqlite: Check if row exists in table
Just do like
Cursor cursor = null;
String sql ="SELECT PID FROM "+TableName+" WHERE PID="+pidValue;
cursor= db.rawQuery(sql,null);
Log("Cursor Count : " + cursor.getCount());
if(cursor.getCount()>0){
//PID Found
}else{
//PID Not Found
}
cursor.close();
Android - SQlite check if value in row exists
Update you Insert Query
String name = edtName.getText().toString().trim();
String query = "Select * From STUDENTS where name = '"+name+"'";
if(sqLiteHelper.getData(query).getCount()>0){
Toast.makeText(getApplicationContext(), "Already Exist!", Toast.LENGTH_SHORT).show();
}else{
sqLiteHelper.insertData(
name,
edtPrice.getText().toString().trim(),
imageViewToByte(imageView)
);
Toast.makeText(getApplicationContext(), "Added successfully!", Toast.LENGTH_SHORT).show();
}
Check if Record Exists in database
From docs:
Return boolean, indicating whether the move succeeded.
if (c.moveToFirst()) {
...
} else {
// cursor is empty, print here
}
Obviously, if there are no records then move hasn't succeeded, you have to check it in else
block.
check if row already exist in sqlite database
Check below your updated method for ifExists(),
public boolean ifExists(Model model)
{
Cursor cursor = null;
String checkQuery = "SELECT " + KEY_NAME + " FROM " + TABLE_SHOP + " WHERE " + KEY_NAME + "= '"+model.getName() + "'";
cursor= db.rawQuery(checkQuery,null);
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
Two things, first is you were passing variable as value for column name and second thing is you were passing name value with space and without quote them so it will crash app.
Now try above code.
How to check Sqlite if record exist in c# android
Try this method. I'm not sure how your sqlite code works so im just guessing here. I am also guessing that you want to check if the newly created record exist, so this code is based off that.
conn.CreateTable<Record>();
//check if records exist
var items = conn.Table<Record>().Where(array => array.Jobno == photo.Jobno && array.Applicationletter == photo.Applicationletter && array.Signno == photo.Signno);
if (items?.Count() == 0)
{
var numberofrows = conn.Insert(record);
if (numberofrows > 0)
{
DisplayAlert("Success", "record has been saved successfully", "Ok");
MainImage.Source = " ";
}
else
{
DisplayAlert("Failure", "Error occoured while saving record", "Try again");
}
}
else
{
DisplayAlert("Failure", "Photo already exist", "ok");
}
//clear the notes field
notesentry.Text = "";
When add new record in SQLite check if this name already exists in Android
The simplest way would be to utilise UNIQUE thus making the column/composite columns which will result in a constraint conflict and thus not allow the row to be inserted.
What is not abundantly clear is what columns you are referring to to. The title appears to indicate that it is the name column, in which case you could change COl_NAME + " TEXT, " +
, to be COl_NAME + " TEXT UNIQUE, " +
.
The text how states I wan't to check if the search url and native url exists or not in the DB. This implies that it is only if a combination of both exist that you don't want to add the row, but that a combination where only one of the columns already exists should be added.
Assuming this then you could add a composite UNIQUE constraint in which case, you could add the line
", UNIQUE(" + COL_NATIVEURL + "," + COL_SEARCHURL + ")" +
This would be added after the line COL_SEARCHURL + " TEXT" +
If wanted the row to not be added if either existed then you could add UNIQUE to the column definitions by using :-
COL_NATIVEURL + " TEXT UNIQUE," +
COL_SEARCHURL + " TEXT UNIQUE" +
- Note that addBookMark would return -1 if the row wasn't added.
If the above does not suit and you want to check a column then you could base that check upon something like the following method in the BookmarkDB class:-
public boolean ifNativeUrlExists(String nativeUrl) {
boolean rv = false;
String whereclause = COL_NATIVEURL + "=?";
String[] whereargs = new String[]{nativeUrl};
Cursor csr = mDB.query(TBL_BOOKMARK,null,whereclause,whereargs,null,null, null)
if (csr.getCount() > 0) {
rv = true;
}
csr.close();
return rv;
}
You could utilise this using something like :-
public void saveData() {
Random r = new Random();
int low = 14;
int high = 100;
int result = r.nextInt(high-low) + low;
if (!bookmarkDB.ifNativeUrlExists(mUrl.getText().toString())) {
bookmarkDB.addBookmark(result, mName.getText().toString(), false, "", mUrl.getText().toString(), "http://" + mUrl.getText().toString());
} else {
..... code to indicate not added
}
}
- Obviously this could be adapted quite easily for other columns.
Note the above is in-principle code and has not been tested or run and mat therefore contain some errors.
Related Topics
How to Downgrade Android Emulator
Parse JSON Array Without Key in Android
How to Open an Android Studio Project in Eclipse
Led Flashlight on Galaxy Nexus Controllable by What API
How to Force the Action Bar to Be at the Bottom in Ics
Host-Based Card Emulation with Fixed Card Id
How to Get Access Token After User Is Signed in from Gmail in Android
Change Background Color of the Action Bar Using Appcompat
Error: Status{Statuscode=Developer_Error, Resolution=Null}
Trying to Create a File in Android: Open Failed: Erofs (Read-Only File System)
How to Set Time to Device Programmatically
Android: How to Select Texts from Webview
Google Play Services Out of Date. Requires 3159100 But Found 3158130
Geocoder.Getfromlocation Throws Ioexception on Android Emulator
Convert Magnetic Field X, Y, Z Values from Device into Global Reference Frame