{"id":58407,"date":"2024-10-12T20:26:42","date_gmt":"2024-10-12T12:26:42","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58407\/"},"modified":"2024-10-12T20:26:42","modified_gmt":"2024-10-12T12:26:42","slug":"android%e9%96%8b%e7%99%bc%ef%bc%9a%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc%ef%bc%9f-android%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58407\/","title":{"rendered":"Android\u958b\u767c\uff1a\u5982\u4f55\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\uff1f (android\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc)"},"content":{"rendered":"<h1 id=\"android%e9%96%8b%e7%99%bc%ef%bc%9a%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc%ef%bc%9f-EZFSahwhdF\">Android\u958b\u767c\uff1a\u5982\u4f55\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\uff1f<\/h1>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u975e\u5e38\u666e\u904d\u7684\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5b58\u5132\u548c\u7ba1\u7406\u5927\u91cf\u6578\u64da\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Android\u61c9\u7528\u4e2d\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"1-%e9%81%b8%e6%93%87%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a1%9e%e5%9e%8b-EZFSahwhdF\">1. \u9078\u64c7\u6578\u64da\u5eab\u985e\u578b<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u9069\u5408\u7684\u6578\u64da\u5eab\u985e\u578b\u3002Android\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>SQLite\uff1a<\/strong>\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u5927\u591a\u6578\u61c9\u7528\u3002<\/li>\n<li><strong>Room\uff1a<\/strong>Android Jetpack\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u500b\u62bd\u8c61\u5c64\u4f86\u7c21\u5316SQLite\u7684\u4f7f\u7528\u3002<\/li>\n<li><strong>Realm\uff1a<\/strong>\u4e00\u500b\u79fb\u52d5\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u6613\u7528\u6027\u3002<\/li>\n<\/ul>\n<p>\u672c\u7bc7\u6587\u7ae0\u5c07\u91cd\u9ede\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SQLite\u548cRoom\u4f86\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8sqlite%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc-EZFSahwhdF\">2. \u4f7f\u7528SQLite\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc<\/h2>\n<h3 id=\"2-1-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e5%b9%ab%e5%8a%a9%e9%a1%9e-EZFSahwhdF\">2.1 \u5275\u5efa\u6578\u64da\u5eab\u5e6b\u52a9\u985e<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u7e7c\u627f\u81eaSQLiteOpenHelper\u7684\u985e\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u7684\u5275\u5efa\u548c\u7248\u672c\u7ba1\u7406\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}<\/code><\/pre>\n<h3 id=\"2-2-%e4%bd%bf%e7%94%a8%e6%95%b8%e6%93%9a%e5%ba%ab-EZFSahwhdF\">2.2 \u4f7f\u7528\u6578\u64da\u5eab<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u9019\u500b\u5e6b\u52a9\u985e\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff1a<\/p>\n<pre><code>MyDatabaseHelper dbHelper = new MyDatabaseHelper(context);\nSQLiteDatabase db = dbHelper.getWritableDatabase();\n\n\/\/ \u63d2\u5165\u6578\u64da\nContentValues values = new ContentValues();\nvalues.put(\"name\", \"John Doe\");\ndb.insert(\"my_table\", null, values);\n\n\/\/ \u67e5\u8a62\u6578\u64da\nCursor cursor = db.query(\"my_table\", null, null, null, null, null, null);\nif (cursor.moveToFirst()) {\n    do {\n        String name = cursor.getString(cursor.getColumnIndex(\"name\"));\n        \/\/ \u8655\u7406\u6578\u64da\n    } while (cursor.moveToNext());\n}\ncursor.close();\ndb.close();<\/code><\/pre>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8room%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bb%a3%e7%a2%bc-EZFSahwhdF\">3. \u4f7f\u7528Room\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc<\/h2>\n<h3 id=\"3-1-%e6%b7%bb%e5%8a%a0%e4%be%9d%e8%b3%b4-EZFSahwhdF\">3.1 \u6dfb\u52a0\u4f9d\u8cf4<\/h3>\n<p>\u9996\u5148\uff0c\u5728\u4f60\u7684build.gradle\u6587\u4ef6\u4e2d\u6dfb\u52a0Room\u7684\u4f9d\u8cf4\uff1a<\/p>\n<pre><code>dependencies {\n    implementation \"androidx.room:room-runtime:2.4.2\"\n    annotationProcessor \"androidx.room:room-compiler:2.4.2\"\n}<\/code><\/pre>\n<h3 id=\"3-2-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%af%a6%e9%ab%94-EZFSahwhdF\">3.2 \u5275\u5efa\u6578\u64da\u5be6\u9ad4<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u6578\u64da\u5be6\u9ad4\u985e\uff1a<\/p>\n<pre><code>@Entity(tableName = \"my_table\")\npublic class User {\n    @PrimaryKey(autoGenerate = true)\n    public int id;\n\n    @ColumnInfo(name = \"name\")\n    public String name;\n}<\/code><\/pre>\n<h3 id=\"3-3-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e8%a8%aa%e5%95%8f%e5%b0%8d%e8%b1%a1%ef%bc%88dao%ef%bc%89-EZFSahwhdF\">3.3 \u5275\u5efa\u6578\u64da\u8a2a\u554f\u5c0d\u8c61\uff08DAO\uff09<\/h3>\n<pre><code>@Dao\npublic interface UserDao {\n    @Insert\n    void insert(User user);\n\n    @Query(\"SELECT * FROM my_table\")\n    List getAllUsers();\n}<\/code><\/pre>\n<h3 id=\"3-4-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%ba%ab%e9%a1%9e-EZFSahwhdF\">3.4 \u5275\u5efa\u6578\u64da\u5eab\u985e<\/h3>\n<pre><code>@Database(entities = {User.class}, version = 1)\npublic abstract class AppDatabase extends RoomDatabase {\n    public abstract UserDao userDao();\n}<\/code><\/pre>\n<h3 id=\"3-5-%e4%bd%bf%e7%94%a8room-EZFSahwhdF\">3.5 \u4f7f\u7528Room<\/h3>\n<pre><code>AppDatabase db = Room.databaseBuilder(getApplicationContext(),\n        AppDatabase.class, \"mydatabase\").build();\n\nUser user = new User();\nuser.name = \"John Doe\";\ndb.userDao().insert(user);\n\nList users = db.userDao().getAllUsers();<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EZFSahwhdF\">\u7e3d\u7d50<\/h2>\n<p>\u5728Android\u958b\u767c\u4e2d\uff0c\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\u662f\u7ba1\u7406\u61c9\u7528\u6578\u64da\u7684\u91cd\u8981\u6b65\u9a5f\u3002\u7121\u8ad6\u662f\u4f7f\u7528SQLite\u9084\u662fRoom\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u3002\u900f\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u5e0c\u671b\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Android\u61c9\u7528\u4e2d\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\u3002<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u958b\u767c\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\u3002\u6211\u5011\u63d0\u4f9b\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728Android\u958b\u767c\u4e2d\u6dfb\u52a0\u6578\u64da\u5eab\u4ee3\u78bc\uff0c\u638c\u63e1SQLite\u7684\u4f7f\u7528\uff0c\u63d0\u5347\u61c9\u7528\u6578\u64da\u7ba1\u7406\u80fd\u529b\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-58407","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58407","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=58407"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58407\/revisions"}],"predecessor-version":[{"id":58408,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58407\/revisions\/58408"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}