java - comparing a input string to string in a database -
i'm studying android studio , started databases week. little app. simple. user enters string , presses button in first activity. string sent second activity compared values in database , display ones similar:
public class drinkcategoryactivity extends listactivity { public static final string extra_message = "message"; private sqlitedatabase db; private cursor cursor; private cursor newcursor; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); string messagetext = intent.getstringextra(extra_message); //setcontentview(r.layout.activity_drink_category); listview listdrinks = getlistview(); try{ sqliteopenhelper starbucksdatabasehelper = new starbucksdatabasehelper(this); db = starbucksdatabasehelper.getreadabledatabase(); /* select _id, name drinks; */ cursor = db.query("drink", new string[]{"_id", "name"}, null, null, null, null, null); newcursor = cursor.equals(messagetext); cursoradapter listadapter = new simplecursoradapter(this, android.r.layout.simple_list_item_1, cursor, new string[]{"name"}, new int[]{android.r.id.text1}, 0); listdrinks.setadapter(listadapter); }catch (sqliteexception ex){ toast toast = toast.maketext(this, "database unavailable", toast.length_long); toast.show(); }
i have been looking on web can not find explanation. database
this database drinks
public class starbucksdatabasehelper extends sqliteopenhelper{ add varaible db name private static final string db_name = "starbucks"; version of database private static final int db_version = 1; starbucksdatabasehelper (context context){ super(context, db_name, null, db_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table drink (" + "_id integer primary key autoincrement, " + "name text, " + "description text, " + "image_resource_id integer);"); insert data insertdrink(db, "soy latte", "esspresso , steamed soy milk", r.drawable.latte); insertdrink(db, "mocaccino", "esspresso, hot milk, steamed milk foam, , cocoa", r.drawable.cappuccino); insertdrink(db, "american", "drip coffee", r.drawable.filter); } private static void insertdrink (sqlitedatabase db, string name, string description, int resourceid){ contentvalues drinkvalues = new contentvalues(); drinkvalues.put("name", name ); drinkvalues.put("description", description); drinkvalues.put("image_resource_id", resourceid); db.insert("drink", null, drinkvalues); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { }
try query instead if want row containing messagetext
in start, middle or end should retrieved -
cursor = db.query("drink", new string[]{"_id", "name"}, "name %?%", new string[]{messagetext}, null, null, null);
Comments
Post a Comment