我们要做的是这么个东西
保存用户基本信息到手机sd卡中的ssss.db sqlite数据库中,而数据库的读写是通过ContentProvider共享的
1.先在自己电脑上新建名称为ssss.db的sqlite数据,里面新建表userinfo,列有int _id,text
name,int age, float height
_id为主键,设置为自动增长,
2.数据新建完成后,把数据库导入android虚拟机的sdcard中mydatabase目录下
3.新建工程Class20131012Provider
在工程里面写个MyProvider类,继承ContentProvider,实现里面的抽象方法,用于共享数据库
MyProvider代码:
package com.example.class20131012provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
public class MyProvider extends ContentProvider{
SQLiteDatabase sqliteDatabase;
@Override
public boolean onCreate() {
sqliteDatabase =
SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory().getPath()+"/mydatabase/ssss.db",
null,
SQLiteDatabase.OPEN_READWRITE);
if(null!=sqliteDatabase)
{
return
true;
}
return false;
}
private static String authority =
"com.example.class20131012provider";
private String pack =
"com.example.class20131012provider";
private static UriMatcher
uriMatcher;//用于匹配Uri
static{
uriMatcher = new
UriMatcher(UriMatcher.NO_MATCH);
//authority:一般放程序的包名
//path:一般放表名
//code:表示匹配后返回的值
//匹配多条
uriMatcher.addURI(authority,
"userinfo", 1);
//匹配单条
uriMatcher.addURI(authority,
"userinfo/#", 2);
}
@Override
public Cursor query(Uri uri, String[] projection,
String selection,
String[]
selectionArgs, String sortOrder) {
//分析URI 客户端想要一条数据,还是多条数据
switch (uriMatcher.match(uri))
{
case 2://单条数据
//content://com...xx./userinfo/77
String table
= uri.getPathSegments().get(0);
String argId
= uri.getPathSegments().get(1);
return
sqliteDatabase.query(table, projection,"_id=?",new String[]{argId},
null, null, sortOrder);
case 1://多条数据
String table1
= uri.getPathSegments().get(0);
return
sqliteDatabase.query(table1, projection, selection, selectionArgs,
null, null, sortOrder);
default:
break;
}
return null;
}
@Override
public String getType(Uri uri) {
String ret = null;
switch (uriMatcher.match(uri))
{
case 2://单条数据
ret =
"vnd.android.cursor.item/vnd."+pack+".userinfo";
break;
case 1://多条数据
ret =
"vnd.android.cursor.dir/vnd."+pack+".userinfo";
break;
default:
break;
}
return ret;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
String table =
uri.getPathSegments().get(0);
long id =
sqliteDatabase.insert(table, null, values);
String uriStr =
"content://"+authority+"/userinfo/"+id;
Uri retUri =
Uri.parse(uriStr);
return retUri;
}
@Override
public int delete(Uri uri, String selection,
String[] selectionArgs) {
switch (uriMatcher.match(uri))
{
case 1:
String
table = uri.getPathSegments().get(0);
return
sqliteDatabase.delete(table, null, null);
case 2:
String table2
= uri.getPathSegments().get(0);
String argId
= uri.getPathSegments().get(1);
Log.i("AAA",
"table:"+table2);
Log.i("AAA",
"argId:"+argId);
return
sqliteDatabase.delete(table2, "_id=?", new String[]{argId});
default:
break;
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values,
String selection,
String[]
selectionArgs) {
String table =
uri.getPathSegments().get(0);
String argId =
uri.getPathSegments().get(1);
Log.i("AAA",
"table:"+table);
Log.i("AAA",
"argId:"+argId);
return
sqliteDatabase.update(table, values, "_id=?", new
String[]{argId});
}
}
4.配置AndroidManifest.xml文件
MyProvider类写好之后,还要在AndroidManifest.xml中配置权限,对sd卡进行读写的权限
还需要配置provider
5.发布程序即可
6.共享数据库的工作已经完成,现在我们新建工程Class20131012Resolver
7.在编写程序界面的布局文件,就是上面图片的那个样子,代码如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" />
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:" />
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高:" />
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableLayout1" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加数据" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="全部显示" />
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="清除显示" />
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="全部删除" />
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1" >
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID:" />
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="4" />
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID删除" />
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID查询" />
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID更新" />
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout2"
android:text="数据库:" />
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView5"
android:text="TextView" />
8.获得ContentResolver,实现对数据库中的数据进行增删改查(增删改查在provider中已经写好了,这里只需要获得resolver,然后调用里面对应的方法传参数过去就行了,系统就会找到我们所有用到的provider,然后进行处理。)代码如下:
package com.example.class20131012resolver;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements
OnClickListener{
EditText edtName,edtAge,edtHeight,edtId;
TextView tv; private ContentResolver resolver;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtName =
(EditText)findViewById(R.id.editText1);
edtAge =
(EditText)findViewById(R.id.editText2);
edtHeight =
(EditText)findViewById(R.id.editText3);
edtId =
(EditText)findViewById(R.id.editText4);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
tv =
(TextView)findViewById(R.id.textView6);
resolver =
getContentResolver();
}
@Override
public void onClick(View v) {
Cursor cursor;
int id;
String name;
int age;
float height;
StringBuffer sb = new
StringBuffer();
ContentValues cv = new
ContentValues();
switch(v.getId())
{
case R.id.button1://添加数据
cv.put("name",
edtName.getText().toString());
cv.put("age",
Integer.valueOf(edtAge.getText().toString()));
cv.put("height",
Float.valueOf(edtHeight.getText().toString()));
Uri
requestUri =
Uri.parse("content://com.example.class20131012provider/userinfo/");
resolver.insert(requestUri,
cv);
break;
case R.id.button2://全部显示
Uri findUri =
Uri.parse("content://com.example.class20131012provider/userinfo/");
cursor =
resolver.query(findUri, null, null, null, null);
// 查询到的数据的条数
int count =
cursor.getCount();
// 将游标移动到第一条数据
cursor.moveToFirst();
for (int
i=0;i
{
id
= cursor.getInt(cursor.getColumnIndex("_id"));
name
= cursor.getString(cursor.getColumnIndex("name"));
age
= cursor.getInt(cursor.getColumnIndex("age"));
height
= cursor.getFloat(cursor.getColumnIndex("height"));
sb.append("ID:"+id+" 姓名:"+name+" 年龄:"+age+" 身高:"+height+"\n");
cursor.moveToNext();
}
tv.setText(sb.toString());
break;
case R.id.button3://清除显示
tv.setText("");
break;
case R.id.button4://全部删除
Uri
deleteAllUri =
Uri.parse("content://com.example.class20131012provider/userinfo/");
resolver.delete(deleteAllUri,
null, null);
break;
case R.id.button5://ID删除
Uri
deleteByIdUri =
Uri.parse("content://com.example.class20131012provider/userinfo/"+edtId.getText().toString());
resolver.delete(deleteByIdUri,
null, new String[]{edtId.getText().toString()});
break;
case R.id.button6://ID查询
Uri
findByIdUri =
Uri.parse("content://com.example.class20131012provider/userinfo/"+edtId.getText().toString());
cursor =
resolver.query(findByIdUri, null, null, new
String[]{edtId.getText().toString()}, null);
cursor.moveToFirst();
if(cursor.getCount()>0)
{
id
= cursor.getInt(cursor.getColumnIndex("_id"));
name
= cursor.getString(cursor.getColumnIndex("name"));
age
= cursor.getInt(cursor.getColumnIndex("age"));
height
= cursor.getFloat(cursor.getColumnIndex("height"));
sb.append("ID:"+id+" 姓名:"+name+" 年龄:"+age+" 身高:"+height+"\n");
}
tv.setText(sb.toString());
break;
case R.id.button7://ID更新
Uri
updateByIdUri =
Uri.parse("content://com.example.class20131012provider/userinfo/"+edtId.getText().toString());
cv.put("name",
edtName.getText().toString());
cv.put("age",
Integer.valueOf(edtAge.getText().toString()));
cv.put("height",
Float.valueOf(edtHeight.getText().toString()));
resolver.update(updateByIdUri,
cv, null, new String[]{edtId.getText().toString()});
break;
}
}
}