//1.继承SQLiteOpenHelper类,并重写其中方法
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class VegetableStore
extends SQLiteOpenHelper{
public VegetableStore(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version) {
super(context, name, factory, version); }
public VegetableStore(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//2.重写onCreate方法--数据库初始化方法---常用的操作:建表的sql语句的创建和执行
@Override
public void onCreate(SQLiteDatabase db) {
//建表语句
String sql = "create table luobo(_id Integer primary key autoincrement,amount text,price text)";
//执行语句
db.execSQL(sql);
String sql1 ="create table lizi(_id Integer primary key autoincrement,amount text,price text)";
db.execSQL(sql1);
String sql2 = "create table taozi(_id Integer primary key autoincrement,amount text,price text)";
db.execSQL(sql2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
//3.库的实例化,调用有参构造方法
VegetableStore vegetableStore =
new VegetableStore(
this,
"vegetable.db",
null,
1);
//4.使用库对象----此步骤完成后可以编译执行,导出库,可看到库下的表和字段名已经生成
SQLiteDatabase myVegetabledb = vegetableStore.getWritableDatabase();
}
}
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.support.annotation.Nullable;
/**
* Created by think on 2016/5/24.
*/
//5.创建内容提供者,并在文件管理器中配置提供者的主机名、提供者名字、和可访问的权限--继承ContentProvider,并重写其中的方法
public class VegetableProvider
extends ContentProvider{
private SQLiteDatabase
myVegdbPro;
//8.创建筛选器---实例化UriMatcher对象
//地址校验器---为实现多个表提供后方便解析者查找
UriMatcher
um =
new UriMatcher(UriMatcher.
NO_MATCH);
{
//9.将库中的表通过addURL方法加入筛选器中
//参数1:主机名字-提供者的配置文件中,参数2:表名,参数3:表名的数字
um.addURI(
"com.example.vegetable",
"luobo",
1);
um.addURI(
"com.example.vegetable",
"lizi",
2);
um.addURI(
"com.example.vegetable",
"taozi",
3);
}
@Override
public boolean onCreate() {
//6.实例化库对象,通过库名找到已经建立的库
//参数2:为数据库名字,有后缀.db
VegetableStore vegetableStore =
new VegetableStore(getContext(),
"vegetable.db",
null,
1);
//7.调用方法,确定以什么方式打开此数据库
myVegdbPro = vegetableStore.getWritableDatabase();
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor =
null;
System.
out.println(
"进入了提供者的查询方法");
if(
um.match(uri) ==
1) {
cursor =
myVegdbPro.query(
"luobo",projection, selection, selectionArgs, sortOrder,
null,
null);
}
else if(
um.match(uri) ==
2) {
System.
out.println(
"进入了提供者的查询方法中的第二个判断");
cursor =
myVegdbPro.query(
"lizi",projection, selection, selectionArgs, sortOrder,
null,
null);
System.
out.println(
"执行了提供者的查询方法中的第二个判断");
}
else if(
um.match(uri) ==
3) {
cursor =
myVegdbPro.query(
"taozi",projection, selection, selectionArgs, sortOrder,
null,
null);
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
//10.对传过来经过代码块判断完的链接生成的表名数字进行判断
//地址过滤器的使用---筛选作用
//um.match(uri)方法通过在代码块进行比较,返回一个int型数据,此数据就是链接匹配上后的数字
if(
um.match(uri) ==
1) {
//11.如果匹配成功后,进行的操作----此处就是在对应的表中插入数据,调用insert方法即可
//此步骤完毕后,提供者功能完成,该是解析者的代码了
myVegdbPro.insert(
"luobo",
null,values);
}
else if(
um.match(uri) ==
2) {
myVegdbPro.insert(
"lizi",
null,values);
}
else if(
um.match(uri) ==
3) {
myVegdbPro.insert(
"taozi",
null,values);
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
System.
out.println(
"进入了提供者的删除方法");
switch (
um.match(uri)) {
case
1:
myVegdbPro.delete(
"luobo",selection,selectionArgs);
break;
case
2:
myVegdbPro.delete(
"lizi",selection,selectionArgs);
break;
case
3:
System.
out.println(
"进入了提供者的删除方法的case3");
myVegdbPro.delete(
"taozi",selection,selectionArgs);
System.
out.println(
"执行了case3中的删除");
break;
}
return
0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
switch (
um.match(uri)) {
case
1:
myVegdbPro.update(
"luobo",values,selection,selectionArgs);
break;
case
2:
myVegdbPro.update(
"lizi",values,selection,selectionArgs);
break;
case
3:
myVegdbPro.update(
"taozi",values,selection,selectionArgs);
break;
}
return
0;
}
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private ContentResolver resolver;
private Uri uri;
private ContentValues values;
private Cursor cursor;
/**
* 当外部访问其他app时,被访问的app要是运行的状态,所以多注意运行被访问的app
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//12.内容解析者----通过调用getContentResolver()方法
resolver =
this.getContentResolver();
/*//插入内容
//14.设置想要进行操作的表的链接
//Uri对象调用静态方法从字符串中解析出内容提供者对象---添加筛选器后地址字符格式:content://文件清单中配置的主机名/表名
uri = Uri.parse("content://com.example.vegetable/luobo");
//15.实例化ContentValues对象
values = new ContentValues();
//16.将想要插入内容填入values中,调用put方法,其中参数1:表中的字段名,参数2:记录的内容
values.put("amount","20");
values.put("price","1");
//13.调用insert方法进行数据的插入,由于需要参数1:Uri,参数2:ContentValues对象,进行14和15/16
resolver.insert(uri, values);
uri = Uri.parse("content://com.example.vegetable/lizi");
values = new ContentValues();
values.put("amount","200");
values.put("price","10");
resolver.insert(uri,values);
uri = Uri.parse("content://com.example.vegetable/taozi");
values = new ContentValues();
values.put("amount","10");
values.put("price","12");
resolver.insert(uri,values);*/
/*//查询内容
uri = Uri.parse("content://com.example.vegetable/lizi");
System.out.println("获得lizi的链接");
//本句话执行时会进入到提供者的query方法中,
//参数2:为查询时可以显示的内容,如果参数设置为自由amount,当执行cursor.getString()方法是,其中的参数不能为1,为1时不能打印内容-
//参数3:为选择语句
//参数4:为问号位置的内容--筛选条件
cursor = resolver.query(uri, new String[]{"amount","price"}, "amount=?", new String[]{"200"}, null);
System.out.println("执行了query语句");
if(cursor != null) {
while (cursor.moveToNext()) {
String amount = cursor.getString(0);
String price = cursor.getString(1);
System.out.println(amount+"---"+price);
}
}*/
/*//删除内容
uri = Uri.parse("content://com.example.vegetable/taozi");
System.out.println("获得taozi的链接");
resolver.delete(uri,"amount=?",new String[]{"10"});
System.out.println("执行了删除语句");*/
//修改内容
uri = Uri.parse("content://com.example.vegetable/luobo");
values = new ContentValues();
values.put("amount","10");
resolver.update(uri,values,"amount=?",new String[]{"xiaoming"});
}
}