{"id":10252,"date":"2023-09-27T16:32:09","date_gmt":"2023-09-27T08:32:09","guid":{"rendered":"https:\/\/app.60day.cn\/appday\/?p=10252"},"modified":"2023-09-27T16:32:09","modified_gmt":"2023-09-27T08:32:09","slug":"%e5%ae%89%e5%8d%93%e6%95%b0%e6%8d%ae%e5%ba%93%e5%b7%a5%e5%85%b7%e7%b1%bb%e5%b0%81%e8%a3%85%e5%95%a5%e6%84%8f%e6%80%9d%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.60day.cn\/appday\/kaifaapp\/10252\/","title":{"rendered":"\u5b89\u5353\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u5c01\u88c5\u5565\u610f\u601d\uff1f"},"content":{"rendered":"<p>\u5728Android\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5b58\u50a8\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u90e8\u5206\uff0c\u5176\u4e2d\u6570\u636e\u5e93\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\u4e4b\u4e00\u3002\u4f7f\u7528\u6570\u636e\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6570\u636e\u67e5\u8be2\u548c\u6301\u4e45\u5316\u7684\u529f\u80fd\u3002\u4e3a\u4e86\u65b9\u4fbf\u5f00\u53d1\u8005\u4f7f\u7528\u6570\u636e\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u5c01\u88c5\u4e00\u4e2a\u6570\u636e\u5e93\u5de5\u5177\u7c7b\uff0c\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u6d41\u7a0b\u3002<\/p>\n<p>\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u7684\u5c01\u88c5\u53ef\u4ee5\u9075\u5faa\u5355\u4f8b\u6a21\u5f0f\u7684\u8bbe\u8ba1\u539f\u5219\uff0c\u786e\u4fdd\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u907f\u514d\u591a\u4e2a\u6570\u636e\u5e93\u5b9e\u4f8b\u5f15\u8d77\u7684\u8d44\u6e90\u6d6a\u8d39\u548c\u51b2\u7a81\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\u7684\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u7684\u5c01\u88c5\u5b9e\u73b0\uff1a<\/p>\n<p>&#8220;`java<\/p>\n<p>public class DatabaseHelper extends SQLiteOpenHelper {<\/p>\n<p>    private static final String DB_NAME = &#8220;my_database.db&#8221;;<\/p>\n<p>    private static final int DB_VERSION = 1;<\/p>\n<p>    private static volatile DatabaseHelper instance;<\/p>\n<p>    private DatabaseHelper(Context context) {<\/p>\n<p>        super(context, DB_NAME, null, DB_VERSION);<\/p>\n<p>    }<\/p>\n<p>    public synchronized static DatabaseHelper getInstance(Context context) {<\/p>\n<p>        if (instance == null) {<\/p>\n<p>            instance = new DatabaseHelper(context);<\/p>\n<p>        }<\/p>\n<p>        return instance;<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public void onCreate(SQLiteDatabase db) {<\/p>\n<p>        \/\/ \u521b\u5efa\u8868\u683c\u7684SQL\u8bed\u53e5<\/p>\n<p>        String createTableSQL = &#8220;CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)&#8221;;<\/p>\n<p>        db.execSQL(createTableSQL);<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<\/p>\n<p>        \/\/ \u6570\u636e\u5e93\u7248\u672c\u5347\u7ea7\u65f6\u9700\u8981\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8868\u683c\u7ed3\u6784\u7684\u4fee\u6539<\/p>\n<p>        \/\/ \u4e00\u822c\u60c5\u51b5\u4e0b\u53ef\u4ee5\u76f4\u63a5\u5220\u9664\u8868\u683c\uff0c\u7136\u540e\u8c03\u7528onCreate\u65b9\u6cd5\u521b\u5efa\u65b0\u8868\u683c<\/p>\n<p>        String dropTableSQL = &#8220;DROP TABLE IF EXISTS user&#8221;;<\/p>\n<p>        db.execSQL(dropTableSQL);<\/p>\n<p>        onCreate(db);<\/p>\n<p>    }<\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/ag\/99\/tupian\/22.jpg\" \/><\/figure>\n<\/p>\n<p>    public void insertUser(User user) {<\/p>\n<p>        SQLiteDatabase db = getWritableDatabase();<\/p>\n<p>        ContentValues values = new ContentValues();<\/p>\n<p>        values.put(&#8220;name&#8221;, user.getName());<\/p>\n<p>        values.put(&#8220;age&#8221;, user.getAge());<\/p>\n<p>        db.insert(&#8220;user&#8221;, null, values);<\/p>\n<p>        db.close();<\/p>\n<p>    }<\/p>\n<p>    public List getAllUsers() {<\/p>\n<p><p>        List userList = new ArrayList();<\/p>\n<p><p>   <a href=\"https:\/\/www.yimenyun.ltd\/app-fengzhuang-gongju-146.html\">\u7f51\u9875\u5c01\u88c5\u5de5\u5177\u5b89\u5353\u7248<\/a>     SQLiteDat<a href=\"https:\/\/www.yimenyun.ltd\/app-fengzhuang-gongju-76.html\">app\u5c01\u88c5\u5de5\u5177\u5b89\u5353<\/a>abase db = getReadableDatabase();<\/p>\n<p>        Cursor cursor = db.query(&#8220;user&#8221;, null, null, null, null, null, null);<\/p>\n<p>        if (cursor != null &amp;&amp; cursor.moveToFirst()) {<\/p>\n<p>            do {<\/p>\n<p>                int id = cursor.getInt(cursor.getColumnIndex(&#8220;id&#8221;));<\/p>\n<p>                String name = cursor.getString(cursor.getColumnIndex(&#8220;name&#8221;));<\/p>\n<p>                int age = cursor.getInt(cursor.getColumnIndex(&#8220;age&#8221;));<\/p>\n<p>                User user = new User(id, name, age);<\/p>\n<p>                userList.add(user);<\/p>\n<p>            } while (cursor.moveToNext());<\/p>\n<p>            cursor.close();<\/p>\n<p>        }<\/p>\n<p>        db.close();<\/p>\n<p>        return userList;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c`DatabaseHelper`\u7ee7\u627f\u81ea`SQLiteOpenHelper`\uff0c\u5e76\u5b9e\u73b0\u4e86`onCreate`\u548c`onUpgrade`\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u548c\u5347\u7ea7\u6570\u636e\u5e93\u8868\u683c\u3002\u5176\u4e2d`onCreate`\u65b9\u6cd5\u5728\u7b2c\u4e00\u6b21\u521b\u5efa\u6570\u636e\u5e93\u65f6\u8c03\u7528\uff0c\u7528\u4e8e\u521b\u5efa\u8868\u683c\uff1b`onUpgrade`\u65b9\u6cd5\u5728\u6570\u636e\u5e93\u7248\u672c\u53d1\u751f\u53d8\u5316\u65f6\u8c03\u7528\uff0c\u7528\u4e8e\u66f4\u65b0\u8868\u683c\u7ed3\u6784\u3002<\/p>\n<p>\u901a\u8fc7`getInstance`\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5230`DatabaseHelper`\u7684\u5b9e\u4f8b\uff0c\u786e\u4fdd\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u6570\u636e\u5e93\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528\u4e86\u53cc\u91cd\u68c0\u9a8c\u9501\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u4fdd\u8bc1\u4e86\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u83b7\u53d6\u5b9e\u4f8b\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u6570\u636e\u5e93\u7684\u57fa\u672c\u64cd\u4f5c\u65b9\u6cd5\u5982`insertUser`\u548c`getAllUsers`\u7528\u4e8e\u63d2\u5165\u6570\u636e\u548c\u67e5\u8be2\u6570\u636e\u3002\u5176\u4e2d\uff0c`insertUser`\u65b9\u6cd5\u5c06User\u5bf9\u8c61\u7684\u6570\u636e\u63d2\u5165\u5230user\u8868\u683c\u4e2d\uff0c`getAllUsers`\u65b9\u6cd5\u67e5\u8be2user\u8868\u683c\u4e2d\u7684\u6240\u6709\u6570\u636e\u5e76\u8fd4\u56de\u4e00\u4e2aList\u5217\u8868\u3002<\/p>\n<p>\u4f7f\u7528\u4e86\u5c01\u88c5\u597d\u7684\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u540e\uff0c\u6211\u4eec\u5728\u5e94\u7528\u4e2d\u5c31\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u5199\u590d\u6742\u7684SQL\u8bed\u53e5\u548c\u6570\u636e\u5e93\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u5173\u95ed\u64cd\u4f5c\u3002<\/p>\n<p>\u5c01\u88c5\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027\uff0c\u5c06\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ec6\u8282\u5c01\u88c5\u8d77\u6765\uff0c\u8ba9\u4e1a\u52a1\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u7b80\u6d01\u3002\u540c\u65f6\uff0c\u6570\u636e\u5e93\u5de5\u5177\u7c7b\u4e5f\u53ef\u4ee5\u52a0\u5165\u4e00\u4e9b\u6269\u5c55\u65b9\u6cd5\uff0c\u5982\u66f4\u65b0\u6570\u636e\u3001\u5220\u9664\u6570\u636e\u7b49\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u62d3\u5c55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Android\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5b58\u50a8\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u90e8\u5206\uff0c\u5176\u4e2d\u6570\u636e\u5e93\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u5b58\u50a8\u65b9\u5f0f\u4e4b\u4e00\u3002\u4f7f\u7528\u6570\u636e\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6570\u636e\u67e5\u8be2\u548c\u6301\u4e45\u5316\u7684\u529f\u80fd\u3002\u4e3a\u4e86\u65b9\u4fbf\u5f00\u53d1\u8005\u4f7f\u7528\u6570\u636e\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[15117,2233,431,17454,8718],"topic":[],"class_list":["post-10252","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-h5apk","tag-2233","tag-431","tag-app","tag-8718"],"_links":{"self":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/10252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/comments?post=10252"}],"version-history":[{"count":0,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/10252\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/media?parent=10252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/categories?post=10252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/tags?post=10252"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/topic?post=10252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}