package mutou.test;import android.database.sqlite.*;import android.content.Context;import android.database.sqlite.SQLiteDatabase.CursorFactory;public class MySQLiteHelper extends SQLiteOpenHelper {public MySQLiteHelper(Context context, String name, CursorFactory factory, int version){super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db){db.execSQL("create table if not exists hero_info(" + "id integer primary key," + "name varchar," + "level integer)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}
Package mutou. test; import android. app. activity; import android. content. contentvalues; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. OS. bundle; import android. widget. textview; public class sample43activity extends activity {mysqlitehelper myhelper; textview TV;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinst Ancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); TV = (textview) This. findviewbyid (R. id. TV); myhelper = new mysqlitehelper (this, "My. DB ", null, 1); insertandupdatedata (myhelper); string result = querydata (myhelper); TV. settext ("Name \ t registration \ n" + result);} public void insertandupdatedata (mysqlitehelper myhelper) {sqlitedatabase DB = myhelper. getwritabledatabase (); db.exe CS QL ("insert into hero_info (name, level) values ('muu', 1)"); contentvalues values = new contentvalues (); values. put ("name", "Music"); values. put ("level", 2); dB. insert ("hero_info", "ID", values); values. clear (); values. put ("name", "musicvs"); values. put ("level", 3); dB. update ("hero_info", values, "level = 2", null);} Public String querydata (mysqlitehelper myhelper) {string result = ""; sqlitedat Abase DB = myhelper. getreadabledatabase (); cursor = dB. query ("hero_info", null, "id ASC"); int nameindex = cursor. getcolumnindex ("name"); int levelindex = cursor. getcolumnindex ("level"); For (cursor. movetofirst (); cursor. isafterlast ()! = True; cursor. movetonext () {result + = cursor. getstring (nameindex) + ""; Result + = cursor. getstring (levelindex) + "\ n";} cursor. close (); dB. close (); return result ;}@ overrideprotected void ondestroy () {sqlitedatabase DB = myhelper. getwritabledatabase ();/* Get database object * // dB. delete ("hero_info", "1", null); super. ondestroy ();}}