0

I want to know the Query to check whether the particular Record in the DataBase already exists or not for my application.

I have made one function for these, but it is not working properly.

public boolean ifExisting(String name) {
    Cursor c = db.rawQuery("SELECT * FROM " + TABLE_NAME + "WHERE" + "name" + "=" + name, null);
    if(c.getCount() == 0)
     return false; 
else
     return true;
}

This is the place where I need to perform the function of checking for the Duplication Process.

if(dh.ifExisting(dataVector.get(position)) == true) {

} else {
dh.insert(dataVector.get(position));
} 

Can anybody please help me.

Thanks, david

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
David Brown
  • 4,783
  • 17
  • 50
  • 75
  • 1
    Stop spamming the same question! Triple post: http://stackoverflow.com/questions/4427875/query-to-check-the-existing-record-in-the-sqlite-database-in-android http://stackoverflow.com/questions/4427660/check-the-duplication-of-records-in-the-database-by-select-query – WarrenFaith Dec 13 '10 at 10:30
  • please don't ask the same question over and over again. You can edit your question to update it, you can comment on answers to get more information, and you can offer a bounty if you cannot find a suitable answer. –  Dec 13 '10 at 12:40

3 Answers3

1

Can you be a bit more specific? "no working properly" isn't a real problem description.

Anyway, your query is missing some spaces:

"SELECT * FROM " + TABLE_NAME + "WHERE" + "name" + "=" + name
// if you would print the string you would get this:
"SELECT * FROM TABLE_NAMEWHEREname=name"
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • realtime refreshing would be helpful, your advice seems to be more correct then mine :) – Thrawn80 Dec 13 '10 at 09:12
  • there is a "nearly realtime" refresh, but nevertheless both of us were right :) – WarrenFaith Dec 13 '10 at 09:47
  • 1
    The quotes around `name` are also missing: "SELECT * FROM " + TABLE_NAME + " WHERE " + "name" + "= '" + name + "'" – ccheneson Dec 13 '10 at 10:32
  • and the fact that the table name is a variable, but the column name not :) there are a lot of issues there... he could have used the parameter, too – WarrenFaith Dec 13 '10 at 10:35
0

First question: your data.Vector.get(position) returns a string as result? Second: if your rawquery doesn't works, then try:

" where " + name + " like '"
                    + name + "'", null);

Maybe this might help you

Thrawn80
  • 351
  • 2
  • 7
0
Cursor c = db.rawQuery("SELECT * FROM " + TABLE_NAME + "WHERE" + "name" + "=" + name, null); 


>TABLE_NAME + "WHERE" + "name"

will be "TablenameWHEREname" instead of "Tablename WHERE name".

metter
  • 1,434
  • 11
  • 22