1. SQLiteTest工程(第一个工程)
1.1 DataBaseOpenHelp:
1 import android.content.Context;
2 import android.database.sqlite.SQLiteDatabase;
3 import android.database.sqlite.SQLiteOpenHelper;
4
5 public class DataBaseOpenHelp extends SQLiteOpenHelper
6 {
7 // 数据库的名字
8 private static final String SQLITE_NAME = "itcast";
9 // 数据库版本
10 private static final int version = 1;
11
12 public DataBaseOpenHelp(Context context)
13 {
14 super(context, SQLITE_NAME, null, version);
15
16 }
17
18 @Override
19 public void onCreate(SQLiteDatabase db)
20 {
21 db.execSQL("CREATE TABLE person (personid integer primary key autoincrement,name varchar(20),age INTEGER)");
22 }
23
24 @Override
25 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
26 {
27 db.execSQL("DROP TABLE IF EXISTS person");
28 onCreate(db);
29 }
30
31 }
1.2 Person:
1 package com.vanceinfo.domian;
2 // "person" bean类
3 public class Person
4 {
5 private String name;
6
7 private Integer personid;
8
9 private short age;
10
11 public Person(Integer personid, String name, short age)
12 {
13 this.name = name;
14 this.personid = personid;
15 this.age = age;
16 }
17
18 public Person(String name, short age)
19 {
20 this.name = name;
21 this.age = age;
22 }
23
24 public String getName()
25 {
26 return name;
27 }
28
29 public void setName(String name)
30 {
31 this.name = name;
32 }
33
34 public Integer getPersonid()
35 {
36 return personid;
37 }
38
39 public void setPersonid(Integer personid)
40 {
41 this.personid = personid;
42 }
43
44 public short getAge()
45 {
46 return age;
47 }
48
49 public void setAge(short age)
50 {
51 this.age = age;
52 }
53 }
1.3 PersonProvider:
1 package com.vanceinfo.provider;
2
3 import com.vanceinfo.service.DataBaseOpenHelp;
4
5 import android.content.ContentProvider;
6 import android.content.ContentUris;
7 import android.content.ContentValues;
8 import android.content.UriMatcher;
9 import android.database.Cursor;
10 import android.database.sqlite.SQLiteDatabase;
11 import android.net.Uri;
12 import android.text.TextUtils;
13
14 public class PersonProvider extends ContentProvider
15 {
16 private DataBaseOpenHelp dataBaseOpenHelp;
17
18 private final static int PERSON = 1;
19
20 private final static int PERSONS = 2;
21
22 private final static String AUTHORITY = "com.vanceinfo.personprovider";
23
24 private static final UriMatcher pMatcher = new UriMatcher(UriMatcher.NO_MATCH);
25 static
26 {
27 pMatcher.addURI(AUTHORITY, "person", PERSONS);
28 pMatcher.addURI(AUTHORITY, "person/#", PERSON);
29 }
30
31 // ok
32 @Override
33 public int delete(Uri uri, String selection, String[] selectionArgs)
34 {
35 SQLiteDatabase db = dataBaseOpenHelp.getWritableDatabase();
36 int count = 0;
37 switch (pMatcher.match(uri))
38 {
39 case PERSONS:
40 count = db.delete("person", selection, selectionArgs);
41 break;
42 case PERSON:
43 long sid = ContentUris.parseId(uri);
// 下面一句话的意思是分为两种情况查询,例如:
// 1. 删除 id 为 1 的记录的条件:"person_id = 1"
// 2. 除了删除 id 为 1 的记录,还有其他的条件,如姓名为"zhangshang"的条件:"name like zhangshang and person_id = 1"
44 String where = TextUtils.isEmpty(selection) ? "personid=?" : selection + "and personid=?";
45 String[] params = new String[] {String.valueOf(sid)};
46 if (!TextUtils.isEmpty(selection) && selectionArgs != null)
47 {
48 params = new String[selectionArgs.length + 1];
49 for (int i = 0; i < selectionArgs.length; i++)
50 {
51 params[i] = selectionArgs[i];
52 }
53 params[selectionArgs.length] = String.valueOf(sid);
54 }
55 count = db.delete("person", where, params);
56 break;
57 default:
58 throw new IllegalArgumentException("Unknow Uri:" + uri);
59 }
60 return count;
61 }
62
63 // ok
64 @Override
65 public String getType(Uri uri)
66 {
67 switch (pMatcher.match(uri))
68 {
69 case PERSONS:
70 return "vnd.android.cursor.dir/personprovider.person";
71 case PERSON:
72 return "vnd.android.cursor.item/personprovider.person";
73 default:
74 throw new IllegalArgumentException("Unknow Uri:" + uri);
75 }
76 }
77
78 // ok
79 @Override
80 public Uri insert(Uri uri, ContentValues values)
81 {
82 SQLiteDatabase db = dataBaseOpenHelp.getWritableDatabase();
83 long pid = 0;
84 switch (pMatcher.match(uri))
85 {
86 case PERSONS:
87 pid = db.insert("person", "name", values);
88 return ContentUris.withAppendedId(uri, pid);
89 case PERSON:
90 pid = db.insert("person", "name", values);
91 String path = uri.toString();
92 return Uri.parse(path.substring(0, path.lastIndexOf('/') + 1) + pid);
93 default:
94 throw new IllegalArgumentException("Unknow Uri:" + uri);
95 }
96 }
97
98 // ok
99 @Override
100 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
101 {
102 SQLiteDatabase db = dataBaseOpenHelp.getWritableDatabase();
103 int count = 0;
104 switch (pMatcher.match(uri))
105 {
106 case PERSONS:
107 count = db.update("person", values, selection, selectionArgs);
108 break;
109 case PERSON:
110 long sid = ContentUris.parseId(uri);
111 String where = TextUtils.isEmpty(selection) ? "personid=?" : selection + "and personid=?";
112 String[] params = new String[] {String.valueOf(sid)};
113 if (!TextUtils.isEmpty(selection) && selectionArgs != null)
114 {
115 params = new String[selectionArgs.length + 1];
116 for (int i = 0; i < selectionArgs.length; i++)
117 {
118 params[i] = selectionArgs[i];
119 }
120 params[selectionArgs.length] = String.valueOf(sid);
121 }
122 count = db.delete("person", where, params);
123 break;
124 default:
125 throw new IllegalArgumentException("Unknow Uri:" + uri);
126 }
127 return count;
128 }
129
130 @Override
131 public boolean onCreate()
132 {
133 dataBaseOpenHelp = new DataBaseOpenHelp(this.getContext());
134 return true;
135 }
136
137 // ok
138 @Override
139 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
140 {
141 SQLiteDatabase db = dataBaseOpenHelp.getWritableDatabase();
142 switch (pMatcher.match(uri))
143 {
144 case PERSONS:
145 return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
146 case PERSON:
147 long pid = ContentUris.parseId(uri);
148 String where = TextUtils.isEmpty(selection) ? "personid=?" : selection + "and personid=?";
149 String[] params = new String[] {String.valueOf(pid)};
150 if (!TextUtils.isEmpty(selection) && selectionArgs != null)
151 {
152 params = new String[selectionArgs.length + 1];
153 for (int i = 0; i < selectionArgs.length; i++)
154 {
155 params[i] = selectionArgs[i];
156 }
157 }
158 return db.query("person", projection, where, params, null, null, sortOrder);
159 default:
160 throw new IllegalArgumentException("Unknow Uri:" + uri);
161 }
162 }
163
164 }
1.4 AndroidManifest.xml文件中的配置:
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.vanceinfo.db" android:versionCode="1" android:versionName="1.0">
4 <uses-sdk android:minSdkVersion="9" />
5 <application android:icon="@drawable/icon" android:label="@string/app_name">
6 <provider android:name="com.vanceinfo.provider.PersonProvider"
7 android:authorities="com.vanceinfo.personprovider" />
8 <uses-library android:name="android.test.runner" />
9 <activity android:name=".DBActivity" android:label="@string/app_name">
10 <intent-filter>
11 <action android:name="android.intent.action.MAIN" />
12 <category android:name="android.intent.category.LAUNCHER" />
13 </intent-filter>
14 </activity>
15 </application>
16 <instrumentation android:name="android.test.InstrumentationTestRunner"
17 android:targetPackage="com.vanceinfo.db" />
18 </manifest>
2. SQLiteTestContentProvider工程(第二个工程)
2.1 SQLiteTestContentProvider:
1 package com.vanceinfo.SQLiteTestContentProvider;
2
3 import android.app.Activity;
4 import android.content.ContentResolver;
5 import android.content.ContentValues;
6 import android.database.Cursor;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12
13 public class SQLiteTestContentProvider extends Activity
14 {
15 private static final String TAG = "SQLiteTestContentProvider";
16
17 private static final int ONE = 1;
18
19 private static final int TWO = 2;
20
21 private Button bz1;
22
23 private Button bz2;
24
25 private Button bs1;
26
27 private Button bs2;
28
29 private Button bc1;
30
31 private Button bc2;
32
33 private Button bg1;
34
35 private Button bg2;
36
37 @Override
38 public void onCreate(Bundle savedInstanceState)
39 {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.main);
42 bz1 = (Button) findViewById(R.id.z1);
43 bz2 = (Button) findViewById(R.id.z2);
44 bs1 = (Button) findViewById(R.id.s1);
45 bs2 = (Button) findViewById(R.id.s2);
46 bc1 = (Button) findViewById(R.id.c1);
47 bc2 = (Button) findViewById(R.id.c2);
48 bg1 = (Button) findViewById(R.id.g1);
49 bg2 = (Button) findViewById(R.id.g2);
50 bz1.setOnClickListener(l);
51 bz2.setOnClickListener(l);
52 bs1.setOnClickListener(l);
53 bs2.setOnClickListener(l);
54 bc1.setOnClickListener(l);
55 bc2.setOnClickListener(l);
56 bg1.setOnClickListener(l);
57 bg2.setOnClickListener(l);
58 }
59
60 OnClickListener l = new OnClickListener()
61 {
62
63 @Override
64 public void onClick(View v)
65 {
66 switch (v.getId())
67 {
68 case R.id.z1:
69 add(ONE);
70 break;
71 case R.id.z2:
72 add(TWO);
73 break;
74 case R.id.s1:
75 delete(ONE);
76 break;
77 case R.id.s2:
78 delete(TWO);
79 break;
80 case R.id.c1:
81 query(ONE);
82 break;
83 case R.id.c2:
84 query(TWO);
85 break;
86 case R.id.g1:
87 update(ONE);
88 break;
89 case R.id.g2:
90 update(TWO);
91 break;
92 default:
93 break;
94 }
95 }
96
97 private void add(int type)
98 {
99 ContentResolver contentResolver = getContentResolver();
100 System.out.println("contentResolver=" + contentResolver);
101 Uri url = null;
102 ContentValues values = new ContentValues();
103 switch (type)
104 {
105 case ONE:
106 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO");
107 values.put("sender", "haha");
108 // values.put("age", "29");
109 System.out.println(contentResolver.insert(url, values).toString());
110 break;
111 case TWO:
112 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO/1");
113 values.put("sender", "libai");
114 // values.put("age", "60");
115 System.out.println(contentResolver.insert(url, values).toString());
116 break;
117 }
118 }
119 };
120
121 protected void delete(int type)
122 {
123 ContentResolver contentResolver = getContentResolver();
124 Uri url = null;
125 String where = "";
126 String[] selectionArgs = null;
127 switch (type)
128 {
129 case ONE:
130 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO");
131 where = "sender = ?";
132 selectionArgs = new String[] {"system"};
133 contentResolver.delete(url, where, selectionArgs);
134 break;
135 case TWO:
136 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO/2");
137 where = null;
138 selectionArgs = null;
139 contentResolver.delete(url, where, selectionArgs);
140 break;
141 }
142
143 }
144
145 private void query(int type)
146 {
147 ContentResolver contentResolver = getContentResolver();
148 Uri url = null;
149 String[] projection = new String[] {"_id", "photoid", "sendtime", "name", "sender", "savepath", "size",
150 "isnewphoto", "status", "desc", "type", "orientation"};
151 String selection = "";
152 String[] selectionArgs = null;
153 String sortOrder = "";
154 Cursor cursor = null;
155 switch (type)
156 {
157 case ONE:
158 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO");
159 selection = "_id<?";
160 selectionArgs = new String[] {"3"};
161 cursor = contentResolver.query(url, projection, selection, selectionArgs, sortOrder);
162 while (cursor.moveToNext())
163 {
164 System.out.println("_id=" + cursor.getInt(0) + ",photoid=" + cursor.getInt(1) + ",sendtime="
165 + cursor.getInt(2));
166 }
167 break;
168 case TWO:
169 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO/18");
170 cursor = contentResolver.query(url, projection, selection, selectionArgs, sortOrder);
171 while (cursor.moveToNext())
172 {
173 System.out.println("_id=" + cursor.getInt(0) + ",photoid=" + cursor.getInt(1) + ",sendtime="
174 + cursor.getInt(2));
175 }
176 break;
177 }
178 }
179
180 private void update(int type)
181 {
182 ContentResolver contentResolver = getContentResolver();
183 Uri url = null;
184 ContentValues values = new ContentValues();
185 String where = "";
186 String[] selectionArgs = null;
187 switch (type)
188 {
189 case ONE:
190 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO");
191 values.put("photoid", "3");
192 values.put("sendtime", "99");
193 where = "sender = ?";
194 selectionArgs = new String[] {"haha"};
195 contentResolver.update(url, values, where, selectionArgs);
196 break;
197 case TWO:
198 url = Uri.parse("content://com.vanceinfo.contentprovider/PHOTO/1");
199 values.put("name", "update");
200 values.put("age", "99");
201 selectionArgs = null;
202 where = null;
203 contentResolver.update(url, values, where, selectionArgs);
204 break;
205 }
206 }
207
208 }