How to check whether Id already exist in the database or not in Android? -


my problem is, have created app need download documents, want compare value/id whether exist in database or not, if exist copy , replace existing one. have document id unique or primary key.

i read tutorials no benefit.

this code:

         intent = new intent();            i.putextra("item_name", item_name.folder_name);            i.putextra("item_url", item_url.url);            i.putextra("id", documentid.id);            string doc_id = i.getstringextra("id");            string name = i.getstringextra("item_name");            string url = i.getstringextra("item_url");            i.getstringextra("item_type");            database = new dms_database(files_folders_activity.this);            database.save_doc(doc_id, name, url);            database.close();           // check here if doc_id exist show toast,         // how perform function exist????      downloadmanager.request request = new downloadmanager.request(uri.parse(url));            request.settitle(name);            // in order if run, must use android 3.2 compile app             if (build.version.sdk_int >= build.version_codes.honeycomb)               {               request.allowscanningbymediascanner();               request.setnotificationvisibility(downloadmanager.request.visibility_visible_notify_completed);              }               request.setdestinationinexternalpublicdir(environment.directory_downloads + "/downloads", name);               downloadmanager manager = (downloadmanager) getsystemservice(context.download_service);               manager.enqueue(request);             }           }        }       });     }   

database:

public void save_doc(string id, string doc_name, string url) {      sqlitedatabase db = this.getwritabledatabase();   contentvalues values = new contentvalues();   values.put(document_id, id);   values.put(document_name, doc_name);   values.put(document_url, url);   db.insert(document_table, null, values); } public cursor get_doc(string rowid) {   sqlitedatabase db = this.getreadabledatabase();   //string[] columans = new string[] {document_id, document_name, document_url};   cursor c = db.rawquery("select document_id " + document_table + "where     document_id = ?", new string[]{string.valueof(rowid)});   return c; }   //getting contacts public list<folderlist> getalldoc() {    list<folderlist> doclist = new arraylist<folderlist>();    // select query    string selectquery = "select  * " + document_table;    sqlitedatabase db = this.getwritabledatabase();    cursor cursor = db.rawquery(selectquery, null);    // looping through rows , adding list    if (cursor.movetofirst()) {        {        folderlist document = new folderlist();        document.setid(cursor.getstring(0));   //     document.setname(cursor.getstring(1));   //     document.seturl(cursor.getstring(2));        // adding doc list        doclist.add(document);    } while (cursor.movetonext());    }    return doclist;      // return doc list   } public void delete_doc(string id)  {   sqlitedatabase db = this.getreadabledatabase();   db.delete(document_table, document_id + "=?", new string[]{string.valueof(id)}); } public void update_doc(string id, string name, string url) {   sqlitedatabase db = this.getwritabledatabase();   contentvalues updatenote = new contentvalues();   updatenote.put(document_id, id);   updatenote.put(document_name, name);   updatenote.put(document_url, url);   db.update(document_table, updatenote, document_id + "=?" , new string[]    {string.valueof(id)}); }   } 

you can implement function in database (ourdatabase).

public boolean dididexist(int id) {          string[] allcolumns = { document_id };         cursor cursor = ourdatabase.query(document_table, allcolumns,                 document_id + " = " + id, null, null, null,                 null);         cursor.movetofirst();         boolean retbool = false;         if (!cursor.isafterlast()) {              retbool = true;          }         return retbool;     } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -