{"id":58385,"date":"2024-10-12T20:17:20","date_gmt":"2024-10-12T12:17:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58385\/"},"modified":"2024-10-12T20:17:20","modified_gmt":"2024-10-12T12:17:20","slug":"android%ef%bc%9a%e5%8f%af%e5%90%a6%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%84%b2%e5%a4%a7%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-android-%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%84%b2%e5%a4%a7%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58385\/","title":{"rendered":"Android\uff1a\u53ef\u5426\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\uff1f (android \u672c\u5730\u5b58\u5132\u5927\u6578\u64da\u5eab\u55ce)"},"content":{"rendered":"<h1 id=\"android%ef%bc%9a%e5%8f%af%e5%90%a6%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%84%b2%e5%a4%a7%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-jjlayFkVMY\">Android\uff1a\u53ef\u5426\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\uff1f<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5b58\u5132\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u8b70\u984c\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u65e5\u76ca\u589e\u5f37\uff0c\u958b\u767c\u8005\u9762\u81e8\u8457\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u5132\u5927\u91cf\u6578\u64da\u7684\u6311\u6230\u3002\u7279\u5225\u662f\u5728Android\u5e73\u53f0\u4e0a\uff0c\u958b\u767c\u8005\u5e38\u5e38\u9700\u8981\u8003\u616e\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\u7684\u53ef\u884c\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0eAndroid\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\u7684\u53ef\u80fd\u6027\u53ca\u5176\u76f8\u95dc\u6280\u8853\u3002<\/p>\n<h2 id=\"android%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%84%b2%e7%9a%84%e9%81%b8%e6%93%87-jjlayFkVMY\">Android\u672c\u5730\u5b58\u5132\u7684\u9078\u64c7<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u591a\u7a2e\u65b9\u5f0f\u4f86\u9032\u884c\u672c\u5730\u6578\u64da\u5b58\u5132\uff0c\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u7a2e\uff1a<\/p>\n<ul>\n<li><strong>SharedPreferences\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u5c0f\u578b\u6578\u64da\uff0c\u5982\u7528\u6236\u8a2d\u7f6e\u548c\u504f\u597d\u3002<\/li>\n<li><strong>SQLite\u6578\u64da\u5eab\uff1a<\/strong>\u9069\u5408\u5b58\u5132\u7d50\u69cb\u5316\u6578\u64da\uff0c\u652f\u6301\u8907\u96dc\u67e5\u8a62\u548c\u4e8b\u52d9\u8655\u7406\u3002<\/li>\n<li><strong>\u6587\u4ef6\u7cfb\u7d71\uff1a<\/strong>\u53ef\u4ee5\u7528\u65bc\u5b58\u5132\u5927\u578b\u6587\u4ef6\u6216\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002<\/li>\n<li><strong>Room\u6301\u4e45\u5316\u5eab\uff1a<\/strong>\u662f\u5c0dSQLite\u7684\u5c01\u88dd\uff0c\u63d0\u4f9b\u4e86\u66f4\u7c21\u55ae\u7684API\u548c\u6578\u64da\u5eab\u7248\u672c\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"sqlite%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%bd%bf%e7%94%a8-jjlayFkVMY\">SQLite\u6578\u64da\u5eab\u7684\u4f7f\u7528<\/h2>\n<p>SQLite\u662fAndroid\u4e2d\u6700\u5e38\u7528\u7684\u672c\u5730\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u3002\u5b83\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u5b58\u5132\u7d50\u69cb\u5316\u6578\u64da\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528SQLite\u4f86\u5275\u5efa\u3001\u8b80\u53d6\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Android\u4e2d\u4f7f\u7528SQLite\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 my_table (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 my_table\");\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\u201cmy_table\u201d\u7684\u8868\uff0c\u4e26\u5b9a\u7fa9\u4e86\u5169\u500b\u5b57\u6bb5\uff1aid\u548cname\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u64f4\u5c55\u9019\u500b\u8868\u7684\u7d50\u69cb\u3002<\/p>\n<h2 id=\"room%e6%8c%81%e4%b9%85%e5%8c%96%e5%ba%ab%e7%9a%84%e5%84%aa%e5%8b%a2-jjlayFkVMY\">Room\u6301\u4e45\u5316\u5eab\u7684\u512a\u52e2<\/h2>\n<p>Room\u662fGoogle\u63a8\u51fa\u7684\u4e00\u500b\u6301\u4e45\u5316\u5eab\uff0c\u65e8\u5728\u7c21\u5316SQLite\u7684\u4f7f\u7528\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u500b\u62bd\u8c61\u5c64\uff0c\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u7c21\u55ae\u548c\u5b89\u5168\u3002\u4f7f\u7528Room\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u7c21\u5316\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff1aRoom\u63d0\u4f9b\u4e86\u8a3b\u89e3\u4f86\u5b9a\u7fa9\u6578\u64da\u5be6\u9ad4\u548cDAO\uff08\u6578\u64da\u8a2a\u554f\u5c0d\u8c61\uff09\uff0c\u6e1b\u5c11\u4e86\u6a23\u677f\u4ee3\u78bc\u3002<\/li>\n<li>\u7de8\u8b6f\u6642\u6aa2\u67e5\uff1aRoom\u5728\u7de8\u8b6f\u6642\u6aa2\u67e5SQL\u67e5\u8a62\u7684\u6b63\u78ba\u6027\uff0c\u6e1b\u5c11\u4e86\u904b\u884c\u6642\u932f\u8aa4\u3002<\/li>\n<li>\u652f\u6301LiveData\u548cRxJava\uff1aRoom\u53ef\u4ee5\u8207LiveData\u548cRxJava\u7121\u7e2b\u96c6\u6210\uff0c\u5be6\u73fe\u6578\u64da\u7684\u5be6\u6642\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a4%a7%e5%9e%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%8c%91%e6%88%b0-jjlayFkVMY\">\u5927\u578b\u6578\u64da\u5eab\u7684\u6311\u6230<\/h2>\n<p>\u5118\u7ba1Android\u652f\u6301\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\uff0c\u4f46\u958b\u767c\u8005\u4ecd\u9700\u9762\u5c0d\u4e00\u4e9b\u6311\u6230\u3002\u4f8b\u5982\uff0c\u6578\u64da\u5eab\u7684\u5927\u5c0f\u53ef\u80fd\u6703\u5f71\u97ff\u61c9\u7528\u7684\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u67e5\u8a62\u548c\u66f4\u65b0\u64cd\u4f5c\u6642\u3002\u6b64\u5916\uff0c\u6578\u64da\u7684\u5099\u4efd\u548c\u6062\u5fa9\u4e5f\u662f\u4e00\u500b\u9700\u8981\u8003\u616e\u7684\u554f\u984c\u3002\u958b\u767c\u8005\u61c9\u8a72\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\uff0c\u5408\u7406\u8a2d\u8a08\u6578\u64da\u5eab\u7d50\u69cb\uff0c\u4e26\u8003\u616e\u4f7f\u7528\u7d22\u5f15\u4f86\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-jjlayFkVMY\">\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0cAndroid\u5e73\u53f0\u78ba\u5be6\u53ef\u4ee5\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\uff0c\u4e3b\u8981\u901a\u904eSQLite\u548cRoom\u7b49\u6280\u8853\u4f86\u5be6\u73fe\u3002\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u61c9\u7528\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u5b58\u5132\u65b9\u6848\uff0c\u4e26\u8003\u616e\u6027\u80fd\u548c\u6578\u64da\u7ba1\u7406\u7684\u6311\u6230\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0cAndroid\u7684\u6578\u64da\u5b58\u5132\u80fd\u529b\u5c07\u6703\u66f4\u52a0\u5f37\u5927\uff0c\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u66f4\u591a\u7684\u9078\u64c7\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728Android\u4e0a\u9032\u884c\u6578\u64da\u5b58\u5132\u6709\u9032\u4e00\u6b65\u7684\u8208\u8da3\uff0c\u6216\u8005\u9700\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code><a href=\"https:\/\/server.hk\">VPS<\/a><\/code>\u548c<code><a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a><\/code>\u7684\u8cc7\u8a0a\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Android\u672c\u5730\u5b58\u5132\u5927\u578b\u6578\u64da\u5eab\u7684\u53ef\u80fd\u6027\uff0c\u4e86\u89e3\u6700\u4f73\u5be6\u8e10\u548c\u6280\u8853\u9078\u64c7\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-58385","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58385","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=58385"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58385\/revisions"}],"predecessor-version":[{"id":58386,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58385\/revisions\/58386"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}