建立私有數據庫android
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("您的路徑不匹配,請檢查路徑!"); } } }
讀取上面應用建立的數據庫sql
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); } } } }