{"id":181946,"date":"2024-11-09T19:49:27","date_gmt":"2024-11-09T11:49:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/181946\/"},"modified":"2024-11-09T19:49:27","modified_gmt":"2024-11-09T11:49:27","slug":"%e6%b7%b1%e5%85%a5%e6%8e%a2%e7%b4%a2android%e6%95%b8%e6%93%9a%e5%ba%ab%e6%ba%90%e7%a2%bc%e8%a7%a3%e6%9e%90%e8%83%8c%e5%be%8c%e7%9a%84%e6%8a%80%e8%a1%93%e5%a5%a7%e7%a7%98%ef%bc%88android%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/181946\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22Android\u6578\u64da\u5eab\u6e90\u78bc\u89e3\u6790\u80cc\u5f8c\u7684\u6280\u8853\u5967\u79d8\uff08android\u6578\u64da\u5eab\u6e90\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e6%8e%a2%e7%b4%a2android%e6%95%b8%e6%93%9a%e5%ba%ab%e6%ba%90%e7%a2%bc%e8%a7%a3%e6%9e%90%e8%83%8c%e5%be%8c%e7%9a%84%e6%8a%80%e8%a1%93%e5%a5%a7%e7%a7%98-YFmJNzidZl\">\u6df1\u5165\u63a2\u7d22Android\u6578\u64da\u5eab\u6e90\u78bc\u89e3\u6790\u80cc\u5f8c\u7684\u6280\u8853\u5967\u79d8<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Android\u4f5c\u70ba\u4e00\u500b\u958b\u653e\u6e90\u78bc\u7684\u64cd\u4f5c\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u4fbf\u662fSQLite\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eAndroid\u6578\u64da\u5eab\u7684\u6e90\u78bc\uff0c\u89e3\u6790\u5176\u80cc\u5f8c\u7684\u6280\u8853\u5967\u79d8\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"android%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%9e%b6%e6%a7%8b-YFmJNzidZl\">Android\u6578\u64da\u5eab\u7684\u57fa\u672c\u67b6\u69cb<\/h2>\n<p>Android\u7684\u6578\u64da\u5eab\u4e3b\u8981\u57fa\u65bcSQLite\uff0c\u9019\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u79fb\u52d5\u8a2d\u5099\u3002SQLite\u7684\u6e90\u78bc\u662f\u958b\u653e\u7684\uff0c\u9019\u610f\u5473\u8457\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u7531\u5730\u67e5\u770b\u548c\u4fee\u6539\u5176\u4ee3\u78bc\u3002Android\u7cfb\u7d71\u4e2d\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u4e3b\u8981\u901a\u904e\u4ee5\u4e0b\u5e7e\u500b\u985e\u4f86\u5be6\u73fe\uff1a<\/p>\n<ul>\n<li><code>SQLiteOpenHelper<\/code>\uff1a\u9019\u662f\u4e00\u500b\u8f14\u52a9\u985e\uff0c\u7528\u65bc\u7ba1\u7406\u6578\u64da\u5eab\u7684\u5275\u5efa\u548c\u7248\u672c\u7ba1\u7406\u3002<\/li>\n<li><code>SQLiteDatabase<\/code>\uff1a\u9019\u500b\u985e\u63d0\u4f9b\u4e86\u5c0d\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u63a5\u53e3\uff0c\u5305\u62ec\u589e\u522a\u6539\u67e5\u7b49\u57fa\u672c\u64cd\u4f5c\u3002<\/li>\n<li><code>ContentProvider<\/code>\uff1a\u9019\u662f\u4e00\u500b\u7528\u65bc\u5728\u4e0d\u540c\u61c9\u7528\u4e4b\u9593\u5171\u4eab\u6578\u64da\u7684\u7d44\u4ef6\u3002<\/li>\n<\/ul>\n<h2 id=\"sqliteopenhelper%e7%9a%84%e4%bd%bf%e7%94%a8-YFmJNzidZl\">SQLiteOpenHelper\u7684\u4f7f\u7528<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u4f7f\u7528<code>SQLiteOpenHelper<\/code>\u4f86\u5275\u5efa\u548c\u7ba1\u7406\u6578\u64da\u5eab\u662f\u975e\u5e38\u5e38\u898b\u7684\u505a\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>public class MyDatabaseHelper extends SQLiteOpenHelper {\n    private static final String DATABASE_NAME = \"mydatabase.db\";\n    private static final int DATABASE_VERSION = 1;\n\n    public MyDatabaseHelper(Context context) {\n        super(context, DATABASE_NAME, null, DATABASE_VERSION);\n    }\n\n    @Override\n    public void onCreate(SQLiteDatabase db) {\n        String CREATE_TABLE = \"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)\";\n        db.execSQL(CREATE_TABLE);\n    }\n\n    @Override\n    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n        db.execSQL(\"DROP TABLE IF EXISTS users\");\n        onCreate(db);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba<code>users<\/code>\u7684\u8868\uff0c\u4e26\u5b9a\u7fa9\u4e86\u5176\u7d50\u69cb\u3002\u7576\u6578\u64da\u5eab\u7248\u672c\u5347\u7d1a\u6642\uff0c<code>onUpgrade<\/code>\u65b9\u6cd5\u6703\u88ab\u8abf\u7528\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7d50\u69cb\u59cb\u7d42\u662f\u6700\u65b0\u7684\u3002<\/p>\n<h2 id=\"sqlitedatabase%e7%9a%84%e6%93%8d%e4%bd%9c-YFmJNzidZl\">SQLiteDatabase\u7684\u64cd\u4f5c<\/h2>\n<p>\u4e00\u65e6\u6578\u64da\u5eab\u88ab\u5275\u5efa\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u4f7f\u7528<code>SQLiteDatabase<\/code>\u4f86\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<pre><code>SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();\n\n\/\/ \u63d2\u5165\u6578\u64da\nContentValues values = new ContentValues();\nvalues.put(\"name\", \"John Doe\");\ndb.insert(\"users\", null, values);\n\n\/\/ \u67e5\u8a62\u6578\u64da\nCursor cursor = db.query(\"users\", null, null, null, null, null, null);\nif (cursor.moveToFirst()) {\n    do {\n        String name = cursor.getString(cursor.getColumnIndex(\"name\"));\n        Log.d(\"User\", name);\n    } while (cursor.moveToNext());\n}\ncursor.close();\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5c55\u793a\u4e86\u5982\u4f55\u63d2\u5165\u548c\u67e5\u8a62\u6578\u64da\u3002\u4f7f\u7528<code>ContentValues<\/code>\u4f86\u5c01\u88dd\u8981\u63d2\u5165\u7684\u6578\u64da\uff0c\u4e26\u4f7f\u7528<code>Cursor<\/code>\u4f86\u904d\u6b77\u67e5\u8a62\u7d50\u679c\u3002<\/p>\n<h2 id=\"contentprovider%e7%9a%84%e6%87%89%e7%94%a8-YFmJNzidZl\">ContentProvider\u7684\u61c9\u7528<\/h2>\n<p>\u7576\u9700\u8981\u5728\u4e0d\u540c\u7684\u61c9\u7528\u4e4b\u9593\u5171\u4eab\u6578\u64da\u6642\uff0c<code>ContentProvider<\/code>\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u7d71\u4e00\u7684\u63a5\u53e3\u4f86\u8a2a\u554f\u6578\u64da\uff0c\u7121\u8ad6\u6578\u64da\u5b58\u5132\u5728\u4f55\u8655\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684<code>ContentProvider<\/code>\u793a\u4f8b\uff1a<\/p>\n<pre><code>public class MyContentProvider extends ContentProvider {\n    private static final String AUTHORITY = \"com.example.myapp.provider\";\n    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);\n\n    static {\n        uriMatcher.addURI(AUTHORITY, \"users\", 1);\n    }\n\n    @Override\n    public boolean onCreate() {\n        myDatabaseHelper = new MyDatabaseHelper(getContext());\n        return true;\n    }\n\n    @Override\n    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {\n        SQLiteDatabase db = myDatabaseHelper.getReadableDatabase();\n        return db.query(\"users\", projection, selection, selectionArgs, null, null, sortOrder);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b<code>ContentProvider<\/code>\uff0c\u4e26\u5be6\u73fe\u4e86<code>query<\/code>\u65b9\u6cd5\u4f86\u67e5\u8a62\u6578\u64da\u3002\u9019\u6a23\uff0c\u5176\u4ed6\u61c9\u7528\u5c31\u53ef\u4ee5\u901a\u904e\u9019\u500b<code>ContentProvider<\/code>\u4f86\u8a2a\u554f<code>users<\/code>\u8868\u4e2d\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YFmJNzidZl\">\u7e3d\u7d50<\/h2>\n<p>Android\u6578\u64da\u5eab\u7684\u6e90\u78bc\u89e3\u6790\u63ed\u793a\u4e86\u5176\u80cc\u5f8c\u7684\u6280\u8853\u5967\u79d8\uff0c\u5f9e<code>SQLiteOpenHelper<\/code>\u5230<code>ContentProvider<\/code>\uff0c\u6bcf\u4e00\u500b\u7d44\u4ef6\u90fd\u5728\u6578\u64da\u7ba1\u7406\u4e2d\u626e\u6f14\u8457\u91cd\u8981\u7684\u89d2\u8272\u3002\u7406\u89e3\u9019\u4e9b\u7d44\u4ef6\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u61c9\u7528\u4e2d\u7684\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">VPS<\/a>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002\u7121\u8ad6\u662f\u9078\u64c7<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u9084\u662f\u5176\u4ed6\u985e\u578b\u7684<a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0cServer.HK\u90fd\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u7d22Android\u6578\u64da\u5eab\u6e90\u78bc\u89e3\u6790\uff0c\u63ed\u793a\u5176\u80cc\u5f8c\u7684\u6280\u8853\u5967\u79d8\uff0c\u52a9\u4f60\u7406\u89e3\u6578\u64da\u5b58\u5132\u8207\u7ba1\u7406\u7684\u6838\u5fc3\u539f\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-181946","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/181946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=181946"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/181946\/revisions"}],"predecessor-version":[{"id":181947,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/181946\/revisions\/181947"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=181946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=181946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=181946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}