來源:_110 發(fā)布時間:2018-11-30 17:43:08 閱讀量:1147
應用A
package com.example.testc;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
//創(chuàng)建 表格 名字data 表格里面 主鍵 uid int ,total text , name text
private static final String CREATE_TABLE = "create table data(_id INTEGER PRIMARY KEY autoincrement,uid int,total text,name text);";
private Context mContext;
public DatabaseHelper (Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub創(chuàng)建數(shù)據(jù)庫后,對數(shù)據(jù)庫的操作
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub 更改數(shù)據(jù)庫版本的操作
db.execSQL("drop table if exists peopleinfo");
onCreate(db);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.testc;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@TargetApi(23)
public class MainActivity extends Activity {
private Context mcontext;
private TextView IEMI;
MapBroadcast mapReceiver;
DatabaseHelper helper;
SQLiteDatabase db;
ArrayList< Appinfo> lists ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcontext = getApplicationContext();
initView();
helper = new DatabaseHelper(MainActivity.this, "mydb.db", null, 1);
db = helper.getWritableDatabase();
lists = new ArrayList<Appinfo>();
/* 創(chuàng)建一個MySQLiteOpenHelper,該語句執(zhí)行是不會創(chuàng)建或打開連接的 */
}
private void initView() {
// TODO Auto-generated method stub
IEMI = (TextView) findViewById(R.id.hello);
Button bt = (Button) findViewById(R.id.bt);
Button bt1 = (Button)findViewById(R.id.bt1);
Button bt2 = (Button)findViewById(R.id.bt3);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("DELETE FROM data"); //插入前清空表格
for (int i = 0; i < 10; i++) {
ContentValues values = new ContentValues();
values.put("uid", i);
values.put("total", "12MB");
values.put("name", "zhangsan");
// 插入數(shù)據(jù)
db.insert("data", null, values);
}
}
});
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//清空表格數(shù)據(jù)
testQuery();
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//查詢數(shù)據(jù)
Cursor cursor = db.query("data", null, null, null, null, null, null);
lists.clear();
while (cursor.moveToNext()) {
String uid = cursor.getString(cursor.getColumnIndex("uid"));
String total = cursor.getString(cursor.getColumnIndex("total"));
String name = cursor.getString(cursor.getColumnIndex("name"));
Appinfo appinfo=new Appinfo(uid, total, name);
/* Log.e("TAG", "[uid]="+uid+"[total]="+total+"[name]="+name);
*/ appinfo.setName(name);
appinfo.setTotal(total);
appinfo.setUid(uid);
lists.add(appinfo);
}
cursor.close();
if(lists.size() > 0){
for (Appinfo listappinfo : lists) {
Log.e("data", "[info]="+listappinfo.toString());
}
}
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
數(shù)據(jù)庫對外提供接口 內(nèi)容提供者
package com.example.testc;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class AppTotalContentProvider extends ContentProvider {
private DatabaseHelper dbHelper;
// 若不匹配采用UriMatcher.NO_MATCH(-1)返回
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
// 匹配碼
private static final int CODE_NOPARAM = 1;
static
{
// 對等待匹配的URI進行匹配操作,必須符合cn.xyCompany.providers.personProvider/person格式
// 匹配返回CODE_NOPARAM,不匹配返回-1
MATCHER.addURI("com.example.testc", "data", CODE_NOPARAM);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dbHelper = new DatabaseHelper(this.getContext(), "mydb.db", null, 1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getReadableDatabase();
return db.query("data", projection, selection, selectionArgs, null, null, sortOrder);
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getWritableDatabase();
// 特別說一下第二個參數(shù)是當name字段為空時,將自動插入一個NULL。
long rowid = db.insert("data", "null", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);// 得到代表新增記錄的Uri
this.getContext().getContentResolver().notifyChange(uri, null);
return insertUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//插入數(shù)據(jù)到數(shù)據(jù)庫
ContentResolver contentResolver = getContentResolver();
Uri insertUri = Uri.parse("content://com.example.testc/data");
ContentValues values = new ContentValues();
values.put("name", "wangkuifeng");
values.put("uid", "1111");
values.put("total", "123M");
Uri uri = contentResolver.insert(insertUri, values);
1
2
3
4
5
6
7
8
在AndroidManifest.xml 添加
<!-- 配置內(nèi)容提供者,android:authorities為該內(nèi)容提供者取名作為在本應用中的唯一標識 -->
<provider
android:name="com.example.testc.AppTotalContentProvider"
android:authorities="com.example.testc"
android:exported="true" />
1
2
3
4
5
測試: 應用B
package com.example.testb;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testQuery();
}
public void testQuery(){
Uri uri = Uri.parse("content://com.example.testc/data");
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"uid","name","total"}, null, null, null);
while(cursor.moveToNext()){
String uid= cursor.getString(cursor.getColumnIndex("uid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String total = cursor.getString(cursor.getColumnIndex("total"));
Log.e("TAG", "[uid]="+uid+"[total]="+total +"[name]="+name);
}
cursor.close();
}
}
---------------------