layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.TestActivity"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="初始化数据库"
17         android:onClick="bt1_onClick"/>
18     <Button
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:text="升级数据库"
22         android:onClick="bt2_onClick"/>
23     <LinearLayout
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content">
26         <EditText
27             android:layout_width="0dp"
28             android:layout_height="wrap_content"
29             android:layout_weight="1"
30             android:id="@+id/et_id"
31             android:hint="id"/>
32         <EditText
33             android:layout_width="0dp"
34             android:layout_height="wrap_content"
35             android:layout_weight="1"
36             android:id="@+id/et_name"
37             android:hint="name"/>
38     </LinearLayout>
39     <LinearLayout
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content">
42         <EditText
43             android:layout_width="0dp"
44             android:layout_height="wrap_content"
45             android:layout_weight="1"
46             android:id="@+id/et_sex"
47             android:hint="sex"/>
48         <EditText
49             android:layout_width="0dp"
50             android:layout_height="wrap_content"
51             android:layout_weight="1"
52             android:id="@+id/et_age"
53             android:hint="age"/>
54     </LinearLayout>
55     <Button
56         android:layout_width="match_parent"
57         android:layout_height="wrap_content"
58         android:text="新增数据"
59         android:onClick="bt3_onClick"/>
60     <Button
61         android:layout_width="match_parent"
62         android:layout_height="wrap_content"
63         android:text="查询数据"
64         android:onClick="bt4_onClick"/>
65     <Button
66         android:layout_width="match_parent"
67         android:layout_height="wrap_content"
68         android:text="带条件查询数据"
69         android:onClick="bt5_onClick"/>
70     <Button
71         android:layout_width="match_parent"
72         android:layout_height="wrap_content"
73         android:text="修改数据"
74         android:onClick="bt6_onClick"/>
75     <Button
76         android:layout_width="match_parent"
77         android:layout_height="wrap_content"
78         android:text="删除数据"
79         android:onClick="bt7_onClick"/>
80     <Button
81         android:layout_width="match_parent"
82         android:layout_height="wrap_content"
83         android:text="数据库事务"
84         android:onClick="bt8_onClick"/>
85 </LinearLayout>

