创建私有数据库
package com.lidaochen.test001;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "lidaochen.db", null, 1); } // 表结构的初始化 @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),money varchar(20))"); db.execSQL("insert into info(name,money) values(?,?)", new String[]{"张三", "5000"}); db.execSQL("insert into info(name,money) values(?,?)", new String[]{"李四", "3000"}); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
package com.lidaochen.test001;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;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); MyOpenHelper myOpenHelper = new MyOpenHelper(getApplicationContext()); // 获取数据库对象 SQLiteDatabase db = myOpenHelper.getReadableDatabase(); Cursor cursor = db.query("info", null, null, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String name = cursor.getString(1); String phone = cursor.getString(2); System.out.println("name:" + name + "--------" + phone); } } }}
package com.lidaochen.test001;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.widget.TableRow;public class AccountProvider extends ContentProvider { private MyOpenHelper myOpenHelper; private static final int QUERYSUCCESS = 0; private static final int INSERTSUCCESS = 1; private static final int UPDATESUCCESS = 2; private static final int DELETESUCCESS = 3; // 定义路径匹配器 private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); // 定义静态代码块 添加匹配原则 static { sURIMatcher.addURI("com.lidaochen.provider", "query", QUERYSUCCESS); sURIMatcher.addURI("com.lidaochen.provider", "insert", INSERTSUCCESS); sURIMatcher.addURI("com.lidaochen.provider", "update", UPDATESUCCESS); sURIMatcher.addURI("com.lidaochen.provider", "delete", DELETESUCCESS); } public AccountProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int code = sURIMatcher.match(uri); if (code == DELETESUCCESS) { SQLiteDatabase db =myOpenHelper.getReadableDatabase(); // 代表影响的行数 int delete = db.delete("info", selection, selectionArgs); // 关闭数据库 db.close(); if (delete > 0) { getContext().getContentResolver().notifyChange(uri, null); } return delete; } else { throw new IllegalArgumentException("您的路径不匹配,请检查路径!"); } } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) { int code = sURIMatcher.match(uri); if (code == INSERTSUCCESS) { // 获取数据库对象 SQLiteDatabase db = myOpenHelper.getReadableDatabase(); // 返回值代表新插入行数的ID long insert = db.insert("info", null, values); // 关闭数据库 db.close(); if (insert > 0) { // 发送一条消息 说明数据库发生了改变 getContext().getContentResolver().notifyChange(uri, null); } Uri uri2 = Uri.parse("com.lidaochen.insert/" + insert); return uri2; } else { throw new IllegalArgumentException("您的路径不匹配,请检查路径!"); } } @Override public boolean onCreate() { myOpenHelper = new MyOpenHelper(getContext()); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { int code = sURIMatcher.match(uri); if (code == QUERYSUCCESS) { // 说明路径匹配成功 把query方法给实现 数据库的查询方法 对数据库进行查询的操作 // 获取数据库对象 SQLiteDatabase db = myOpenHelper.getReadableDatabase(); // 这里需要注意 cursor 不能关闭 Cursor cursor = db.query("info", projection, selection ,selectionArgs, null, null, sortOrder); // 数据库被人操作了 自己发送一条信息 getContext().getContentResolver().notifyChange(uri, null); return cursor; } else { throw new IllegalArgumentException("您的路径不匹配,请检查路径!"); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int code = sURIMatcher.match(uri); if (code == UPDATESUCCESS) { SQLiteDatabase db = myOpenHelper.getReadableDatabase(); int update = db.update("info", values, selection, selectionArgs); // 关闭数据库 db.close(); if (update > 0) { getContext().getContentResolver().notifyChange(uri, null); } return update; } else { throw new IllegalArgumentException("您的路径不匹配,请检查路径!"); } }}
读取上面应用创建的数据库
package com.lidaochen.test002;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 对数据库进行增加一条记录 public void click1(View v) { Uri uri = Uri.parse("content://com.lidaochen.provider/insert"); ContentValues values = new ContentValues(); values.put("name", "王五"); values.put("money", 1000); Uri insert = getContentResolver().insert(uri, values); System.out.println("insert:" + insert); } // 对数据库进行删除一条记录 public void click2(View v) { Uri uri = Uri.parse("content://com.lidaochen.provider/delete"); int delete = getContentResolver().delete(uri, "name=?", new String[]{"王五"}); Toast.makeText(getApplicationContext(), "删除了第" + delete + "行", Toast.LENGTH_SHORT).show(); } // 对数据库进行修改一条记录 public void click3(View v) { Uri uri = Uri.parse("content://com.lidaochen.provider/update"); ContentValues values = new ContentValues(); values.put("money", 999); int update = getContentResolver().update(uri, values, "name=?", new String[]{"李四"}); Toast.makeText(getApplicationContext(), "更新了第" + update + "行", Toast.LENGTH_SHORT).show(); } // 对数据库进行查找一条记录 public void click4(View v) { // 由于 第一个应用里面的私有数据库已经通过内容提供者给暴露出来了 所以可以直接通过内容的解析者进行访问 // 通过上下文获取内容的解析者 // 路径和你定义的路径是一样的 Uri uri = Uri.parse("content://com.lidaochen.provider/query"); Cursor cursor = getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { while (cursor.moveToNext()) { String name = cursor.getString(1); String phone = cursor.getString(2); System.out.println("第二个应用name: " + name + "------" + phone); } } }}