码迷,mamicode.com
首页 > 数据库 > 详细

数据库事务课上代码

时间:2016-06-05 23:26:17      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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.TestActivity2"
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="名称"/>
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="性别"/>
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="年龄"/>
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 
86 
87 
88 </LinearLayout>
.xml

 

 

技术分享
  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.os.Bundle;
  8 import android.support.v7.app.AppCompatActivity;
  9 import android.util.Log;
 10 import android.view.View;
 11 import android.widget.EditText;
 12 import android.widget.Toast;
 13 
 14 public class TestActivity2 extends AppCompatActivity {
 15 
 16     EditText et_id,et_name,et_sex,et_age;
 17 
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.activity_test2);
 22 
 23         et_id=(EditText)findViewById(R.id.et_id);
 24         et_name=(EditText)findViewById(R.id.et_name);
 25         et_sex=(EditText)findViewById(R.id.et_sex);
 26         et_age=(EditText)findViewById(R.id.et_age);
 27     }
 28 
 29     //初始化数据库
 30     public void bt1_OnClick(View v)
 31     {
 32         //使用工具类得到数据库对象
 33         MyDBHelper myDBHelper=new MyDBHelper("test.db",1);
 34 
 35         //得到连接
 36         SQLiteDatabase sd=myDBHelper.getWritableDatabase();
 37 
 38         Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 39 
 40         //关闭连接
 41         sd.close();
 42     }
 43 
 44     //升级数据库
 45     public void bt2_OnClick(View v)
 46     {
 47         //使用工具类得到数据库对象
 48         MyDBHelper myDBHelper=new MyDBHelper("test.db",2);
 49 
 50         //得到连接
 51         SQLiteDatabase sd=myDBHelper.getReadableDatabase();
 52 
 53         Toast.makeText(TestActivity2.this, "连接数据库成功", Toast.LENGTH_SHORT).show();
 54 
 55         //关闭连接
 56         sd.close();
 57     }
 58 
 59     //插入新数据
 60     public void bt3_OnClick(View v)
 61     {
 62         //1.连接数据库,得到数据库连接对象
 63 
 64         //得到连接
 65         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
 66 
 67         //2.准备数据
 68         ContentValues cv=new ContentValues();
 69         cv.put("name",et_name.getText().toString());
 70         cv.put("sex",et_sex.getText().toString());
 71         cv.put("age",et_age.getText().toString());
 72 
 73         //3.调用insert(),插入数据
 74         long l=sd.insert("t_user", null, cv);
 75 
 76         Toast.makeText(TestActivity2.this, "插入数据的主键="+l, Toast.LENGTH_SHORT).show();
 77 
 78         //4.关闭连接
 79         sd.close();
 80     }
 81 
 82     //数据查询
 83     public void bt4_OnClick(View v)
 84     {
 85         //1.连接数据库,得到数据库连接对象
 86 
 87         //得到连接
 88         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
 89 
 90         //2.全表全字段查询
 91         Cursor c=sd.query("t_user", null, null, null, null, null, null);
 92 
 93         //3.遍历结果集
 94         while (c.moveToNext())
 95         {
 96             //读取数据
 97             String str="_id="+c.getLong(c.getColumnIndex("_id"))+"name="+c.getString(1)
 98                     +"sex="+c.getString(2)+"age"+c.getString(3);
 99 
100             Log.e("TAG",str);
101         }
102 
103         Toast.makeText(TestActivity2.this, "结果集的记录条数"+c.getCount(), Toast.LENGTH_SHORT).show();
104 
105         c.close();
106 
107         //4.关闭连接
108         sd.close();
109     }
110 
111     //修改数据
112     public void bt6_OnClick(View v)
113     {
114         //1.连接数据库,得到数据库连接对象
115 
116         //得到连接
117         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
118 
119         //2.修改
120         ContentValues cv=new ContentValues();
121         cv.put("name",et_name.getText().toString());
122         cv.put("sex",et_sex.getText().toString());
123         cv.put("age", et_age.getText().toString());
124 
125         int i=sd.update("t_user",cv,"_id=?",new String[] {et_id.getText().toString()});
126 
127 
128         Toast.makeText(TestActivity2.this, "修改的记录条数"+i, Toast.LENGTH_SHORT).show();
129 
130 
131 
132         //4.关闭连接
133         sd.close();
134     }
135 
136     //数据库事务
137     public void bt8_OnClick(View v)
138     {
139         //1.连接数据库,得到数据库连接对象
140         //得到连接
141         SQLiteDatabase sd = new MyDBHelper("test.db", 2).getReadableDatabase();
142 
143         try {
144 
145             //启动事务
146             sd.beginTransaction();
147 
148             //2.修改
149             ContentValues cv = new ContentValues();
150             cv.put("age", "44");
151 
152             //修改1
153             int i = sd.update("t_user", cv, "_id=1", null);
154 
155             //抛出异常
156 //            if (i > 0) {
157 //                throw new RuntimeException("模拟事务异常");
158 //            }
159 
160             ContentValues cv2 = new ContentValues();
161             cv2.put("age", "54");
162 
163             //修改2
164             int k = sd.update("t_user", cv2, "_id=2", null);
165 
166             //设置事务是成功完成的
167             sd.setTransactionSuccessful();
168 
169             Toast.makeText(TestActivity2.this, "修改的记录条数" + (i + k), Toast.LENGTH_SHORT).show();
170 
171         }
172         catch (Exception e)
173         {
174             e.printStackTrace();
175 
176             Toast.makeText(TestActivity2.this, "修改出错", Toast.LENGTH_SHORT).show();
177         }
178         finally {
179 
180             //一定会被执行的代码
181             //结束事务
182             //1.没有异常,提交事务
183             //2.发生异常,回滚事务
184             sd.endTransaction();
185 
186             //4.关闭连接
187             sd.close();
188         }
189     }
190 
191     //删除数据
192     public void bt7_OnClick(View v)
193     {
194         //1.连接数据库,得到数据库连接对象
195 
196         //得到连接
197         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
198 
199         //2.删除
200         int i=sd.delete("t_user","_id=?",new String[] {et_id.getText().toString()});
201 
202         Toast.makeText(TestActivity2.this, "修改的记录条数"+i, Toast.LENGTH_SHORT).show();
203 
204 
205 
206         //4.关闭连接
207         sd.close();
208     }
209 
210 
211 
212     //带条件数据查询
213     public void bt5_OnClick(View v)
214     {
215         //1.连接数据库,得到数据库连接对象
216 
217         //得到连接
218         SQLiteDatabase sd=new MyDBHelper("test.db",2).getReadableDatabase();
219 
220         String strWhere="1=1";
221 
222         //select*from t_user where 1=1 and  _id=1 and name like ‘%张%‘ and sex=‘‘
223 
224         //得到条件
225         if (et_id.getText().length()>0)
226         {
227             strWhere+=" and _id="+et_id.getText().toString();
228         }
229 
230         if (et_name.getText().length()>0)
231         {
232             strWhere+=" and name like ‘%"+et_name.getText().toString()+"%‘";
233         }
234 
235         if (et_sex.getText().length()>0)
236         {
237             strWhere+=" and sex = ‘"+et_sex.getText().toString()+"‘";
238         }
239 
240 
241         if (et_age.getText().length()>0)
242         {
243             strWhere+=" and age = "+et_age.getText().toString()+"";
244         }
245 
246         //2.查询
247         Cursor c=sd.query("t_user", null, strWhere, null, null, null, null);
248 
249         //3.遍历结果集
250         while (c.moveToNext())
251         {
252             //读取数据
253             String str="_id="+c.getLong(c.getColumnIndex("_id"))+"name="+c.getString(1)
254                     +"sex="+c.getString(2)+"age"+c.getString(3);
255 
256             Log.e("TAG",str);
257         }
258 
259         Toast.makeText(TestActivity2.this, "结果集的记录条数"+c.getCount(), Toast.LENGTH_SHORT).show();
260 
261         c.close();
262 
263         //4.关闭连接
264         sd.close();
265     }
266 
267     //实现SQLiteOpenHelper的内部类
268     class MyDBHelper extends SQLiteOpenHelper
269     {
270         //构造方法
271         public MyDBHelper(String dbname,int ver)
272         {
273             //显示调用父类的构造方法
274             //必须在第一行
275             super(TestActivity2.this,dbname,null,ver);
276         }
277 
278         //创建初始化数据库
279         @Override
280         public void onCreate(SQLiteDatabase db) {
281 
282             //1.执行创建数据库的语句
283             String sql="\n" +
284                     "CREATE TABLE t_user" +
285                     " (_id  INTEGER NOT NULL," +
286                     "name  VARCHAR(20) NOT NULL," +
287                     "sex  CHAR(1),\n" +
288                     "age  INTEGER,\n" +
289                     "PRIMARY KEY (\"_id\"))";
290             db.execSQL(sql);
291 
292 
293             Log.e("TAG","表创建成功");
294 
295             //2.执行初始化数据的语句,insert语句
296             ContentValues cv=new ContentValues();
297 
298             cv.put("name", "张三");
299             cv.put("sex","男");
300             cv.put("age",20);
301 
302             //执行插入
303             long l=db.insert("t_user",null,cv);
304 
305             Log.e("TAG","初始化数据="+1);
306 
307         }
308 
309         //升级数据库
310         //触发条件:当版本号增大
311         @Override
312         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
313 
314             //修改数据
315             if (newVersion==2)
316             {
317                 ContentValues cv=new ContentValues();
318                 cv.put("name","李四");
319 
320                 String sql="update t_user set name=‘李四‘ where _id=1";
321 
322                 String[] str={"1","18"};
323 
324                 //调用db的更新方法
325                 int i=db.update("t_user",cv,"_id=? and age>?",str);
326 
327 
328                 Log.e("TAG","升级数据 数据条数="+i);
329             }
330         }
331     }
332 }
.java

 

数据库事务课上代码

标签:

原文地址:http://www.cnblogs.com/cycanfly/p/5562033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!