(1) First create a class inheriting Sqliteopenhelper, overriding the Super (), OnCreate (), onUpgrade () method in the parent class,
In the super () method, declare the content (context) of the database, DatabaseName (the name of the data), factory (cursor Factory), version (versions);
A table can be created in the OnCreate () method;
The version of the database can be updated in the Onupgrade () method.
Public classPersondb extends Sqliteopenhelper { Publicpersondb (Context context) {Super (context,"person.db",NULL,5); } @Override Public voidonCreate (sqlitedatabase sqlitedatabase) {sqlitedatabase.execsql ("CREATE TABLE person (ID integer PRIMARY key autoincrement,name varchar (), number varchar )"); System. out. Print ("Database Creation succeeded"); } @Override Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intIintI1) {System. out. println ("Database Upgrade succeeded"); }}
(2) Create javabean for storing data
Role: Used to hold values in the database. (Note: The variables in this class should be the same as the fields in the database)
The Setxxx () and GetXXX () methods are set through variables in this class (the required methods can also be increased depending on the requirements);
Public classPerson {Private intID; PrivateString name; PrivateString number; PublicPerson () {} Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetNumber () {returnNumber ; } Public voidSetnumber (String number) { This. Number =Number ; } @Override PublicString toString () {return "person{"+"id="+ ID +", Name= '"+ name +'\ ''+", number= '"+ number +'\ ''+'}'; }}
(3) in the mainactivity can be added through the button to increase the monitoring of the database operation, delete, change, check (can be the results of the query through the ListView bar data display to the phone interface).
<1> Add the required controls in the XML file and find the controls in the XML in Mainactivity by Findviewbyid ()
b1= (Button) Findviewbyid (R.ID.B1);
b2=(Button) Findviewbyid (R.id.sele);
<2> declaring a database in mainactivity (this refers to a context)
helper=New persondb (this);
<3> Operational Database (Note: You should close the database after each operation of the database through the Close () method)
Add: Set Listener events for button
To get a read-write database from Sqlitedatabase
sqlitedatabase db=helper.getwritabledatabase ();
Writing SQL statements
Long i= db.insert (String table,string nullcolumnhack,contentvalues values);
Determine whether the operation succeeded by the return value of the SQL statement
if (i==-1) { toast.maketext (mainactivity). This " Insert Failed " , Toast.length_short). Show (); } Else { toast.maketext (mainactivity). This " Insert Successful " , Toast.length_short). Show (); }
}
Finally close the database
Db.close ();
Delete, change steps: Ibid.
Check: Set Listener events for button
Get a readable or writable database via Sqlitedatabase
Sqlitedatabase db=helper.getreadabledatabase (); Sqlitedatabase db=helper.getwritabledatabase ();
Writes the SQL statement, queries the data into the JavaBean, defines an internal class to inherit the Baseadapter adapter, and finally displays the results to the console via the ListView
B2.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Sqlitedatabase db=helper.getwritabledatabase (); Cursor Cursor= Db.query (" Person",NULL,NULL,NULL,NULL,NULL,NULL); List_infor=NewArraylist<person>(); while(Cursor.movetonext ()) {person infor=NewPerson (); intId=cursor.getint (Cursor.getcolumnindex ("ID")); String name=cursor.getstring (Cursor.getcolumnindex ("name")); String Number=cursor.getstring (Cursor.getcolumnindex (" Number")); Infor.setid (ID); Infor.setname (name); Infor.setnumber (number); List_infor.add (infor); } cursor.close (); Db.close (); Lv.setadapter (NewMyadapter ()); } }); } Private classMyadapter extends Baseadapter {@Override Public intGetCount () {returnlist_infor.size (); } @Override PublicObject GetItem (inti) {return NULL; } @Override Public LongGetitemid (inti) {return 0; } @Override PublicView GetView (intI, view view, ViewGroup ViewGroup) {TextView TV=NewTextView (mainactivity. This); Tv.settext (list_infor.Get(i). toString ()); returnTV; } }
Creation and use of SQLite database