例子说明:
程序A:建立数据库DB,表student、salary,数据SQLiteOpenHelper来存储,
程序B:对程序A进行数据的增删改查;
SQLiteOpenHelper:
public class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context)
{
super(context, "DB", null, 1);//创建数据库“DB”;
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table student(id integer primary key autoincrement, name text, number integer)");
db.execSQL("create table salary(id integer primary key autoincrement, name text, money integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
SQLiteDatabase:
public class DBUtil
{
DBHelper helper;
SQLiteDatabase db;
public DBUtil(Context context)
{
helper = new DBHelper(context); //实例化;
}
private void open() //打开数据库;
{
db = helper.getWritableDatabase();
}
private void close()//关闭数据库;
{
if(db!=null)
{
db.close();
}
}
public long insert(String table, ContentValues values)
{
open();
long code=db.insert(table, null, values); //code为插入数据的行数;
System.out.println( table + " "+code);
close();
return code;
}
//查询表中的所有与数据;
public void query(String table)
{
open();
System.out.println("table ---" + table);
if("student".equals(table))
{
String sql = "select * from student";
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String number = cursor.getString(cursor.getColumnIndex("number"));
System.out.println(table + ", id=" + id + ", name=" + name + ", number=" + number);
}
}
else if("salary".equals(table))
{
String sql = "select * from salary";
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String money = cursor.getString(cursor.getColumnIndex("money"));
System.out.println(table + ", id=" + id + ", name=" + name + ", money=" + money);
}
}
close();
}
//删除由whereClause指定的数据;
public void delete(String table, String whereClause, String[] whereArgs )
{
open();
db.delete(table, whereClause, whereArgs);
System.out.println("whereClause---" + whereClause);
close();
}
//更新数据库,把符合whereClause条件的数据更改为values;
public void update(String table, ContentValues values, String whereClause)
{
open();
db.update(table, values, whereClause, null);
close();
}
}
自定义类MyContentProvider 继承ContentProvide类:
public class MyContentProvider extends ContentProvider
{
DBUtil dbutil;
static UriMatcher uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
final static String AUTHORITY="com.feicui.contentprovider"; //在清单文件注册时的authorities属性值;
final static int URI_STUDENT=1;
final static int URI_SALARY=2;
final static int URI_STUDENT_WHICH=3;
final static int URI_SALARY_WHICH=4;
static
{
//为UriMatcher注册四个Uri;
uriMatcher.addURI(AUTHORITY, "student", URI_STUDENT);
uriMatcher.addURI(AUTHORITY, "student/#", URI_STUDENT_WHICH);
uriMatcher.addURI(AUTHORITY, "salary", URI_SALARY);
uriMatcher.addURI(AUTHORITY, "salary/#", URI_SALARY_WHICH);
}
@Override
public boolean onCreate()
{
dbutil = new DBUtil(getContext());
return false;
}
// 查询数据;
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
int code = uriMatcher.match(uri);
switch(code)
{
case URI_STUDENT:
dbutil.query("student"); //查询student表下的所有数据;
break;
case URI_SALARY:
dbutil.query("salary"); //查询salary表下的所有数据;
break;
default:
throw new IllegalArgumentException("未知Uri:" + uri);
}
return null;
}
@Override
public String getType(Uri uri)
{
return null;
}
// 插入数据;
@Override
public Uri insert(Uri uri, ContentValues values)
{
int code=uriMatcher.match(uri); //获取在上面static代码块中对应的code值;
long insertID=0;
switch (code)
{
case URI_STUDENT:
insertID=dbutil.insert("student", values); //往student表中插入数据;
break;
case URI_SALARY:
insertID=dbutil.insert("salary", values); //往salary表中插入数据;
break;
default:
throw new IllegalArgumentException("未知Uri:" + uri);
}
return ContentUris.withAppendedId(uri, insertID);
}
// 删除数据;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
int code=uriMatcher.match(uri);
switch (code)
{
case URI_STUDENT: //删除整个表的数据;
System.out.println("---------selection----" + selection);
dbutil.delete("student", selection, selectionArgs);
break;
case URI_STUDENT_WHICH: //删除表中指定的一个数据;
{
long id = ContentUris.parseId(uri);
String whereClause = "id=" + id;
if(selection != null && selection.equals("")) //如果原来的selection语句存在,则拼接语句;
{
whereClause = selection + " and " + whereClause;
}
dbutil.delete("student", whereClause, selectionArgs);
break;
}
case URI_SALARY: //删除整个表的数据;
dbutil.delete("salary", selection, selectionArgs);
break;
case URI_SALARY_WHICH://删除表中指定的一个数据;
{
long id2 = ContentUris.parseId(uri);
String whereClause2 = "id=" + id2;
if(selection != null && selection.equals("")) //如果原来的selection语句存在,则拼接语句;
{
whereClause2 = selection + " and " + whereClause2;
}
dbutil.delete("salary", whereClause2, selectionArgs);
break;
}
default:
throw new IllegalArgumentException("未知Uri:" + uri);
}
return 0;
}
//更新数据;
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
int code=uriMatcher.match(uri);
switch (code)
{
case URI_STUDENT:
dbutil.update("student", values, selection);
break;
case URI_SALARY:
dbutil.update("salary", values, selection);
break;
default:
throw new IllegalArgumentException("未知Uri:" + uri);
}
return 0;
}
}
MainActivity:
public class MainActivity extends Activity implements OnClickListener
{
Button salary; //为表salary添加数据;
Button student; //为表student添加数据;
ContentResolver resolver;
final static String CONTENT_URI="content://com.feicui.contentprovider/";
String[] names = {"a","b","c","d","e","f","g","h","i"};//姓名;
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //座号;
int[] money = {2223,2224,2225,2226,2227,2228,2229,2230,2221};//薪水;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
File file = new File("data/data/com.feicui.contentprovider/databases/DB");
if(file.exists()) //如果文件已经存在则删除;
{
file.delete();
}
}
private void init()
{
student = (Button) findViewById(R.id.button1);
salary = (Button) findViewById(R.id.button2);
student.setOnClickListener(this);
salary.setOnClickListener(this);
resolver = getContentResolver(); //获取ContentResolver对象;
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
for(int i=0; inew ContentValues();
values.put("name", names[i]);
values.put("number", numbers[i]);
resolver.insert(Uri.parse(CONTENT_URI+"student"), values);
}
break;
case R.id.button2:
for(int i=0; inew ContentValues();
values2.put("name", names[i]);
values2.put("money", money[i]);
Uri uri=resolver.insert(Uri.parse(CONTENT_URI+"salary"), values2);
System.out.println(ContentUris.parseId(uri) + "----");
}
break;
default:
break;
}
}
}
给程序A注册:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<provider android:name="com.feicui.contentprovider.MyContentProvider" android:authorities="com.feicui.contentprovider"
android:exported="true">
provider>
上面的代码块都是程序A中的代码;
程序B就下面这个代码块:
public class MainActivity extends Activity implements OnClickListener
{
ContentResolver resolver;
Button insert, delete, update, query;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resolver=getContentResolver();
init();
}
private void init()
{
insert = (Button)findViewById(R.id.button1);
delete = (Button)findViewById(R.id.button2);
update = (Button)findViewById(R.id.button3);
query = (Button)findViewById(R.id.button4);
insert.setOnClickListener(this);
delete.setOnClickListener(this);
update.setOnClickListener(this);
query.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button1:
insertData(); //插入数据;
break;
case R.id.button2:
// deleteData(); //删除整个表里的内容;
// deleteCodeData("student", " name='a'"); //删除student表中name为a的数据;
deleteCodeData("student/2", null); //删除student表中id为2的数据;(后面是int型数据时默认的就是指id);
break;
case R.id.button3:
updateData(); //更新数据;
break;
case R.id.button4:
queryData(); //查询数据;
break;
}
}
private void updateData()
{
ContentValues values = new ContentValues();
values.put("money", 9999);
resolver.update(Uri.parse("content://com.feicui.contentprovider/salary"), values, "name='d'", null);
}
private void deleteData()
{
resolver.delete(Uri.parse("content://com.feicui.contentprovider/student"), null, null);
}
private void deleteCodeData(String table, String where)
{
resolver.delete(Uri.parse("content://com.feicui.contentprovider/" + table), where, null);
}
private void queryData()
{
resolver.query(Uri.parse("content://com.feicui.contentprovider/student"), null, null, null, null);
resolver.query(Uri.parse("content://com.feicui.contentprovider/salary"), null, null, null, null);
}
private void insertData()
{
ContentValues values1=new ContentValues();
values1.put("name", "j");
values1.put("number", 10);
resolver.insert(Uri.parse("content://com.feicui.contentprovider/student"), values1);
ContentValues values2=new ContentValues();
values2.put("name", "k");
values2.put("number", 11);
resolver.insert(Uri.parse("content://com.feicui.contentprovider/student"), values2);
ContentValues values3=new ContentValues();
values3.put("name", "j");
values3.put("money", 2232);
resolver.insert(Uri.parse("content://com.feicui.contentprovider/salary"), values3);
ContentValues values4=new ContentValues();
values4.put("name", "k");
values4.put("money", 2233);
resolver.insert(Uri.parse("content://com.feicui.contentprovider/salary"), values4);
}
}