android 7.4.2
打开以前写的数据库项目,(不是litepal),把MydatabaseHelper.java文件里的那行Toast去掉, 用鼠标右键点击包名new->other->content provider的方法在maniactivity.java旁边新建个类名为DatabaseProvider,授权为com.example.databasetest.provider的文件,把底下2个选项也勾上,修改内容为package com.example.xinwei.databasetest;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;
public class DatabaseProvider extends ContentProvider {
public static final int BOOK_DIR = 0;
public static final int BOOK_ITEM = 1;
public static final int CATEGORY_DIR = 2;
public static final int CATEGORY_ITEM = 3;
public static final String AUTHORITY = "com.example.databasetest.provider";
private static UriMatcher uriMatcher;
private MyDatabaseHelper dbHelper;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
uriMatcher.addURI(AUTHORITY,"category",CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM);
}
public DatabaseProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
SQLiteDatabase db=dbHelper.getWritableDatabase();
int deleteRows=0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
deleteRows=db.delete("book",selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegments().get(1);
deleteRows=db.delete("book","id=?",new String[]{bookId});
break;
case CATEGORY_DIR:
deleteRows=db.delete("Category",selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegments().get(1);
deleteRows=db.delete("Category","id=?",new String[]{categoryId});
break;
default:
break;
}
return deleteRows;
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example.databasetest.provider.category";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
SQLiteDatabase db=dbHelper.getWritableDatabase();
Uri uriReturn=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
case BOOK_ITEM:
long newBookId=db.insert("book",null,values);
uriReturn=Uri.parse("content://"+AUTHORITY+"/book/"+newBookId);
break;
case CATEGORY_DIR:
case CATEGORY_ITEM:
break;
default:
break;
}
return uriReturn;
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
dbHelper = new MyDatabaseHelper(getContext(),"BookStore.db",null,2);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
SQLiteDatabase db= dbHelper.getReadableDatabase();
Cursor cursor=null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor=db.query("book",projection,selection,selectionArgs,null,null,sortOrder);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegments().get(1);
cursor=db.query("book",projection,"id=?",new String[]{bookId},null,null,sortOrder);
break;
case CATEGORY_DIR:
cursor=db.query("Category",projection,selection,selectionArgs,null,null,sortOrder);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegments().get(1);
cursor=db.query("Category",projection,"id=?",new String[]{categoryId},null,null,sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
SQLiteDatabase db=dbHelper.getWritableDatabase();
int updateRows=0;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
updateRows=db.update("book",values,selection,selectionArgs);
break;
case BOOK_ITEM:
String bookId=uri.getPathSegments().get(1);
updateRows=db.update("book",values,"id=?",new String[]{bookId});
break;
case CATEGORY_DIR:
updateRows=db.update("Category",values,selection,selectionArgs);
break;
case CATEGORY_ITEM:
String categoryId=uri.getPathSegments().get(1);
updateRows=db.update("Category",values,"id=?",new String[]{categoryId});
break;
default:
break;
}
return updateRows;
}
}
然后建个providerTest新项目,用来读取上个项目数据库的内容,在这之前先把原来手机上的数据库项目删掉,从新运行一下我们刚才修改的数据库项目然后关闭。在新项目里修改布局文件activity_main.xml文件<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/add_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add to book"/>
<Button
android:id="@+id/query_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="query from book"/>
<Button
android:id="@+id/update_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="update book"/>
<Button
android:id="@+id/delete__data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="delete from book"/>
</LinearLayout>
修改mainactivity.javapackage com.example.xinwei.providertest;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private String newId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addData=(Button)findViewById(R.id.add_data);
addData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri=Uri.parse("content://com.example.databasetest.provider/book");
ContentValues contentValues=new ContentValues();
contentValues.put("name","A Clash of Kings");
contentValues.put("author","George Martin");
contentValues.put("pages",1040);
contentValues.put("price",22.85);
Uri newUri=getContentResolver().insert(uri,contentValues);
newId=newUri.getPathSegments().get(1);
}
});
Button queryData=(Button)findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri=Uri.parse("content://com.example.databasetest.provider/book");
Cursor cursor=getContentResolver().query(uri,null,null,null,null);
if (cursor!=null){
while (cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price=cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity","book name is"+name);
Log.d("MainActivity","book author is"+author);
Log.d("MainActivity","book pages is"+pages);
Log.d("MainActivity","book price is"+price);
}
cursor.close();
}
}
});
Button updateData=(Button)findViewById(R.id.update_data);
updateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri=Uri.parse("content://com.example.databasetest.provider/book"+newId);
ContentValues contentValues=new ContentValues();
contentValues.put("name","A Storm of Swords");
contentValues.put("price",24.05);
contentValues.put("pages",1216);
getContentResolver().update(uri,contentValues,null,null);
}
});
Button deleteData=(Button)findViewById(R.id.delete__data);
deleteData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri=Uri.parse("content://com.example.databasetest.provider/book"+newId);
getContentResolver().delete(uri,null,null);
}
});
}
}
再运行这个新项目就可以对原来的数据库进行增删改查了
页:
[1]