650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/53/9E/wKioL1RsXxnz29SwAAC3SlphABw567.jpg "title=" Qq20141119171030.png "alt=" Wkiol1rsxxnz29swaac3slphabw567.jpg "/>
Interface file Activity_main.xml
<button android:id= "@+id/ Createbutton " android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:layout_below= "@id/hello_world" android:text = "Create database"/> <button android:id= "@+id/updatebutton" android:layout_width= "fill _parent " android:layout_height=" Wrap_content " android:layout_below= "@id/createbutton" android:text= "Update database"/> <button &Nbsp; android:id= "@+id/insertbutton" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_ Content " android:layout_below=" @id/updatebutton " android:text= "Insert database"/> <button android:id= "@+id/ Updaterecordbutton " android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:layout_below= "@id/insertbutton" android:text= "Update record"/> <button aNdroid:id= "@+id/queryrecordbutton" android:layout_width= "Fill_ Parent " android:layout_height=" Wrap_content " android:layout_below= "@id/updaterecordbutton" android:text= "Query record"/>
datab Asehelper.java
package com.example.sqlite_01;import android.content.context;import Android.database.sqlite.sqlitedatabase;import android.database.sqlite.sqlitedatabase.cursorfactory;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends Sqliteopenhelper{private static final int version = 1;public databasehelper (context context, string name, cursorfactory factory,int version) {super ( context, name, factory, version);} Public databasehelper (context context, string name, int version) { This (context, name, null, version);} Public databasehelper (Context context, string name) {this (context, name, VERSION);} The first time the database is created, it is actually called @overridepublic void oncreate (SQLITEDATABASE&NBSP;DB) when the first time the Sqlitedatabase object is obtained. {system.out.println ("Create a databASE ");d B.execsql (" Create table user (Id int, name varchar (20)) ");} @Overridepublic void onupgrade (sqlitedatabase db, int oldversion, int newversion) {system.out.println ("Update a database");} /* * abd shell Enter the databases directory under the current app directory to see the database created * open the database sqlite3 test_db * View the table in the current database and the statement that created the table .schema */}
maina Ctivity.java
package com.example.sqlite_01;import android.support.v7.app.actionbaractivity;import android.content.contentvalues;import android.database.cursor;import Android.database.sqlite.sqlitedatabase;import android.os.bundle;import android.view.menu;import android.view.menuitem;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;public class mainactivity extends actionbaractivity { private button createbutton = null;private button updatebutton = null; private button insertbutton = null;private button updaterecordbutton = null;private Button queryRecordButton = null; @Overrideprotected void OnCreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);createbutton = (Button) findviewbyid (r.id.creAtebutton);updatebutton = (Button) findviewbyid (R.id.updatebutton);insertbutton = ( button) findviewbyid (R.id.insertbutton);updaterecordbutton = (button) findviewbyid ( R.id.updaterecordbutton);queryrecordbutton = (Button) findviewbyid (R.id.queryrecordbutton); Createbutton.setonclicklistener (New createlistener ()); Updatebutton.setonclicklistener (new Updatelistener ()); Insertbutton.setonclicklistener (New insertlistener ()); Updaterecordbutton.setonclicklistener (New updaterecordlistener ()); Queryrecordbutton.setonclicklistener ( New querylistener ());} class createlistener implements onclicklistener{@Overridepublic void onclick (View &NBSP;V) {databasehelper dbhelper = new databasehelper (MainActivity.this, "Test_ DB "); Sqlitedatabase db = dbhelper.getreadabledatabase ();//execute this sentence before you create a database}}class updatelistener implements onclicklistener{@OveRridepublic void onclick (VIEW&NBSP;V) {DatabaseHelper dbHelper = new Databasehelper (mainactivity.this, "test_db", 2); Sqlitedatabase db = dbhelper.getreadabledatabase ();//execute this sentence before you create a database}}class insertlistener implements onclicklistener{@Overridepublic void onclick (view v) {contentvalues contentvalues = new contentvalues (); Contentvalues.put ("id", 1); ContentValues.put (" Name ", " Umgsai ");D Atabasehelper dbhelper = new databasehelper (MainActivity.this, "test_db", 2); Sqlitedatabase db = dbhelper.getwritabledatabase ();d b.insert ("User", null, contentvalues);}} class updaterecordlistener implements onclicklistener{@Overridepublic void onclick ( VIEW&NBSP;V) {databasehelper dbhelper = new databasehelper (MainActivity.this, " test_db ", 2); Sqlitedatabase db =&nBsp;dbhelper.getwritabledatabase (); Contentvalues values = new contentvalues (); Values.put ("name", "admin");d b.update (" User ", values, " ID&NBSP;=&NBSP;? ", new string[]{" 1 "});} class querylistener implements onclicklistener{@Overridepublic void onclick (View &NBSP;V) {databasehelper dbhelper = new databasehelper (MainActivity.this, "Test_ DB ", 2); Sqlitedatabase db = dbhelper.getreadabledatabase ();//db.query (table, columns, Selection, selectionargs, groupby, having, orderby) Cursor cursor = db.query ("User", new string[]{"id", "name"}, "ID&NBSP;=&NBSP;?", new string[]{"1"}, null, null, "id");while (Cursor.movetonext ()) {String name = Cursor.getstring (Cursor.getcolumnindex ("name")); System.out.println ("name>>>" + name);}}}
You can view the database through the ADB shell command
>ABD Shell
>ls-l (view files and folders in the current directory)
>CD data (enter data directory)
>ls-l (view files and folders in the current directory )
>CD data (enter data directory)
>ls-l (view files and folders in the current directory )
>CD com.example.sqlite_01 (enter current project directory)
>ls-l (view files and folders in the current directory )
>CD databases (enter the databases directory)
>ls-l (You can now see the database file test_db)
>sqlite3 test_db (using SQLite to open the test_db database )
>.schema (view the table in the current database and the statement that created the table )
>select * from user; (Querying data from the user table)
This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1579729
Use of Android Learning note-sqlite