{"id":58365,"date":"2024-10-12T20:09:17","date_gmt":"2024-10-12T12:09:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/58365\/"},"modified":"2024-10-12T20:09:17","modified_gmt":"2024-10-12T12:09:17","slug":"android-studio-%e5%af%a6%e6%88%b0%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e9%96%8b%e7%99%bc%e6%8c%87%e5%8d%97-android-studio-%e6%95%b8%e6%93%9a%e5%ba%ab%e9%96%8b%e7%99%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/58365\/","title":{"rendered":"Android Studio \u5be6\u6230\uff1a\u6578\u64da\u5eab\u958b\u767c\u6307\u5357 (android studio \u6578\u64da\u5eab\u958b\u767c)"},"content":{"rendered":"<h1 id=\"android-studio-%e5%af%a6%e6%88%b0%ef%bc%9a%e6%95%b8%e6%93%9a%e5%ba%ab%e9%96%8b%e7%99%bc%e6%8c%87%e5%8d%97-DTSGOCbdNd\">Android Studio \u5be6\u6230\uff1a\u6578\u64da\u5eab\u958b\u767c\u6307\u5357<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u79fb\u52d5\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Android Studio \u4f5c\u70ba Android \u61c9\u7528\u958b\u767c\u7684\u5b98\u65b9\u96c6\u6210\u958b\u767c\u74b0\u5883\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5de5\u5177\u548c\u5eab\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u9032\u884c\u6578\u64da\u5eab\u7684\u958b\u767c\u548c\u7ba1\u7406\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Android Studio \u4e2d\u9032\u884c\u6578\u64da\u5eab\u958b\u767c\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%81%b8%e6%93%87-DTSGOCbdNd\">\u6578\u64da\u5eab\u7684\u9078\u64c7<\/h2>\n<p>\u5728 Android \u958b\u767c\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6578\u64da\u5eab\u6709\u5169\u7a2e\uff1aSQLite \u548c Room\u3002SQLite \u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u95dc\u4fc2\u578b\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u5c0f\u578b\u61c9\u7528\u3002\u800c Room \u662f Google \u63d0\u4f9b\u7684\u4e00\u500b\u62bd\u8c61\u5c64\uff0c\u65e8\u5728\u7c21\u5316 SQLite \u7684\u4f7f\u7528\uff0c\u4e26\u63d0\u4f9b\u66f4\u597d\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u9ad4\u9a57\u3002<\/p>\n<h3 id=\"sqlite-%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8-DTSGOCbdNd\">SQLite \u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u5728 Android Studio \u4e2d\u4f7f\u7528 SQLite\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u548c\u64cd\u4f5c SQLite \u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\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 createTable = \"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)\";\n        db.execSQL(createTable);\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\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba `users` \u7684\u8868\uff0c\u4e26\u5b9a\u7fa9\u4e86\u4e09\u500b\u5b57\u6bb5\uff1a`id`\u3001`name` \u548c `age`\u3002<\/p>\n<h3 id=\"%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-DTSGOCbdNd\">\u63d2\u5165\u6578\u64da<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u63d2\u5165\u6578\u64da\uff1a<\/p>\n<pre><code>public void addUser(String name, int age) {\n    SQLiteDatabase db = this.getWritableDatabase();\n    ContentValues values = new ContentValues();\n    values.put(\"name\", name);\n    values.put(\"age\", age);\n    db.insert(\"users\", null, values);\n    db.close();\n}\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8-room-%e6%95%b8%e6%93%9a%e5%ba%ab-DTSGOCbdNd\">\u4f7f\u7528 Room \u6578\u64da\u5eab<\/h2>\n<p>Room \u63d0\u4f9b\u4e86\u4e00\u500b\u66f4\u7c21\u55ae\u7684\u65b9\u5f0f\u4f86\u8655\u7406\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u6dfb\u52a0 Room \u7684\u4f9d\u8cf4\uff1a<\/p>\n<pre><code>implementation \"androidx.room:room-runtime:2.4.2\"\nannotationProcessor \"androidx.room:room-compiler:2.4.2\"<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b\u5be6\u9ad4\u985e\uff1a<\/p>\n<pre><code>@Entity(tableName = \"users\")\npublic class User {\n    @PrimaryKey(autoGenerate = true)\n    public int id;\n\n    @ColumnInfo(name = \"name\")\n    public String name;\n\n    @ColumnInfo(name = \"age\")\n    public int age;\n}\n<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u8a2a\u554f\u5c0d\u8c61\uff08DAO\uff09\uff1a<\/p>\n<pre><code>@Dao\npublic interface UserDao {\n    @Insert\n    void insert(User user);\n\n    @Query(\"SELECT * FROM users\")\n    List getAllUsers();\n}\n<\/code><\/pre>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u6578\u64da\u5eab\u985e\uff1a<\/p>\n<pre><code>@Database(entities = {User.class}, version = 1)\npublic abstract class AppDatabase extends RoomDatabase {\n    public abstract UserDao userDao();\n}\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-DTSGOCbdNd\">\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li>\u4f7f\u7528\u7570\u6b65\u64cd\u4f5c\uff1a\u907f\u514d\u5728\u4e3b\u7dda\u7a0b\u4e2d\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li>\u6578\u64da\u5eab\u7248\u672c\u7ba1\u7406\uff1a\u5728\u6578\u64da\u5eab\u7d50\u69cb\u8b8a\u66f4\u6642\uff0c\u8a18\u5f97\u66f4\u65b0\u7248\u672c\u865f\u4e26\u8655\u7406\u6578\u64da\u9077\u79fb\u3002<\/li>\n<li>\u4f7f\u7528 LiveData\uff1a\u7d50\u5408 Room \u548c LiveData\uff0c\u53ef\u4ee5\u5be6\u73fe\u6578\u64da\u7684\u81ea\u52d5\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-DTSGOCbdNd\">\u7e3d\u7d50<\/h2>\n<p>\u5728 Android Studio \u4e2d\u9032\u884c\u6578\u64da\u5eab\u958b\u767c\u662f\u4e00\u500b\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7121\u8ad6\u662f\u4f7f\u7528 SQLite \u9084\u662f Room\uff0c\u90fd\u80fd\u5920\u6709\u6548\u5730\u7ba1\u7406\u61c9\u7528\u4e2d\u7684\u6578\u64da\u3002\u900f\u904e\u672c\u6587\u7684\u4ecb\u7d39\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9078\u64c7\u6700\u9069\u5408\u81ea\u5df1\u9700\u6c42\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u4e26\u638c\u63e1\u57fa\u672c\u7684\u64cd\u4f5c\u6280\u5de7\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u6280\u8853\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u638c\u63e1 Android Studio \u6578\u64da\u5eab\u958b\u767c\u7684\u5be6\u6230\u6280\u5de7\uff0c\u5f9e\u8a2d\u8a08\u5230\u5be6\u73fe\uff0c\u5168\u9762\u63d0\u5347\u4f60\u7684\u61c9\u7528\u958b\u767c\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-58365","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58365","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=58365"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58365\/revisions"}],"predecessor-version":[{"id":58366,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/58365\/revisions\/58366"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=58365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=58365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=58365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}