SQLite(二)高级操作

SQLite(二)适配器,数据库事务

 

(1)适配器

SQLite适配器 SimpleCursorAdapter

(注意!使用SimpleCursorAdapter适配器主键的名字一点要是_id,不然会报错

SimpleCursorAdapter作用是把cursor游标中保存的数据放到ListView控件中

使用ListView的时候,因为ListView里面有多个子布局,所以需要定义子布局。

子布局的代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6     
 7     <TextView
 8          android:id="@+id/text1"
 9          android:layout_width="0dp"
10          android:layout_height="match_parent"
11          android:layout_weight="1"
12          android:text="id"
13          android:gravity="center_horizontal"
14         />
15     
16     <TextView
17          android:id="@+id/text2"
18          android:layout_width="0dp"
19          android:layout_height="match_parent"
20          android:layout_weight="1"
21          android:text="age"
22          android:gravity="center_horizontal"
23         />
24     
25     <TextView
26          android:id="@+id/text3"
27          android:layout_width="0dp"
28          android:layout_height="match_parent"
29          android:layout_weight="1"
30          android:text="name"
31          android:gravity="center_horizontal"
32         />
33 
34 </LinearLayout>

from和to的意思是把数据库中的Constant.ID放到text1控件中,把Constant.AGE放到text2中,把Constant.NAME放到text3中,重复直到把cursor中的数据读取完,

有两种使用方法:

一:

 1 public void Show(SQLiteDatabase DB){
 2         cursor = operation.Select(null, null, null);
 3         /*SimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags);
 4          * Context context : 上下文对象
 5          * int layout : ListView里面有多个子布局,这个是子布局的id
 6          * Cursor cursor : 数据源
 7          * String[] from : 表示cursor中数据表字段的数组
 8          * int[] to : 表示将对应的字段的数据放到子布局对应的子控件中,数据里面是子控件的id
 9          * int flags : 设置适配器的标志
10          * 注意使用SimpleCursorAdapter主键名字必须是_id
11          */
12         SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.litem, cursor, 
13                 new String[]{Constant.ID,Constant.AGE,Constant.NAME}, new int[]{R.id.text1,R.id.text2,R.id.text3}, 
14                 SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
15         lv.setAdapter(adapter);
16         DB.close();
17     }

二:

1 public void Show2(SQLiteDatabase DB){
2         cursor = operation.Select(null, null, null);
3         MyCursorAdapter adapter = new MyCursorAdapter(MainActivity.this,cursor,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
4         lv.setAdapter(adapter);
5         DB.close();
6     }
 1 public class MyCursorAdapter extends CursorAdapter {
 2 
 3     public MyCursorAdapter(Context context, Cursor cursor, int flags) {
 4         super(context, cursor, flags);
 5     }
 6     @Override
 7     /*
 8      *ViewGroup : listView布局的id
 9      *返回listView的子控件
10      */
11     public View newView(Context context, Cursor cursor, ViewGroup arg2) {
12         View view = LayoutInflater.from(context).inflate(R.layout.litem, null);
13         return view;
14     }
15     @Override
16     /*
17      * view 是newView返回的
18      */
19     public void bindView(View view, Context context, Cursor cursor) {
20         TextView id = (TextView) view.findViewById(R.id.text1);
21         TextView age = (TextView) view.findViewById(R.id.text2);
22         TextView name = (TextView) view.findViewById(R.id.text3);
23         id.setText(cursor.getInt(cursor.getColumnIndex(Constant.ID))+"");
24         age.setText(cursor.getInt(cursor.getColumnIndex(Constant.AGE))+"");
25         name.setText(cursor.getString(cursor.getColumnIndex(Constant.NAME))+"");
26     }
27 }

两种方法效果都一样,这个两个是上一篇的Show()和Show2()方法。

(2)数据库事务

数据库事务Transaction指的是在数据库批量操作的时候使用的。

按照上诉方法,如果要插入100条数据,每一次都要打开关闭数据库,影响效率,如果使用了数据库事务,可以实现打开一次进行批量插入。批量删除更新也是,一单批量操作的任何一条操作有错误,都会出错不进行。

实现数据库事务一共需要三步,开始事务,提交事务,关闭事务。

示例代码:

 1 public class MainActivity extends Activity {
 2 
 3     private MySQLiteOpenHelper helper = null;
 4     private ListView lv = null;
 5     SimpleCursorAdapter adapter = null;
 6     int n =1;
 7     @SuppressLint("NewApi") @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.activity_main);
11         lv = (ListView)findViewById(R.id.lv);
12         if(helper == null){
13             helper = new MySQLiteOpenHelper(this);
14         }
15         SQLiteDatabase DB = helper.getWritableDatabase(); 
16         //1.数据库显示开启事务
17         DB.beginTransaction();
18         while(n<10){
19         ContentValues values = new ContentValues();
20         values.put("_id", n);
21         values.put("age", 1);
22         values.put("name", "wlj");
23         DB.insert("A", null, values);
24         n++;
25         }
26         //2.提交当前事务
27         DB.setTransactionSuccessful();
28         //3.关闭事务
29         DB.endTransaction();
30         Cursor cursor = DB.query("a", null, null, null, null, null, null);
31         SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list, cursor, 
32         new String[]{"_id","age","name"}, new int[]{R.id.text1,R.id.text2,R.id.text3}, 
33         SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
34         lv.setAdapter(adapter);
35         DB.close();
36     }
37 }

补充,要获得Cursor中的资源的方法:

1 while(cursor.moveToNext()){
2             String id = cursor.getString(cursor.getColumnIndex("_id"));
3             String age = cursor.getString(cursor.getColumnIndex("age"));
4             String name = cursor.getString(cursor.getColumnIndex("name"));
5             Log.i("tag",id+" "+age+" "+name);
6         }

 如果有什么错误,或者我理解错误或不当的,恳请大家纠正,谢谢!嘻嘻嘻~

posted on 2017-03-04 21:16  艹艹哒丶  阅读(192)  评论(0编辑  收藏  举报

导航