This is a small SQLite program I have seen on the website. I think it is well written for reference, so I have reprinted it. It is actually a small favorites.
The Code is as follows:
ActivityMain_1.java
Public class ActivityMain_1 extends Activity {<br/> // declare the EditText instance <br/> private EditText et1, et2, et3; <br/> // declare the Button instance <br/> private Button button; </p> <p> @ Override <br/> protected void onCreate (Bundle savedInstanceState) {<br/> // TODO Auto-generated method stub <br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main_1); </p> <p> // obtain the instance object by ID <br/> et1 = (EditText) findViewById (R. id. editText01); <br/> et2 = (EditText) findViewById (R. id. editText02); <br/> et3 = (EditText) findViewById (R. id. editText03); <br/> button = (Button) findViewById (R. id. button); </p> <p> // event <br/> button. setOnClickListener (new View. onClickListener () {</p> <p> @ Override <br/> public void onClick (View v) {<br/> String name = et1.getText (). toString (); <br/> String url = et2.getText (). toString (); <br/> String desc = et3.getText (). toString (); </p> <p> ContentValues values = new ContentValues (); </p> <p> values. put ("name", name); <br/> values. put ("url", url); <br/> values. put ("desc", desc); </p> <p> // instantiate the Database Help class <br/> DBHelper helper = new DBHelper (getApplicationContext ()); <br/> // open the database <br/> helper. open (); </p> <p> // insert data <br/> helper. insert (values); <br/> // instantiate Intent <br/> Intent intent = new Intent (ActivityMain_1.this, QueryActivity. class); <br/> startActivity (intent); <br/> helper. close (); <br/>}< br/>}); </p> <p >}< br/>
QueryActivity. java
Public class QueryActivity extends ListActivity {</p> <p> private ListView listView; <br/> private Cursor c; </p> <p> @ Override <br/> protected void onCreate (Bundle savedInstanceState) {<br/> // TODO Auto-generated method stub <br/> super. onCreate (savedInstanceState); <br/> // setContentView (R. layout. row); </p> <p> // instantiate the Database help class <br/> final DBHelper help = new DBHelper (this); <br/> help. open (); <br/> c = help. query (); <Br/> query (); <br/> // dialog box <br/> final AlertDialog. builder builder = new AlertDialog. builder (this); <br/> listView. setOnItemClickListener (new OnItemClickListener () {<br/> @ Override <br/> public void onItemClick (AdapterView <?> Parent, View view, <br/> int position, long id) {<br/> final long temp = id; <br/> builder. setMessage ("are you sure you want to delete this record? "). SetPositiveButton ("yes", new DialogInterface. onClickListener () {</p> <p> @ Override <br/> public void onClick (DialogInterface dialog, int which) {<br/> System. out. println ("yes"); <br/> help. del (int) temp); <br/> // re-query <br/> // query (); <br/> Cursor c = help. query (); <br/> // list item array <br/> String [] from = {"_ id", "name", "url ", "desc" }; <br/> // list item ID <br/> int [] to = {R. id. text0, R. id. text1, R. id. text2, R. id. text3 }; <br/> // adapter <br/> SimpleCursorAdapter adapter = new SimpleCursorAdapter (getApplicationContext (), R. layout. row, c, from, to); <br/> ListView listView = getListView (); <br/> listView. setAdapter (adapter); </p> <p >}< br/> }). setNegativeButton ("no", new DialogInterface. onClickListener () {</p> <p> @ Override <br/> public void onClick (DialogInterface dialog, int which) {<br/> // TODO Auto-generated method stub </p> <p >}< br/>}); <br/> builder. create (); <br/> builder. show (); <br/>}< br/>}); </p> <p >}< br/> @ Override <br/> protected void onStop () {<br/> // TODO Auto-generated method stub <br/> super. onStop (); </p> <p >}< br/> public void query () {<br/> // list item array <br/> String [] from = {"_ id", "name", "url", "desc "}; <br/> // list item ID <br/> int [] to = {R. id. text0, R. id. text1, R. id. text2, R. id. text3 };</p> <p> // adapter <br/> SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R. layout. row, c, from, to); <br/> // adapter. notifyDataSetChanged (); <br/> // List View <br/> listView = getListView (); <br/> // Add an adapter to the List View <br/> listView. setAdapter (adapter); </p> <p >}< br/>
DBHelper. java
Public class DBHelper extends SQLiteOpenHelper {</p> <p> private static final String DB_NAME = "coll. db "; <br/> // table name <br/> private static final String TBL_NAME =" logtable "; <br/> // create an SQL statement <br/> private static final String CREATE_TBL = "create table logtable" + <br/> "(_ id integer primary key autoincrement, name text, url text, desc text) "; <br/> // SQLiteDatabase instance <br/> private SQLiteDatabase db; </p> <p> public D BHelper (Context context) {<br/> super (context, DB_NAME, null, 2 ); </p> <p >}< br/> @ Override <br/> public void onCreate (SQLiteDatabase db) {</p> <p> db.exe cSQL (CREATE_TBL ); <br/>}</p> <p> // open the database <br/> public void open () {<br/> db = getWritableDatabase (); <br/>}</p> <p> @ Override <br/> public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {<br/> // TODO Auto-generated method stub <br/>}</p> <p> // Insert method <br/> public void insert (ContentValues values) {</p> <p> db. insert (TBL_NAME, null, values); <br/>}</p> <p> // query method <br/> public Cursor query () {<br/> Cursor c = db. query (TBL_NAME, null, null); <br/> return c; <br/>}</p> <p> // deletion method <br/> public void del (int id) {<br/> db. delete (TBL_NAME, "_ id =? ", New String [] {String. valueOf (id)}); <br/>}</p> <p> // close the database <br/> public void close () {<br/> if (db! = Null) {<br/> db. close (); <br/>}< br/>
Main_1.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "vertical" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> </p> <TextView <br/> android: id = "@ + id/TextView01" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "website name" <br/> <EditText <br/> android: id = "@ + id/EditText01" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" </p> <p>/> <br/> <TextView <br/> android: id = "@ + id/TextView02" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "URL" <br/> <EditText <br/> android: id = "@ + id/EditText02" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> <TextView <br/> android: id = "@ + id/TextView03" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: text = "website description" <br/> <EditText <br/> android: id = "@ + id/EditText03" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "wrap_content" <br/> android: height = "100px" <br/> </p> <Button <br/> android: id = "@ + id/button" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: text = "add" <br/> </LinearLayout>
Custom Layout: row. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Br/> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" <br/> android: orientation = "horizontal" <br/> android: layout_width = "fill_parent" <br/> android: layout_height = "fill_parent" <br/> android: layout_gravity = "center_vertical" <br/> </p> <TextView <br/> android: id = "@ + id/text0" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: paddingRight = "10px" <br/> <TextView <br/> android: id = "@ + id/text1" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: paddingRight = "10px" <br/> <TextView <br/> android: id = "@ + id/text2" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: paddingRight = "10px" <br/> <TextView <br/> android: id = "@ + id/text3" <br/> android: layout_width = "wrap_content" <br/> android: layout_height = "wrap_content" <br/> android: paddingRight = "10px" <br/> </p> </LinearLayout>
Result: