{"id":58399,"date":"2024-10-12T20:23:14","date_gmt":"2024-10-12T12:23:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58399\/"},"modified":"2024-10-12T20:23:14","modified_gmt":"2024-10-12T12:23:14","slug":"android%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e5%af%a6%e7%8f%be%e4%b8%a6%e7%99%bc%e8%ab%8b%e6%b1%82-android-%e4%b8%a6%e7%99%bc%e8%ab%8b%e6%b1%82%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58399\/","title":{"rendered":"Android\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u5be6\u73fe\u4e26\u767c\u8acb\u6c42 (android \u4e26\u767c\u8acb\u6c42\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"android%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e5%af%a6%e7%8f%be%e4%b8%a6%e7%99%bc%e8%ab%8b%e6%b1%82-GKnjnFNHfG\">Android\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u5be6\u73fe\u4e26\u767c\u8acb\u6c42<\/h1>\n<p>\u5728\u73fe\u4ee3\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u5f80\u5f80\u9700\u8981\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\u3002\u7279\u5225\u662f\u5728Android\u5e73\u53f0\u4e0a\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0eAndroid\u4e2d\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u7684\u5be6\u73fe\u65b9\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab-GKnjnFNHfG\">\u70ba\u4ec0\u9ebc\u9700\u8981\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab<\/h2>\n<p>\u5728Android\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u64cd\u4f5c\u901a\u5e38\u662f\u8017\u6642\u7684\u4efb\u52d9\u3002\u5982\u679c\u5728\u4e3b\u7dda\u7a0b\u4e2d\u57f7\u884c\u9019\u4e9b\u64cd\u4f5c\uff0c\u5c07\u6703\u5c0e\u81f4\u61c9\u7528\u754c\u9762\u5361\u9813\uff0c\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u591a\u7dda\u7a0b\u4f86\u8655\u7406\u6578\u64da\u5eab\u8acb\u6c42\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u4e3b\u7dda\u7a0b\u4fdd\u6301\u6d41\u66a2\uff0c\u4e26\u4e14\u80fd\u5920\u540c\u6642\u8655\u7406\u591a\u500b\u8acb\u6c42\u3002<\/p>\n<h2 id=\"android%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-GKnjnFNHfG\">Android\u4e2d\u7684\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>Android\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u65b9\u5f0f\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fSQLite\u3002SQLite\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u806f\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u79fb\u52d5\u8a2d\u5099\u3002\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528SQLiteOpenHelper\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u7684\u5275\u5efa\u548c\u7248\u672c\u7ba1\u7406\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8sqliteopenhelper-GKnjnFNHfG\">\u4f7f\u7528SQLiteOpenHelper<\/h3>\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        db.execSQL(\"CREATE TABLE mytable (id INTEGER PRIMARY KEY, name TEXT)\");\n    }\n\n    @Override\n    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n        db.execSQL(\"DROP TABLE IF EXISTS mytable\");\n        onCreate(db);\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e7%8f%be%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-GKnjnFNHfG\">\u5be6\u73fe\u591a\u7dda\u7a0b\u6578\u64da\u5eab\u64cd\u4f5c<\/h2>\n<p>\u5728Android\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528AsyncTask\u3001HandlerThread\u6216Executors\u4f86\u5be6\u73fe\u591a\u7dda\u7a0b\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Executors\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>ExecutorService executorService = Executors.newFixedThreadPool(4);\n\nexecutorService.execute(new Runnable() {\n    @Override\n    public void run() {\n        SQLiteDatabase db = myDatabaseHelper.getWritableDatabase();\n        ContentValues values = new ContentValues();\n        values.put(\"name\", \"John Doe\");\n        db.insert(\"mytable\", null, values);\n        db.close();\n    }\n});\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u56fa\u5b9a\u5927\u5c0f\u7684\u7dda\u7a0b\u6c60\uff0c\u4e26\u5728\u5176\u4e2d\u57f7\u884c\u6578\u64da\u5eab\u63d2\u5165\u64cd\u4f5c\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u591a\u500b\u8acb\u6c42\uff0c\u4e26\u78ba\u4fdd\u6578\u64da\u5eab\u64cd\u4f5c\u4e0d\u6703\u963b\u585e\u4e3b\u7dda\u7a0b\u3002<\/p>\n<h2 id=\"%e8%99%95%e7%90%86%e4%b8%a6%e7%99%bc%e8%ab%8b%e6%b1%82%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-GKnjnFNHfG\">\u8655\u7406\u4e26\u767c\u8acb\u6c42\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li><strong>\u6578\u64da\u5eab\u9396\u5b9a\uff1a<\/strong>\u5728\u9032\u884c\u5beb\u5165\u64cd\u4f5c\u6642\uff0cSQLite\u6703\u81ea\u52d5\u9396\u5b9a\u6578\u64da\u5eab\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u5176\u4ed6\u8acb\u6c42\u88ab\u963b\u585e\u3002\u56e0\u6b64\uff0c\u61c9\u8a72\u76e1\u91cf\u6e1b\u5c11\u5beb\u5165\u64cd\u4f5c\u7684\u6642\u9593\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e8b\u52d9\uff1a<\/strong>\u5728\u9032\u884c\u591a\u500b\u5beb\u5165\u64cd\u4f5c\u6642\uff0c\u4f7f\u7528\u4e8b\u52d9\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u4e26\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u932f\u8aa4\u8655\u7406\u5c24\u70ba\u91cd\u8981\u3002\u61c9\u8a72\u5728\u6bcf\u500b\u7dda\u7a0b\u4e2d\u6355\u7372\u7570\u5e38\uff0c\u4e26\u9032\u884c\u9069\u7576\u7684\u8655\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-GKnjnFNHfG\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u5be6\u73fe\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u4ee5\u8655\u7406\u4e26\u767c\u8acb\u6c42\u662f\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u95dc\u9375\u3002\u901a\u904e\u4f7f\u7528SQLite\u548c\u9069\u7576\u7684\u591a\u7dda\u7a0b\u6280\u8853\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5f9e\u800c\u63d0\u4f9b\u66f4\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\uff0c\u5982<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\uff0c\u4e5f\u80fd\u9032\u4e00\u6b65\u63d0\u5347\u6574\u9ad4\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5728Android\u4e2d\u5be6\u73fe\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u4ee5\u652f\u6301\u4e26\u767c\u8acb\u6c42\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\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-58399","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58399","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=58399"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58399\/revisions"}],"predecessor-version":[{"id":58400,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58399\/revisions\/58400"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}