Activity
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.lenovo.myapplication.SQLActivity"android:orientation= "vertical"> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Bt_1"Android:text= "Initialize Database"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Bt_2"Android:text= "Upgrade Database"/> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"> <EditTextAndroid:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/et_id"Android:hint= "id"/> <EditTextAndroid:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/et_name"Android:hint= "Name"/> </LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"> <EditTextAndroid:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/et_sex"Android:hint= "Gender"/> <EditTextAndroid:layout_width= "0DP"Android:layout_height= "Match_parent"Android:layout_weight= "1"Android:id= "@+id/et_age"Android:hint= "Age"/> </LinearLayout> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Bt_3"Android:text= "New Data"/> <ButtonAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:onclick= "Bt_4"Android:text= "Query data"/></LinearLayout>
View Code
Java
Package Com.example.lenovo.myapplication;import Android.content.contentvalues;import Android.database.Cursor; Import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.edittext;import Android.widget.toast;public class SQLActivity extends appcompatactivity {EditText et_id,et_name,et_sex,et_age; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_sql); Et_id= (EditText) Findviewbyid (r.id.et_id); Et_name= (EditText) Findviewbyid (r.id.et_name); et_sex= (EditText) Findviewbyid (r.id.et_sex); Et_age= (EditText) Findviewbyid (r.id.et_age); }//Initialize database public void bt_1 (View v) {//Use tool class to get database connection object Mydbhelper mydbhelper=new mydbhelper ("Test.d B ", 1); Get Connected sqlitedAtabase sd= mydbhelper.getwritabledatabase (); Toast.maketext (sqlactivity.this, "database connection succeeded", Toast.length_short). Show (); Close connection sd.close (); }//Upgrade database public void Bt_2 (View v) {//Use tool class to get database connection object Mydbhelper mydbhelper=new mydbhelper ("test.db ", 2); Get Connected Sqlitedatabase sd= mydbhelper.getwritabledatabase (); Toast.maketext (sqlactivity.this, "database connection succeeded", Toast.length_short). Show (); Close connection sd.close (); }//Insert new data public void Bt_3 (View v) {//1-Connect to database, get database Connection object//Get connected sqlitedatabase sd= new Mydbhelper ("Test.db", 2). Getwritabledatabase (); 2-Prepare Data contentvalues cv=new contentvalues (); Cv.put ("Name", Et_name.gettext (). toString ()); Cv.put ("Sex", Et_sex.gettext (). toString ()); Cv.put ("Age", Et_age.gettext (). toString ()); 3-Call Insret (); Insert data long L=sd.insert ("T_user", NULL, CV); Toast.maketext (Sqlactivity.this, "PRIMARY key for inserting data =" +l, toast.length_short). Show (); 4-Close the connection. Sd.close (); }//Data query public void Bt_4 (View v) {//1-Connect to database, get database Connection object//Get Connected sqlitedatabase sd= new MYDBHE Lper ("Test.db", 2). Getwritabledatabase (); 2-full-table full-field queries Cursor c= sd.query ("T_user", NULL, NULL, NULL, NULL, NULL, NULL); 3-traverse the result set. cursor. while (C.movetonext ()) {//If the move succeeds, read data String str= "id=" +c.getlong (C.getcolumnindex ("_id") + "last Name Name = "+c.getstring (1) +" gender = "+c.getstring (2) +" age = "+c.getstring (3); LOG.E ("TAG", str); } c.close (); 4-Close the connection. Sd.close (); }//Implement Sqliteopenhelper inner class Mydbhelper extends Sqliteopenhelper {public Mydbhelper (String dbname,int ver) {//Shows the constructor method that calls the parent class//must be in the first row super (Sqlactivity.this,dbname,null,ver); }//Create initialization database @Override public void onCreate (Sqlitedatabase db) {//1-EXECUTE statement to create database String sql= "CREATE TABLE T_user ("+" _id INTEGER PRIMARY KEY autoincrement not NULL, "+" name Varcha R () not NULL, "+" Sex CHAR9 (1), age INTEGER) "; Db.execsql (SQL);//Build Table LOG.E ("TAG", "Table creation succeeded"); 2-Execute the initialization data statement, add the statement insert Contentvalues contentvalues=new contentvalues (); Contentvalues.put ("name", "Zhang San"); Contentvalues.put ("Sex", "male"); Contentvalues.put ("Age", "20"); 3-Perform INSERT, return a primary key long L=db.insert ("T_user", null,contentvalues); LOG.E ("TAG", "Initialize data =" +l); }//Upgrade database//Trigger condition: When version number is increased @Override public void Onupgrade (sqlitedatabase db, int oldversion, int NewVersion) {//Modify data if (newversion==2) {Contentvalues contentvalues = n EW contentvalues (); Contentvalues.put ("name", "John Doe"); String sql= "Update t_user set name = ' John Doe ' where _id = 1"; String [] Strings ={"1", "18"}; And? One by one corresponding, using an array to pass in//Call the Update method of the DB int i =db.update ("T_user", Contentvalues, "_id=?") and Age>? ", strings); LOG.E ("TAG", "Upgrade data data Bar number =" +i); } } }}View Code
Database connected to Android