Andre,
Yes, "recentC != null" is wrong, because a cursor is returned if the
query itself succeeds, even if there is no data (for various reasons:
the cursor returns the schema of your query, also you can requery the
cursor later, and get new data if any is added).
Two ways to check existence are:
- Try to call moveNext() and check if it returns true;
- Call getCount() and check if it's greater than 0.
( and a third one is to execute "SELECT COUNT(*) FROM .... WHERE ..." )
-- Kostya
29.01.2011 17:41, André пишет:
This is doing my head in.
I have a database where there is currently one entry. What I want to
do is to check if this entry exists. If it does I want to update it,
if not create a new one.
recentC = db.fetchExisting(file);
if (recentC != null){
long rwId =
recentC.getLong(recentC.getColumnIndexOrThrow(SilverDbManager.RECENT_ROWID));
db.updateRecent(rwId, name, file, time);
} else {
db.createRecent(name, file, time);
}
public Cursor fetchExisting(String file) throws SQLException {
Cursor mCursor = null;
mCursor = mDb.query(true, RECENT_TABLE, new String[]
{RECENT_ROWID, RECENT_NAME, RECENT_FILE, RECENT_TIME}, RECENT_FILE + "
= ?", new String[]{file}, null, null, null, null);
return mCursor;
}
I know that recentC != null is probably wrong but I dont know what to
change it to. And its some problem with the query because it can't
find the entry I already have there when I search for it.
Any suggestions?
//André
--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en