java类:

  1 package com.hanqi.testapp3;
  2 
  3 import android.content.ContentValues;
  4 import android.database.Cursor;
  5 import android.database.sqlite.SQLiteDatabase;
  6 import android.database.sqlite.SQLiteOpenHelper;
  7 import android.support.v7.app.AppCompatActivity;
  8 import android.os.Bundle;
  9 import android.util.Log;
 10 import android.view.View;
 11 import android.widget.EditText;
 12 import android.widget.Toast;
 13 
 14 public class TestActivity extends AppCompatActivity {
 15 
 16     EditText et_id,et_name,et_sex,et_age;
 17     @Override
 18     protected void onCreate(Bundle savedInstanceState) {
 19         super.onCreate(savedInstanceState);
 20         setContentView(R.layout.activity_test);
 21         et_id = (EditText)findViewById(R.id.et_id);
 22         et_name = (EditText)findViewById(R.id.et_name);
 23         et_sex = (EditText)findViewById(R.id.et_sex);
 24         et_age = (EditText)findViewById(R.id.et_age);
 25     }
 26     //初始化数据库
 27     public void bt1_onClick(View v)
 28     {
 29         //使用工具类得到数据库对象
 30         MyDBHelper myDBHelper = new MyDBHelper("test.db",1);
 31         Toast.makeText(TestActivity.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 32         //得到连接
 33         SQLiteDatabase sqLiteDatabase = myDBHelper.getWritableDatabase();
 34         //关闭连接
 35         sqLiteDatabase.close();
 36     }
 37     //升级数据库
 38     public void bt2_onClick(View v)
 39     {
 40         //使用工具类得到数据库对象
 41         MyDBHelper myDBHelper = new MyDBHelper("test.db",2);
 42         Toast.makeText(TestActivity.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 43         //得到连接
 44         SQLiteDatabase sqLiteDatabase = myDBHelper.getWritableDatabase();
 45         //关闭连接
 46         sqLiteDatabase.close();
 47     }
 48     //插入新数据
 49     public void bt3_onClick(View v)
 50     {
 51         //1.连接数据库,得到数据库连接对象
 52         //得到连接
 53         SQLiteDatabase sd = new MyDBHelper("test.db",2).getWritableDatabase();
 54         //2.准备数据
 55         ContentValues cv = new ContentValues();
 56         cv.put("name",et_name.getText().toString());
 57         cv.put("sex",et_sex.getText().toString());
 58         cv.put("age", et_age.getText().toString());
 59         //3.调用insert(),插入数据
 60         long l = sd.insert("t_user", null, cv);
 61         Toast.makeText(TestActivity.this, "插入数据的主键 = "+l, Toast.LENGTH_SHORT).show();
 62         //4.关闭连接
 63         sd.close();
 64     }
 65     //数据查询
 66     public void bt4_onClick(View v)
 67     {
 68         //1.连接数据库,得到数据库连接对象
 69         //得到连接
 70         SQLiteDatabase sd = new MyDBHelper("test.db",2).getWritableDatabase();
 71         //2.全表全字段查询
 72         Cursor c = sd.query("t_user", null, null, null, null, null, null);
 73         //3.遍历结果集
 74         while (c.moveToNext())
 75         {
 76             //读取数据
 77             String str ="_id ="+c.getLong(c.getColumnIndex("_id"))+"name = "
 78                     +c.getString(1)+" sex = "+c.getString(2)+
 79                     " age = "+c.getInt(3);
 80             Log.e("TAG",str);
 81         }
 82         Toast.makeText(TestActivity.this, "结果集的记录条数 = "+c.getCount(), Toast.LENGTH_SHORT).show();
 83         c.close();
 84         //4.关闭连接
 85         sd.close();
 86     }
 87     //带条件数据查询
 88     public void bt5_onClick(View v)
 89     {
 90         //1.连接数据库,得到数据库连接对象
 91         //得到连接
 92         SQLiteDatabase sd = new MyDBHelper("test.db",2).getWritableDatabase();
 93         String strwhere = "1 = 1";
 94         //select * from t_user where _id = 1 and name like '%张%' and sex = ''
 95         //得到条件
 96         if (et_id.getText().length()>0)
 97         {
 98             strwhere += " and _id = "+et_id.getText().toString();
 99         }
100 
101         if (et_name.getText().length()>0)
102         {
103             strwhere += " and name like '%"+et_name.getText().toString()+"%'";
104         }
105         if (et_sex.getText().length()>0)
106         {
107             strwhere += " and sex = "+et_sex.getText().toString()+"'";
108         }
109         if (et_age.getText().length()>0)
110         {
111             strwhere += " and age = '"+et_age.getText().toString()+"'";
112         }
113         //2.查询
114         Cursor c = sd.query("t_user", null, strwhere, null, null, null, null);
115         //3.遍历结果集
116         while (c.moveToNext())
117         {
118             //读取数据
119             String str ="_id ="+c.getLong(c.getColumnIndex("_id"))+"name = "
120                     +c.getString(1)+" sex = "+c.getString(2)+
121                     " age = "+c.getInt(3);
122             Log.e("TAG",str);
123         }
124         Toast.makeText(TestActivity.this, "结果集的记录条数 = "+c.getCount(), Toast.LENGTH_SHORT).show();
125         c.close();
126         //4.关闭连接
127         sd.close();
128     }
129     //修改数据
130     public void bt6_onClick(View v)
131     {
132         //1.连接数据库,得到数据库连接对象
133         //得到连接
134         SQLiteDatabase sd = new MyDBHelper("test.db",2).getWritableDatabase();
135         //2.修改
136         ContentValues cv = new ContentValues();
137         cv.put("name",et_name.getText().toString());
138         cv.put("sex",et_sex.getText().toString());
139         cv.put("age", et_age.getText().toString());
140         int i = sd.update("t_user",cv,"_id = ?",new String[]{et_id.getText().toString()});
141         Toast.makeText(TestActivity.this, "修改的记录条数 = "+i, Toast.LENGTH_SHORT).show();
142         //4.关闭连接
143         sd.close();
144     }
145     //删除数据
146     public void bt7_onClick(View v)
147     {
148         //1.连接数据库,得到数据库连接对象
149         //得到连接
150         SQLiteDatabase sd = new MyDBHelper("test.db",2).getWritableDatabase();
151         //2.删除
152         int i = sd.delete("t_user", "_id = ?", new String[]{et_id.getText().toString()});
153         Toast.makeText(TestActivity.this, "修改的记录条数 = "+i, Toast.LENGTH_SHORT).show();
154         //4.关闭连接
155         sd.close();
156     }
157     //数据库事务
158     public void bt8_onClick(View v)
159     {
160 
161         //1.连接数据库,得到数据库连接对象
162         //得到连接
163         SQLiteDatabase sd = new MyDBHelper("test.db", 2).getWritableDatabase();
164         try {
165             //启动事务
166             sd.beginTransaction();
167             //2.修改
168             ContentValues cv1 = new ContentValues();
169             cv1.put("age", "43");
170             ContentValues cv2 = new ContentValues();
171             cv2.put("age", "53");
172             //修改1
173             int i = sd.update("t_user", cv1, "_id = 1", null);
174             //抛出异常
175             if (i > 0) {
176                 throw new RuntimeException("模拟事务异常");
177             }
178             //修改2
179             int k = sd.update("t_user", cv2, "_id = 2", null);
180             //设置事务是成功完成的
181             sd.setTransactionSuccessful();
182 
183             Toast.makeText(TestActivity.this, "修改的记录条数 = " + (i + k), Toast.LENGTH_SHORT).show();
184 
185         }
186         catch (Exception e)
187         {
188             e.printStackTrace();
189         }
190         finally {
191             //一定会被执行的代码
192             //结束事务
193             //1.没有异常,提交事务
194             //2.发生异常,回滚事务
195             sd.endTransaction();
196             //4.关闭连接
197             sd.close();
198         }
199     }
200     //实现SQLiteOpenHelper的内部类
201     class MyDBHelper extends SQLiteOpenHelper
202     {
203         //构造方法
204         public MyDBHelper(String dbname,int ver)
205         {
206             //显示调用父类的构造方法
207             //必须在第一行
208             super(TestActivity.this,dbname,null,ver);
209         }
210         //创建和初始化数据库
211         @Override
212         public void onCreate(SQLiteDatabase db) {
213             //1.执行创建数据库的语句
214             String sql = "CREATE TABLE t_user " +
215                     "(_id  INTEGER NOT NULL," +
216                     "name  VARCHAR(20) NOT NULL," +
217                     "sex  CHAR(1),age  INTEGER,\n" +
218                     "PRIMARY KEY (\"_id\"))";
219             db.execSQL(sql);
220             Log.e("TAG","表创建成功");
221             //2.执行初始化数据的语句,insert语句
222             ContentValues cv = new ContentValues();
223             cv.put("name","张三");
224             cv.put("sex","男");
225             cv.put("age",20);
226             //执行插入,返回主键
227             long l = db.insert("t_user",null,cv);
228             Log.e("TAG","初始化数据 = "+l);
229 
230         }
231         //升级数据库
232         //触发条件:当版本号增大
233         @Override
234         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
235             //修改数据
236             if (newVersion ==2)
237             {
238                 ContentValues cv = new ContentValues();
239                 cv.put("name","李四");
240                 String sql = "update t_user set name='李四'where _id=1";
241                 String[] str = {"1","18"};
242                 //调用db的更新方法
243                 int i = db.update("t_user",cv,"_id=? and age>?",str);
244                 Log.e("TAG","升级数据 数据条数 = "+i);
245             }
246         }
247     }
248 }