android - Update SQL Database without using ROW_ID -
android - Update SQL Database without using ROW_ID -
how create update
function without having utilize row_id. reason including row_id in sql table giving me multiple errors. working fine without it. need how utilize parameter in update using constraint, suppose, registration number.
public class work { public static final string key_name = "name"; public static final string key_reg = "reg"; public static final string key_quiz1 = "q1"; public static final string key_quiz2 = "q2"; public static final string key_quiz3 = "q3"; public static final string key_cat1 = "c1"; public static final string key_cat2 = "c2"; public static final string key_tee = "t"; private dbhelper ourhelper; private final context ourcontext; private static sqlitedatabase ourdatabase; private static final string database_name = "data"; private static final string database_table = "stable"; private static final int database_version = 1; private static class dbhelper extends sqliteopenhelper{ public dbhelper(context context) { super(context, database_name, null, database_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql("create table " + database_table + " (" + key_reg + " text, " + key_name + " text, " + key_quiz1 + " text, " + key_quiz2 + " text, " + key_quiz3 + " text, " + key_cat1 + " text, " + key_cat2 + " text, " + key_tee + " text);" ); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop tbale if exists " + database_table); oncreate(db); } } public work(context c){ ourcontext = c; } public work open() throws sqlexception{ ourhelper = new dbhelper(ourcontext); ourdatabase = ourhelper.getwritabledatabase(); homecoming this; } public void close(){ ourhelper.close(); } public long createentry(string string, string string2, string string3, string string4, string string5, string string6, string string7, string string8) { // todo auto-generated method stub contentvalues cv = new contentvalues(); cv.put(key_reg, string); cv.put(key_name, string2); cv.put(key_quiz1, string3); cv.put(key_quiz2, string4); cv.put(key_quiz3, string5); cv.put(key_cat1, string6); cv.put(key_cat2, string7); cv.put(key_tee, string8); homecoming ourdatabase.insert(database_table, null, cv); } public string getq1(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_quiz1); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string getq2(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_quiz2); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string getq3(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_quiz3); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string getc1(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{ key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_cat1); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string getc2(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{ key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_cat2); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string gette(int j) { // todo auto-generated method stub int i=0; string[] columns = new string[]{key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int iq1=c.getcolumnindex(key_tee); for(c.movetofirst();i<j && !c.isafterlast();i++,c.movetonext()) {} res = c.getstring(iq1); homecoming res; } public string getdata() { // todo auto-generated method stub string[] columns = new string[]{key_reg, key_name, key_quiz1, key_quiz2, key_quiz3, key_cat1, key_cat2, key_tee}; cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null); string res = ""; int ireg=c.getcolumnindex(key_reg); int ina=c.getcolumnindex(key_name); int iq1=c.getcolumnindex(key_quiz1); int iq2=c.getcolumnindex(key_quiz2); int iq3=c.getcolumnindex(key_quiz3); int ic1=c.getcolumnindex(key_cat1); int ic2=c.getcolumnindex(key_cat2); int it=c.getcolumnindex(key_tee); for(c.movetofirst(); !c.isafterlast(); c.movetonext()) { res+=c.getstring(ireg)+" "+c.getstring(ina)+" "+c.getstring(iq1)+" "+c.getstring(iq2)+" "+c.getstring(iq3)+" "+c.getstring(ic1)+" "+c.getstring(ic2)+" "+c.getstring(it)+"\n"; } homecoming res; } public void updateentry(int j, string g1, string g2, string g3, string g4, string g5, string g6) { // todo auto-generated method stub contentvalues cvup= new contentvalues(); cvup.put(key_quiz1, g1); cvup.put(key_quiz2, g2); cvup.put(key_quiz3, g3); cvup.put(key_cat1, g4); cvup.put(key_cat2, g5); cvup.put(key_tee, g6); ourdatabase.update(database_table, cvup, where, null); }
}
where clause of update statement
ourdatabase.update(database_table, cvup, where, null);
giving sample update clause:
public boolean updatecontact (integer id, string name, string phone, string email) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put("name", name); contentvalues.put("phone", phone); contentvalues.put("email", email); db.update("contacts", contentvalues, "id = ? ", new string[] { integer.tostring(id) } ); homecoming true; }
see here.
update use
private static final string database_name = "data.db"; //you forgot .db
android mysql sql eclipse
Comments
Post a Comment