Public class dbhelper extends sqliteopenhelper {/*** database name */Private Static final string db_name = "mydb. DB ";/*** database version */Private Static final int db_version = 1; Public dbhelper (context, string name, cursorfactory factory, int version) {super (context, name, factory, version);} public dbhelper (context) {This (context, db_name, null, db_version );} /*** create database */Public void oncreate (sqlitedatabase dB) {string SQL = "CREATE TABLE MERs (ID integer primary key autoincrement, name varchar (20), age INT) "; db.exe csql (SQL);}/*** used to upgrade */Public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stub }}
CRUD operation
Public class crud {dbhelper helper; Public CRUD (context CTX) {helper = new dbhelper (CTX) ;}/ *** insert operation */Public void insert (string name, int age) {string SQL = "insert into MERs (name, age) values (?,?) "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {name, age});}/*** update operation */Public void Update (string name, int age, int ID) {string SQL = "Update MERs set name =?, Age =? Where id =? "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {name, age, Id});}/*** delete operation */Public void Delete (INT ID) {string SQL = "delete from MERs where id =? "; Sqlitedatabase DB = helper. getwritabledatabase (); db.exe csql (SQL, new object [] {ID});}/*** query all customer information */public list <object []> findall () {list <object []> List = new arraylist <object []> (); object [] O = NULL; string SQL = "select * from MERs "; sqlitedatabase DB = helper. getwritabledatabase (); cursor cur = dB. rawquery (SQL, null); While (cur. movetonext () {o = new object [3]; O [0] = cur. getint (cur. getcolumnindex ("ID"); O [1] = cur. getstring (cur. getcolumnindex ("name"); O [2] = cur. getint (cur. getcolumnindex ("Age"); list. add (o);} cur. close (); return list ;}}
Mobile development QQ group: 59516399
Csdn download link: http://download.csdn.net/detail/wenwei19861106/4970365