{"id":237,"date":"2016-12-22T19:57:27","date_gmt":"2016-12-22T11:57:27","guid":{"rendered":"http:\/\/www.prayerlaputa.com\/?p=237"},"modified":"2016-12-22T19:57:27","modified_gmt":"2016-12-22T11:57:27","slug":"%e3%80%90%e8%bd%ac%e5%8f%91%e3%80%91android%e4%b8%adsqlite%e5%ba%94%e7%94%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=237","title":{"rendered":"\u3010\u8f6c\u53d1\u3011Android\u4e2dSQLite\u5e94\u7528\u8be6\u89e3"},"content":{"rendered":"<p>\u6253\u7b97\u6574\u7406\u4e00\u4e0b\u6700\u8fd1\u4e24\u5e74\u505aandroid\u6240\u5b66\u5230\u7684\u4e1c\u897f\uff0c\u4ee3\u7801\u3001\u6587\u7ae0\u914d\u5957\u6574\u7406\u4e00\u4e0b\uff0c\u4f46\u662f\u53d1\u73b0\u81ea\u5df1\u5176\u5b9e\u7814\u7a76\u7684\u8fd8\u5f88\u80a4\u6d45\uff0c\u5199\u51fa\u4e1c\u897f\u8fd8\u662f\u6ca1\u4eba\u5bb6\u5199\u7684\u660e\u767d\uff0cso\uff0c\u7d22\u6027\u76f4\u63a5\u8f6c\u53d1\u5427\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u65e5\u540e\u67e5\u8d77\u6765\u65b9\u4fbf\uff0c\u4ee3\u7801\u6253\u51fa\u4e00\u4efd\u65b9\u4fbf\u65e5\u540e\u642c\u7816\u3002<br \/>\n\u4e0b\u9762\u6587\u7ae0\u8f6c\u81ea\uff1a<a href=\"http:\/\/blog.csdn.net\/liuhe688\/article\/details\/6715983\">Android\u4e2dSQLite\u5e94\u7528\u8be6\u89e3<\/a> \u76f8\u5173\u4ee3\u7801\u53c2\u89c1\uff1a<a href=\"https:\/\/github.com\/evasnowind\/PrayerUtility\/\">https:\/\/github.com\/evasnowind\/PrayerUtility\/<\/a><br \/>\n&#8212;&#8212;&#8212;&#8212;\u534e\u4e3d\u7684\u5206\u5272\u7ebf&#8212;&#8212;&#8212;&#8212;<br \/>\n\u4e0a\u6b21\u6211\u5411\u5927\u5bb6\u4ecb\u7ecd\u4e86SQLite\u7684\u57fa\u672c\u4fe1\u606f\u548c\u4f7f\u7528\u8fc7\u7a0b\uff0c\u76f8\u4fe1\u670b\u53cb\u4eec\u5bf9SQLite\u5df2\u7ecf\u6709\u6240\u4e86\u89e3\u4e86\uff0c\u90a3\u4eca\u5929\u5462\uff0c\u6211\u5c31\u548c\u5927\u5bb6\u5206\u4eab\u4e00\u4e0b\u5728Android\u4e2d\u5982\u4f55\u4f7f\u7528SQLite\u3002<!--more--><br \/>\n\u73b0\u5728\u7684\u4e3b\u6d41\u79fb\u52a8\u8bbe\u5907\u50cfAndroid\u3001iPhone\u7b49\u90fd\u4f7f\u7528SQLite\u4f5c\u4e3a\u590d\u6742\u6570\u636e\u7684\u5b58\u50a8\u5f15\u64ce\uff0c\u5728\u6211\u4eec\u4e3a\u79fb\u52a8\u8bbe\u5907\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u4e5f\u8bb8\u5c31\u8981\u4f7f\u7528\u5230SQLite\u6765\u5b58\u50a8\u6211\u4eec\u5927\u91cf\u7684\u6570\u636e\uff0c\u6240\u4ee5\u6211\u4eec\u5c31\u9700\u8981\u638c\u63e1\u79fb\u52a8\u8bbe\u5907\u4e0a\u7684SQLite\u5f00\u53d1\u6280\u5de7\u3002\u5bf9\u4e8eAndroid\u5e73\u53f0\u6765\u8bf4\uff0c\u7cfb\u7edf\u5185\u7f6e\u4e86\u4e30\u5bcc\u7684API\u6765\u4f9b\u5f00\u53d1\u4eba\u5458\u64cd\u4f5cSQLite\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u7684\u5b8c\u6210\u5bf9\u6570\u636e\u7684\u5b58\u53d6\u3002<br \/>\n\u4e0b\u9762\u5c31\u5411\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e0bSQLite\u5e38\u7528\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4e3a\u4e86\u65b9\u4fbf\uff0c\u6211\u5c06\u4ee3\u7801\u5199\u5728\u4e86Activity\u7684onCreate\u4e2d\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        \/\/\u6253\u5f00\u6216\u521b\u5efatest.db\u6570\u636e\u5e93\n        SQLiteDatabase db = openOrCreateDatabase(&quot;test.db&quot;, Context.MODE_PRIVATE, null);\n        db.execSQL(&quot;DROP TABLE IF EXISTS person&quot;);\n        \/\/\u521b\u5efaperson\u8868\n        db.execSQL(&quot;CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)&quot;);\n        Person person = new Person();\n        person.name = &quot;john&quot;;\n        person.age = 30;\n        \/\/\u63d2\u5165\u6570\u636e\n        db.execSQL(&quot;INSERT INTO person VALUES (NULL, ?, ?)&quot;, new Object[]{person.name, person.age});\n        person.name = &quot;david&quot;;\n        person.age = 33;\n        \/\/ContentValues\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u653e\u6570\u636e\n        ContentValues cv = new ContentValues();\n        cv.put(&quot;name&quot;, person.name);\n        cv.put(&quot;age&quot;, person.age);\n        \/\/\u63d2\u5165ContentValues\u4e2d\u7684\u6570\u636e\n        db.insert(&quot;person&quot;, null, cv);\n        cv = new ContentValues();\n        cv.put(&quot;age&quot;, 35);\n        \/\/\u66f4\u65b0\u6570\u636e\n        db.update(&quot;person&quot;, cv, &quot;name = ?&quot;, new String[]{&quot;john&quot;});\n        Cursor c = db.rawQuery(&quot;SELECT * FROM person WHERE age &gt;= ?&quot;, new String[]{&quot;33&quot;});\n        while (c.moveToNext()) {\n            int _id = c.getInt(c.getColumnIndex(&quot;_id&quot;));\n            String name = c.getString(c.getColumnIndex(&quot;name&quot;));\n            int age = c.getInt(c.getColumnIndex(&quot;age&quot;));\n            Log.i(&quot;db&quot;, &quot;_id=&gt;&quot; + _id + &quot;, name=&gt;&quot; + name + &quot;, age=&gt;&quot; + age);\n        }\n        c.close();\n        \/\/\u5220\u9664\u6570\u636e\n        db.delete(&quot;person&quot;, &quot;age &lt; ?&quot;, new String[]{&quot;35&quot;});\n        \/\/\u5173\u95ed\u5f53\u524d\u6570\u636e\u5e93\n        db.close();\n        \/\/\u5220\u9664test.db\u6570\u636e\u5e93\n\/\/      deleteDatabase(&quot;test.db&quot;);\n    }\n<\/pre>\n<p>\u5728\u6267\u884c\u5b8c\u4e0a\u9762\u7684\u4ee3\u7801\u540e\uff0c\u7cfb\u7edf\u5c31\u4f1a\u5728\/data\/data\/[PACKAGE_NAME]\/databases\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u201ctest.db\u201d\u7684\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u5982\u56fe\uff1a<br \/>\n\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u57fa\u672c\u4e0a\u56ca\u62ec\u4e86\u5927\u90e8\u5206\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff1b\u5bf9\u4e8e\u6dfb\u52a0\u3001\u66f4\u65b0\u548c\u5220\u9664\u6765\u8bf4\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u4f7f\u7528<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ndb.executeSQL(String sql);\ndb.executeSQL(String sql, Object[] bindArgs);\/\/sql\u8bed\u53e5\u4e2d\u4f7f\u7528\u5360\u4f4d\u7b26\uff0c\u7136\u540e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5b9e\u9645\u7684\u53c2\u6570\u96c6\n<\/pre>\n<p>\u9664\u4e86\u7edf\u4e00\u7684\u5f62\u5f0f\u4e4b\u5916\uff0c\u4ed6\u4eec\u8fd8\u6709\u5404\u81ea\u7684\u64cd\u4f5c\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ndb.insert(String table, String nullColumnHack, ContentValues values);\ndb.update(String table, Contentvalues values, String whereClause, String whereArgs);\ndb.delete(String table, String whereClause, String whereArgs);\n<\/pre>\n<p>\u4ee5\u4e0a\u4e09\u4e2a\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u662f\u8868\u793a\u8981\u64cd\u4f5c\u7684\u8868\u540d\uff1binsert\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u5982\u679c\u63d2\u5165\u7684\u6570\u636e\u6bcf\u4e00\u5217\u90fd\u4e3a\u7a7a\u7684\u8bdd\uff0c\u9700\u8981\u6307\u5b9a\u6b64\u884c\u4e2d\u67d0\u4e00\u5217\u7684\u540d\u79f0\uff0c\u7cfb\u7edf\u5c06\u6b64\u5217\u8bbe\u7f6e\u4e3aNULL\uff0c\u4e0d\u81f3\u4e8e\u51fa\u73b0\u9519\u8bef\uff1binsert\u4e2d\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u662fContentValues\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u662f\u952e\u503c\u5bf9\u7ec4\u6210\u7684Map\uff0ckey\u4ee3\u8868\u5217\u540d\uff0cvalue\u4ee3\u8868\u8be5\u5217\u8981\u63d2\u5165\u7684\u503c\uff1bupdate\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e5f\u5f88\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u662f\u66f4\u65b0\u8be5\u5b57\u6bb5key\u4e3a\u6700\u65b0\u7684value\u503c\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570whereClause\u8868\u793aWHERE\u8868\u8fbe\u5f0f\uff0c\u6bd4\u5982\u201cage &gt; ? and age &lt; ?\u201d\u7b49\uff0c\u6700\u540e\u7684whereArgs\u53c2\u6570\u662f\u5360\u4f4d\u7b26\u7684\u5b9e\u9645\u53c2\u6570\u503c\uff1bdelete\u65b9\u6cd5\u7684\u53c2\u6570\u4e5f\u662f\u4e00\u6837\u3002<br \/>\n\u4e0b\u9762\u6765\u8bf4\u8bf4\u67e5\u8be2\u64cd\u4f5c\u3002\u67e5\u8be2\u64cd\u4f5c\u76f8\u5bf9\u4e8e\u4e0a\u9762\u7684\u51e0\u79cd\u64cd\u4f5c\u8981\u590d\u6742\u4e9b\uff0c\u56e0\u4e3a\u6211\u4eec\u7ecf\u5e38\u8981\u9762\u5bf9\u7740\u5404\u79cd\u5404\u6837\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u6240\u4ee5\u7cfb\u7edf\u4e5f\u8003\u8651\u5230\u8fd9\u79cd\u590d\u6742\u6027\uff0c\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u8f83\u4e3a\u4e30\u5bcc\u7684\u67e5\u8be2\u5f62\u5f0f\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ndb.rawQuery(String sql, String[] selectionArgs);\ndb.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);\ndb.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);\ndb.query(String distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);\n<\/pre>\n<p>\u4e0a\u9762\u51e0\u79cd\u90fd\u662f\u5e38\u7528\u7684\u67e5\u8be2\u65b9\u6cd5\uff0c\u7b2c\u4e00\u79cd\u6700\u4e3a\u7b80\u5355\uff0c\u5c06\u6240\u6709\u7684SQL\u8bed\u53e5\u90fd\u7ec4\u7ec7\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26\u4ee3\u66ff\u5b9e\u9645\u53c2\u6570\uff0cselectionArgs\u5c31\u662f\u5360\u4f4d\u7b26\u5b9e\u9645\u53c2\u6570\u96c6\uff1b\u4e0b\u9762\u7684\u51e0\u79cd\u53c2\u6570\u90fd\u5f88\u7c7b\u4f3c\uff0ccolumns\u8868\u793a\u8981\u67e5\u8be2\u7684\u5217\u6240\u6709\u540d\u79f0\u96c6\uff0cselection\u8868\u793aWHERE\u4e4b\u540e\u7684\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5360\u4f4d\u7b26\uff0cgroupBy\u6307\u5b9a\u5206\u7ec4\u7684\u5217\u540d\uff0chaving\u6307\u5b9a\u5206\u7ec4\u6761\u4ef6\uff0c\u914d\u5408groupBy\u4f7f\u7528\uff0corderBy\u6307\u5b9a\u6392\u5e8f\u7684\u5217\u540d\uff0climit\u6307\u5b9a\u5206\u9875\u53c2\u6570\uff0cdistinct\u53ef\u4ee5\u6307\u5b9a\u201ctrue\u201d\u6216\u201cfalse\u201d\u8868\u793a\u8981\u4e0d\u8981\u8fc7\u6ee4\u91cd\u590d\u503c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cselection\u3001groupBy\u3001having\u3001orderBy\u3001limit\u8fd9\u51e0\u4e2a\u53c2\u6570\u4e2d\u4e0d\u5305\u62ec\u201cWHERE\u201d\u3001\u201cGROUP BY\u201d\u3001\u201cHAVING\u201d\u3001\u201cORDER BY\u201d\u3001\u201cLIMIT\u201d\u7b49SQL\u5173\u952e\u5b57\u3002<br \/>\n\u6700\u540e\uff0c\u4ed6\u4eec\u540c\u65f6\u8fd4\u56de\u4e00\u4e2aCursor\u5bf9\u8c61\uff0c\u4ee3\u8868\u6570\u636e\u96c6\u7684\u6e38\u6807\uff0c\u6709\u70b9\u7c7b\u4f3c\u4e8eJavaSE\u4e2d\u7684ResultSet\u3002<br \/>\n\u4e0b\u9762\u662fCursor\u5bf9\u8c61\u7684\u5e38\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nc.move(int offset); \/\/\u4ee5\u5f53\u524d\u4f4d\u7f6e\u4e3a\u53c2\u8003,\u79fb\u52a8\u5230\u6307\u5b9a\u884c\nc.moveToFirst();    \/\/\u79fb\u52a8\u5230\u7b2c\u4e00\u884c\nc.moveToLast();     \/\/\u79fb\u52a8\u5230\u6700\u540e\u4e00\u884c\nc.moveToPosition(int position); \/\/\u79fb\u52a8\u5230\u6307\u5b9a\u884c\nc.moveToPrevious(); \/\/\u79fb\u52a8\u5230\u524d\u4e00\u884c\nc.moveToNext();     \/\/\u79fb\u52a8\u5230\u4e0b\u4e00\u884c\nc.isFirst();        \/\/\u662f\u5426\u6307\u5411\u7b2c\u4e00\u6761\nc.isLast();     \/\/\u662f\u5426\u6307\u5411\u6700\u540e\u4e00\u6761\nc.isBeforeFirst();  \/\/\u662f\u5426\u6307\u5411\u7b2c\u4e00\u6761\u4e4b\u524d\nc.isAfterLast();    \/\/\u662f\u5426\u6307\u5411\u6700\u540e\u4e00\u6761\u4e4b\u540e\nc.isNull(int columnIndex);  \/\/\u6307\u5b9a\u5217\u662f\u5426\u4e3a\u7a7a(\u5217\u57fa\u6570\u4e3a0)\nc.isClosed();       \/\/\u6e38\u6807\u662f\u5426\u5df2\u5173\u95ed\nc.getCount();       \/\/\u603b\u6570\u636e\u9879\u6570\nc.getPosition();    \/\/\u8fd4\u56de\u5f53\u524d\u6e38\u6807\u6240\u6307\u5411\u7684\u884c\u6570\nc.getColumnIndex(String columnName);\/\/\u8fd4\u56de\u67d0\u5217\u540d\u5bf9\u5e94\u7684\u5217\u7d22\u5f15\u503c\nc.getString(int columnIndex);   \/\/\u8fd4\u56de\u5f53\u524d\u884c\u6307\u5b9a\u5217\u7684\u503c\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u5df2\u7ecf\u7528\u5230\u4e86\u8fd9\u51e0\u4e2a\u5e38\u7528\u65b9\u6cd5\u4e2d\u7684\u4e00\u4e9b\uff0c\u5173\u4e8e\u66f4\u591a\u7684\u4fe1\u606f\uff0c\u5927\u5bb6\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u8bf4\u660e\u3002<br \/>\n\u6700\u540e\u5f53\u6211\u4eec\u5b8c\u6210\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u540e\uff0c\u8bb0\u5f97\u8c03\u7528SQLiteDatabase\u7684close()\u65b9\u6cd5\u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5426\u5219\u5bb9\u6613\u51fa\u73b0SQLiteException\u3002<br \/>\n\u4e0a\u9762\u5c31\u662fSQLite\u7684\u57fa\u672c\u5e94\u7528\uff0c\u4f46\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4e3a\u4e86\u80fd\u591f\u66f4\u597d\u7684\u7ba1\u7406\u548c\u7ef4\u62a4\u6570\u636e\u5e93\uff0c\u6211\u4eec\u4f1a\u5c01\u88c5\u4e00\u4e2a\u7ee7\u627f\u81eaSQLiteOpenHelper\u7c7b\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u7c7b\uff0c\u7136\u540e\u4ee5\u8fd9\u4e2a\u7c7b\u4e3a\u57fa\u7840\uff0c\u518d\u5c01\u88c5\u6211\u4eec\u7684\u4e1a\u52a1\u903b\u8f91\u65b9\u6cd5\u3002<br \/>\n\u4e0b\u9762\uff0c\u6211\u4eec\u5c31\u4ee5\u4e00\u4e2a\u5b9e\u4f8b\u6765\u8bb2\u89e3\u5177\u4f53\u7684\u7528\u6cd5\uff0c\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3adb\u7684\u9879\u76ee\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<br \/>\n\u5176\u4e2dDBHelper\u7ee7\u627f\u4e86SQLiteOpenHelper\uff0c\u4f5c\u4e3a\u7ef4\u62a4\u548c\u7ba1\u7406\u6570\u636e\u5e93\u7684\u57fa\u7c7b\uff0cDBManager\u662f\u5efa\u7acb\u5728DBHelper\u4e4b\u4e0a\uff0c\u5c01\u88c5\u4e86\u5e38\u7528\u7684\u4e1a\u52a1\u65b9\u6cd5\uff0cPerson\u662f\u6211\u4eec\u7684person\u8868\u5bf9\u5e94\u7684JavaBean\uff0cMainActivity\u5c31\u662f\u6211\u4eec\u663e\u793a\u7684\u754c\u9762\u3002<br \/>\n\u4e0b\u9762\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0bDBHelper\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.scott.db;\nimport android.content.Context;\nimport android.database.sqlite.SQLiteDatabase;\nimport android.database.sqlite.SQLiteOpenHelper;\npublic class DBHelper extends SQLiteOpenHelper {\n    private static final String DATABASE_NAME = &quot;test.db&quot;;\n    private static final int DATABASE_VERSION = 1;\n    public DBHelper(Context context) {\n        \/\/CursorFactory\u8bbe\u7f6e\u4e3anull,\u4f7f\u7528\u9ed8\u8ba4\u503c\n        super(context, DATABASE_NAME, null, DATABASE_VERSION);\n    }\n    \/\/\u6570\u636e\u5e93\u7b2c\u4e00\u6b21\u88ab\u521b\u5efa\u65f6onCreate\u4f1a\u88ab\u8c03\u7528\n    @Override\n    public void onCreate(SQLiteDatabase db) {\n        db.execSQL(&quot;CREATE TABLE IF NOT EXISTS person&quot; +\n                &quot;(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER, info TEXT)&quot;);\n    }\n    \/\/\u5982\u679cDATABASE_VERSION\u503c\u88ab\u6539\u4e3a2,\u7cfb\u7edf\u53d1\u73b0\u73b0\u6709\u6570\u636e\u5e93\u7248\u672c\u4e0d\u540c,\u5373\u4f1a\u8c03\u7528onUpgrade\n    @Override\n    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n        db.execSQL(&quot;ALTER TABLE person ADD COLUMN other STRING&quot;);\n    }\n}\n<\/pre>\n<p>\u6b63\u5982\u4e0a\u9762\u6240\u8ff0\uff0c\u6570\u636e\u5e93\u7b2c\u4e00\u6b21\u521b\u5efa\u65f6onCreate\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u521b\u5efa\u8868\u7684\u8bed\u53e5\uff0c\u5f53\u7cfb\u7edf\u53d1\u73b0\u7248\u672c\u53d8\u5316\u4e4b\u540e\uff0c\u4f1a\u8c03\u7528onUpgrade\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6267\u884c\u4fee\u6539\u8868\u7ed3\u6784\u7b49\u8bed\u53e5\u3002<br \/>\n\u4e3a\u4e86\u65b9\u4fbf\u6211\u4eec\u9762\u5411\u5bf9\u8c61\u7684\u4f7f\u7528\u6570\u636e\uff0c\u6211\u4eec\u5efa\u4e00\u4e2aPerson\u7c7b\uff0c\u5bf9\u5e94person\u8868\u4e2d\u7684\u5b57\u6bb5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.scott.db;\npublic class Person {\n    public int _id;\n    public String name;\n    public int age;\n    public String info;\n    public Person() {\n    }\n    public Person(String name, int age, String info) {\n        this.name = name;\n        this.age = age;\n        this.info = info;\n    }\n}\n<\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2aDBManager\uff0c\u6765\u5c01\u88c5\u6211\u4eec\u6240\u6709\u7684\u4e1a\u52a1\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.scott.db;\nimport java.util.ArrayList;\nimport java.util.List;\nimport android.content.ContentValues;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.database.sqlite.SQLiteDatabase;\npublic class DBManager {\n    private DBHelper helper;\n    private SQLiteDatabase db;\n    public DBManager(Context context) {\n        helper = new DBHelper(context);\n        \/\/\u56e0\u4e3agetWritableDatabase\u5185\u90e8\u8c03\u7528\u4e86mContext.openOrCreateDatabase(mName, 0, mFactory);\n        \/\/\u6240\u4ee5\u8981\u786e\u4fddcontext\u5df2\u521d\u59cb\u5316,\u6211\u4eec\u53ef\u4ee5\u628a\u5b9e\u4f8b\u5316DBManager\u7684\u6b65\u9aa4\u653e\u5728Activity\u7684onCreate\u91cc\n        db = helper.getWritableDatabase();\n    }\n    \/**\n     * add persons\n     * @param persons\n     *\/\n    public void add(List&lt;Person&gt; persons) {\n        db.beginTransaction();  \/\/\u5f00\u59cb\u4e8b\u52a1\n        try {\n            for (Person person : persons) {\n                db.execSQL(&quot;INSERT INTO person VALUES(null, ?, ?, ?)&quot;, new Object[]{person.name, person.age, person.info});\n            }\n            db.setTransactionSuccessful();  \/\/\u8bbe\u7f6e\u4e8b\u52a1\u6210\u529f\u5b8c\u6210\n        } finally {\n            db.endTransaction();    \/\/\u7ed3\u675f\u4e8b\u52a1\n        }\n    }\n    \/**\n     * update person's age\n     * @param person\n     *\/\n    public void updateAge(Person person) {\n        ContentValues cv = new ContentValues();\n        cv.put(&quot;age&quot;, person.age);\n        db.update(&quot;person&quot;, cv, &quot;name = ?&quot;, new String[]{person.name});\n    }\n    \/**\n     * delete old person\n     * @param person\n     *\/\n    public void deleteOldPerson(Person person) {\n        db.delete(&quot;person&quot;, &quot;age &gt;= ?&quot;, new String[]{String.valueOf(person.age)});\n    }\n    \/**\n     * query all persons, return list\n     * @return List&lt;Person&gt;\n     *\/\n    public List&lt;Person&gt; query() {\n        ArrayList&lt;Person&gt; persons = new ArrayList&lt;Person&gt;();\n        Cursor c = queryTheCursor();\n        while (c.moveToNext()) {\n            Person person = new Person();\n            person._id = c.getInt(c.getColumnIndex(&quot;_id&quot;));\n            person.name = c.getString(c.getColumnIndex(&quot;name&quot;));\n            person.age = c.getInt(c.getColumnIndex(&quot;age&quot;));\n            person.info = c.getString(c.getColumnIndex(&quot;info&quot;));\n            persons.add(person);\n        }\n        c.close();\n        return persons;\n    }\n    \/**\n     * query all persons, return cursor\n     * @return  Cursor\n     *\/\n    public Cursor queryTheCursor() {\n        Cursor c = db.rawQuery(&quot;SELECT * FROM person&quot;, null);\n        return c;\n    }\n    \/**\n     * close database\n     *\/\n    public void closeDB() {\n        db.close();\n    }\n}\n<\/pre>\n<p>\u6211\u4eec\u5728DBManager\u6784\u9020\u65b9\u6cd5\u4e2d\u5b9e\u4f8b\u5316DBHelper\u5e76\u83b7\u53d6\u4e00\u4e2aSQLiteDatabase\u5bf9\u8c61\uff0c\u4f5c\u4e3a\u6574\u4e2a\u5e94\u7528\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\uff1b\u5728\u6dfb\u52a0\u591a\u4e2aPerson\u4fe1\u606f\u65f6\uff0c\u6211\u4eec\u91c7\u7528\u4e86\u4e8b\u52a1\u5904\u7406\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\uff1b\u6700\u540e\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2acloseDB\u65b9\u6cd5\uff0c\u91ca\u653e\u6570\u636e\u5e93\u8d44\u6e90\uff0c\u8fd9\u4e00\u4e2a\u6b65\u9aa4\u5728\u6211\u4eec\u6574\u4e2a\u5e94\u7528\u5173\u95ed\u65f6\u6267\u884c\uff0c\u8fd9\u4e2a\u73af\u8282\u5bb9\u6613\u88ab\u5fd8\u8bb0\uff0c\u6240\u4ee5\u670b\u53cb\u4eec\u8981\u6ce8\u610f\u3002<br \/>\n\u6211\u4eec\u83b7\u53d6\u6570\u636e\u5e93\u5b9e\u4f8b\u65f6\u4f7f\u7528\u4e86getWritableDatabase()\u65b9\u6cd5\uff0c\u4e5f\u8bb8\u670b\u53cb\u4eec\u4f1a\u6709\u7591\u95ee\uff0c\u5728getWritableDatabase()\u548cgetReadableDatabase()\u4e2d\uff0c\u4f60\u4e3a\u4ec0\u4e48\u9009\u62e9\u524d\u8005\u4f5c\u4e3a\u6574\u4e2a\u5e94\u7528\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u5462\uff1f\u5728\u8fd9\u91cc\u6211\u60f3\u548c\u5927\u5bb6\u7740\u91cd\u5206\u6790\u4e00\u4e0b\u8fd9\u4e00\u70b9\u3002<br \/>\n\u6211\u4eec\u6765\u770b\u4e00\u4e0bSQLiteOpenHelper\u4e2d\u7684getReadableDatabase()\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic synchronized SQLiteDatabase getReadableDatabase() {\n    if (mDatabase != null &amp;&amp; mDatabase.isOpen()) {\n        \/\/ \u5982\u679c\u53d1\u73b0mDatabase\u4e0d\u4e3a\u7a7a\u5e76\u4e14\u5df2\u7ecf\u6253\u5f00\u5219\u76f4\u63a5\u8fd4\u56de\n        return mDatabase;\n    }\n    if (mIsInitializing) {\n        \/\/ \u5982\u679c\u6b63\u5728\u521d\u59cb\u5316\u5219\u629b\u51fa\u5f02\u5e38\n        throw new IllegalStateException(&quot;getReadableDatabase called recursively&quot;);\n    }\n    \/\/ \u5f00\u59cb\u5b9e\u4f8b\u5316\u6570\u636e\u5e93mDatabase\n    try {\n        \/\/ \u6ce8\u610f\u8fd9\u91cc\u662f\u8c03\u7528\u4e86getWritableDatabase()\u65b9\u6cd5\n        return getWritableDatabase();\n    } catch (SQLiteException e) {\n        if (mName == null)\n            throw e; \/\/ Can't open a temp database read-only!\n        Log.e(TAG, &quot;Couldn't open &quot; + mName + &quot; for writing (will try read-only):&quot;, e);\n    }\n    \/\/ \u5982\u679c\u65e0\u6cd5\u4ee5\u53ef\u8bfb\u5199\u6a21\u5f0f\u6253\u5f00\u6570\u636e\u5e93 \u5219\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\n    SQLiteDatabase db = null;\n    try {\n        mIsInitializing = true;\n        String path = mContext.getDatabasePath(mName).getPath();\/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8def\u5f84\n        \/\/ \u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6570\u636e\u5e93\n        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);\n        if (db.getVersion() != mNewVersion) {\n            throw new SQLiteException(&quot;Can't upgrade read-only database from version &quot; + db.getVersion() + &quot; to &quot;\n                    + mNewVersion + &quot;: &quot; + path);\n        }\n        onOpen(db);\n        Log.w(TAG, &quot;Opened &quot; + mName + &quot; in read-only mode&quot;);\n        mDatabase = db;\/\/ \u4e3amDatabase\u6307\u5b9a\u65b0\u6253\u5f00\u7684\u6570\u636e\u5e93\n        return mDatabase;\/\/ \u8fd4\u56de\u6253\u5f00\u7684\u6570\u636e\u5e93\n    } finally {\n        mIsInitializing = false;\n        if (db != null &amp;&amp; db != mDatabase)\n            db.close();\n    }\n}\n<\/pre>\n<p>\u5728getReadableDatabase()\u65b9\u6cd5\u4e2d\uff0c\u9996\u5148\u5224\u65ad\u662f\u5426\u5df2\u5b58\u5728\u6570\u636e\u5e93\u5b9e\u4f8b\u5e76\u4e14\u662f\u6253\u5f00\u72b6\u6001\uff0c\u5982\u679c\u662f\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u8be5\u5b9e\u4f8b\uff0c\u5426\u5219\u8bd5\u56fe\u83b7\u53d6\u4e00\u4e2a\u53ef\u8bfb\u5199\u6a21\u5f0f\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\uff0c\u5982\u679c\u9047\u5230\u78c1\u76d8\u7a7a\u95f4\u5df2\u6ee1\u7b49\u60c5\u51b5\u83b7\u53d6\u5931\u8d25\u7684\u8bdd\uff0c\u518d\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6570\u636e\u5e93\uff0c\u83b7\u53d6\u6570\u636e\u5e93\u5b9e\u4f8b\u5e76\u8fd4\u56de\uff0c\u7136\u540e\u4e3amDatabase\u8d4b\u503c\u4e3a\u6700\u65b0\u6253\u5f00\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u3002\u65e2\u7136\u6709\u53ef\u80fd\u8c03\u7528\u5230getWritableDatabase()\u65b9\u6cd5\uff0c\u6211\u4eec\u5c31\u8981\u770b\u4e00\u4e0b\u4e86\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic synchronized SQLiteDatabase getWritableDatabase() {\n    if (mDatabase != null &amp;&amp; mDatabase.isOpen() &amp;&amp; !mDatabase.isReadOnly()) {\n        \/\/ \u5982\u679cmDatabase\u4e0d\u4e3a\u7a7a\u5df2\u6253\u5f00\u5e76\u4e14\u4e0d\u662f\u53ea\u8bfb\u6a21\u5f0f \u5219\u8fd4\u56de\u8be5\u5b9e\u4f8b\n        return mDatabase;\n    }\n    if (mIsInitializing) {\n        throw new IllegalStateException(&quot;getWritableDatabase called recursively&quot;);\n    }\n    \/\/ If we have a read-only database open, someone could be using it\n    \/\/ (though they shouldn't), which would cause a lock to be held on\n    \/\/ the file, and our attempts to open the database read-write would\n    \/\/ fail waiting for the file lock. To prevent that, we acquire the\n    \/\/ lock on the read-only database, which shuts out other users.\n    boolean success = false;\n    SQLiteDatabase db = null;\n    \/\/ \u5982\u679cmDatabase\u4e0d\u4e3a\u7a7a\u5219\u52a0\u9501 \u963b\u6b62\u5176\u4ed6\u7684\u64cd\u4f5c\n    if (mDatabase != null)\n        mDatabase.lock();\n    try {\n        mIsInitializing = true;\n        if (mName == null) {\n            db = SQLiteDatabase.create(null);\n        } else {\n            \/\/ \u6253\u5f00\u6216\u521b\u5efa\u6570\u636e\u5e93\n            db = mContext.openOrCreateDatabase(mName, 0, mFactory);\n        }\n        \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u7248\u672c(\u5982\u679c\u521a\u521b\u5efa\u7684\u6570\u636e\u5e93,\u7248\u672c\u4e3a0)\n        int version = db.getVersion();\n        \/\/ \u6bd4\u8f83\u7248\u672c(\u6211\u4eec\u4ee3\u7801\u4e2d\u7684\u7248\u672cmNewVersion\u4e3a1)\n        if (version != mNewVersion) {\n            db.beginTransaction();\/\/ \u5f00\u59cb\u4e8b\u52a1\n            try {\n                if (version == 0) {\n                    \/\/ \u6267\u884c\u6211\u4eec\u7684onCreate\u65b9\u6cd5\n                    onCreate(db);\n                } else {\n                    \/\/ \u5982\u679c\u6211\u4eec\u5e94\u7528\u5347\u7ea7\u4e86mNewVersion\u4e3a2,\u800c\u539f\u7248\u672c\u4e3a1\u5219\u6267\u884conUpgrade\u65b9\u6cd5\n                    onUpgrade(db, version, mNewVersion);\n                }\n                db.setVersion(mNewVersion);\/\/ \u8bbe\u7f6e\u6700\u65b0\u7248\u672c\n                db.setTransactionSuccessful();\/\/ \u8bbe\u7f6e\u4e8b\u52a1\u6210\u529f\n            } finally {\n                db.endTransaction();\/\/ \u7ed3\u675f\u4e8b\u52a1\n            }\n        }\n        onOpen(db);\n        success = true;\n        return db;\/\/ \u8fd4\u56de\u53ef\u8bfb\u5199\u6a21\u5f0f\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\n    } finally {\n        mIsInitializing = false;\n        if (success) {\n            \/\/ \u6253\u5f00\u6210\u529f\n            if (mDatabase != null) {\n                \/\/ \u5982\u679cmDatabase\u6709\u503c\u5219\u5148\u5173\u95ed\n                try {\n                    mDatabase.close();\n                } catch (Exception e) {\n                }\n                mDatabase.unlock();\/\/ \u89e3\u9501\n            }\n            mDatabase = db;\/\/ \u8d4b\u503c\u7ed9mDatabase\n        } else {\n            \/\/ \u6253\u5f00\u5931\u8d25\u7684\u60c5\u51b5\uff1a\u89e3\u9501\u3001\u5173\u95ed\n            if (mDatabase != null)\n                mDatabase.unlock();\n            if (db != null)\n                db.close();\n        }\n    }\n}\n<\/pre>\n<p>\u5927\u5bb6\u53ef\u4ee5\u770b\u5230\uff0c\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\u662f\uff0c\u9996\u5148\u5224\u65admDatabase\u5982\u679c\u4e0d\u4e3a\u7a7a\u5df2\u6253\u5f00\u5e76\u4e0d\u662f\u53ea\u8bfb\u6a21\u5f0f\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5219\u5982\u679cmDatabase\u4e0d\u4e3a\u7a7a\u5219\u52a0\u9501\uff0c\u7136\u540e\u5f00\u59cb\u6253\u5f00\u6216\u521b\u5efa\u6570\u636e\u5e93\uff0c\u6bd4\u8f83\u7248\u672c\uff0c\u6839\u636e\u7248\u672c\u53f7\u6765\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\uff0c\u4e3a\u6570\u636e\u5e93\u8bbe\u7f6e\u65b0\u7248\u672c\u53f7\uff0c\u6700\u540e\u91ca\u653e\u65e7\u7684\u4e0d\u4e3a\u7a7a\u7684mDatabase\u5e76\u89e3\u9501\uff0c\u628a\u65b0\u6253\u5f00\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u8d4b\u4e88mDatabase\uff0c\u5e76\u8fd4\u56de\u6700\u65b0\u5b9e\u4f8b\u3002<br \/>\n\u770b\u5b8c\u4e0a\u9762\u7684\u8fc7\u7a0b\u4e4b\u540e\uff0c\u5927\u5bb6\u6216\u8bb8\u5c31\u6e05\u695a\u4e86\u8bb8\u591a\uff0c\u5982\u679c\u4e0d\u662f\u5728\u9047\u5230\u78c1\u76d8\u7a7a\u95f4\u5df2\u6ee1\u7b49\u60c5\u51b5\uff0cgetReadableDatabase()\u4e00\u822c\u90fd\u4f1a\u8fd4\u56de\u548cgetWritableDatabase()\u4e00\u6837\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\uff0c\u6240\u4ee5\u6211\u4eec\u5728DBManager\u6784\u9020\u65b9\u6cd5\u4e2d\u4f7f\u7528getWritableDatabase()\u83b7\u53d6\u6574\u4e2a\u5e94\u7528\u6240\u4f7f\u7528\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u662f\u53ef\u884c\u7684\u3002\u5f53\u7136\u5982\u679c\u4f60\u771f\u7684\u62c5\u5fc3\u8fd9\u79cd\u60c5\u51b5\u4f1a\u53d1\u751f\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u5148\u7528getWritableDatabase()\u83b7\u53d6\u6570\u636e\u5b9e\u4f8b\uff0c\u5982\u679c\u9047\u5230\u5f02\u5e38\uff0c\u518d\u8bd5\u56fe\u7528getReadableDatabase()\u83b7\u53d6\u5b9e\u4f8b\uff0c\u5f53\u7136\u8fd9\u4e2a\u65f6\u5019\u4f60\u83b7\u53d6\u7684\u5b9e\u4f8b\u53ea\u80fd\u8bfb\u4e0d\u80fd\u5199\u4e86\u3002<br \/>\n\u6700\u540e\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u6765\u663e\u793a\u6570\u636e\uff0c\u4e0b\u9762\u662fMainActivity.Java\u7684\u5e03\u5c40\u6587\u4ef6\u548c\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;&gt;\n    &lt;Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;add&quot; android:onClick=&quot;add&quot;\/&gt;\n    &lt;Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;update&quot; android:onClick=&quot;update&quot;\/&gt;\n    &lt;Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;delete&quot; android:onClick=&quot;delete&quot;\/&gt;\n    &lt;Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;query&quot; android:onClick=&quot;query&quot;\/&gt;\n    &lt;Button android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;queryTheCursor&quot; android:onClick=&quot;queryTheCursor&quot;\/&gt;\n    &lt;ListView android:id=&quot;@+id\/listView&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;\/&gt;\n&lt;\/LinearLayout&gt;\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.scott.db;\nimport java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\nimport android.app.Activity;\nimport android.database.Cursor;\nimport android.database.CursorWrapper;\nimport android.os.Bundle;\nimport android.view.View;\nimport android.widget.ListView;\nimport android.widget.SimpleAdapter;\nimport android.widget.SimpleCursorAdapter;\npublic class MainActivity extends Activity {\n    private DBManager mgr;\n    private ListView listView;\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.main);\n        listView = (ListView) findViewById(R.id.listView);\n        \/\/\u521d\u59cb\u5316DBManager\n        mgr = new DBManager(this);\n    }\n    @Override\n    protected void onDestroy() {\n        super.onDestroy();\n        \/\/\u5e94\u7528\u7684\u6700\u540e\u4e00\u4e2aActivity\u5173\u95ed\u65f6\u5e94\u91ca\u653eDB\n        mgr.closeDB();\n    }\n    public void add(View view) {\n        ArrayList&lt;Person&gt; persons = new ArrayList&lt;Person&gt;();\n        Person person1 = new Person(&quot;Ella&quot;, 22, &quot;lively girl&quot;);\n        Person person2 = new Person(&quot;Jenny&quot;, 22, &quot;beautiful girl&quot;);\n        Person person3 = new Person(&quot;Jessica&quot;, 23, &quot;sexy girl&quot;);\n        Person person4 = new Person(&quot;Kelly&quot;, 23, &quot;hot baby&quot;);\n        Person person5 = new Person(&quot;Jane&quot;, 25, &quot;a pretty woman&quot;);\n        persons.add(person1);\n        persons.add(person2);\n        persons.add(person3);\n        persons.add(person4);\n        persons.add(person5);\n        mgr.add(persons);\n    }\n    public void update(View view) {\n        Person person = new Person();\n        person.name = &quot;Jane&quot;;\n        person.age = 30;\n        mgr.updateAge(person);\n    }\n    public void delete(View view) {\n        Person person = new Person();\n        person.age = 30;\n        mgr.deleteOldPerson(person);\n    }\n    public void query(View view) {\n        List&lt;Person&gt; persons = mgr.query();\n        ArrayList&lt;Map&lt;String, String&gt;&gt; list = new ArrayList&lt;Map&lt;String, String&gt;&gt;();\n        for (Person person : persons) {\n            HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();\n            map.put(&quot;name&quot;, person.name);\n            map.put(&quot;info&quot;, person.age + &quot; years old, &quot; + person.info);\n            list.add(map);\n        }\n        SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,\n                    new String[]{&quot;name&quot;, &quot;info&quot;}, new int[]{android.R.id.text1, android.R.id.text2});\n        listView.setAdapter(adapter);\n    }\n    public void queryTheCursor(View view) {\n        Cursor c = mgr.queryTheCursor();\n        startManagingCursor(c); \/\/\u6258\u4ed8\u7ed9activity\u6839\u636e\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\u53bb\u7ba1\u7406Cursor\u7684\u751f\u547d\u5468\u671f\n        CursorWrapper cursorWrapper = new CursorWrapper(c) {\n            @Override\n            public String getString(int columnIndex) {\n                \/\/\u5c06\u7b80\u4ecb\u524d\u52a0\u4e0a\u5e74\u9f84\n                if (getColumnName(columnIndex).equals(&quot;info&quot;)) {\n                    int age = getInt(getColumnIndex(&quot;age&quot;));\n                    return age + &quot; years old, &quot; + super.getString(columnIndex);\n                }\n                return super.getString(columnIndex);\n            }\n        };\n        \/\/\u786e\u4fdd\u67e5\u8be2\u7ed3\u679c\u4e2d\u6709&quot;_id&quot;\u5217\n        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2,\n                cursorWrapper, new String[]{&quot;name&quot;, &quot;info&quot;}, new int[]{android.R.id.text1, android.R.id.text2});\n        ListView listView = (ListView) findViewById(R.id.listView);\n        listView.setAdapter(adapter);\n    }\n}\n<\/pre>\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662fSimpleCursorAdapter\u7684\u5e94\u7528\uff0c\u5f53\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u9002\u914d\u5668\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u5f97\u5230\u4e00\u4e2aCursor\u5bf9\u8c61\uff0c\u8fd9\u91cc\u9762\u6709\u51e0\u4e2a\u95ee\u9898\uff1a\u5982\u4f55\u7ba1\u7406Cursor\u7684\u751f\u547d\u5468\u671f\uff0c\u5982\u679c\u5305\u88c5Cursor\uff0cCursor\u7ed3\u679c\u96c6\u90fd\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\u3002<br \/>\n\u5982\u679c\u624b\u52a8\u53bb\u7ba1\u7406Cursor\u7684\u8bdd\u4f1a\u975e\u5e38\u7684\u9ebb\u70e6\uff0c\u8fd8\u6709\u4e00\u5b9a\u7684\u98ce\u9669\uff0c\u5904\u7406\u4e0d\u5f53\u7684\u8bdd\u8fd0\u884c\u671f\u95f4\u5c31\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u5e78\u597dActivity\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86startManagingCursor(Cursor cursor)\u65b9\u6cd5\uff0c\u5b83\u4f1a\u6839\u636eActivity\u7684\u751f\u547d\u5468\u671f\u53bb\u7ba1\u7406\u5f53\u524d\u7684Cursor\u5bf9\u8c61\uff0c\u4e0b\u9762\u662f\u8be5\u65b9\u6cd5\u7684\u8bf4\u660e\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/**\n     * This method allows the activity to take care of managing the given\n     * {@link Cursor}'s lifecycle for you based on the activity's lifecycle.\n     * That is, when the activity is stopped it will automatically call\n     * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted\n     * it will call {@link Cursor#requery} for you.  When the activity is\n     * destroyed, all managed Cursors will be closed automatically.\n     *\n     * @param c The Cursor to be managed.\n     *\n     * @see #managedQuery(android.net.Uri , String[], String, String[], String)\n     * @see #stopManagingCursor\n     *\/\n<\/pre>\n<p>\u6587\u4e2d\u63d0\u5230\uff0cstartManagingCursor\u65b9\u6cd5\u4f1a\u6839\u636eActivity\u7684\u751f\u547d\u5468\u671f\u53bb\u7ba1\u7406\u5f53\u524d\u7684Cursor\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u5c31\u662f\u8bf4\u5f53Activity\u505c\u6b62\u65f6\u4ed6\u4f1a\u81ea\u52a8\u8c03\u7528Cursor\u7684deactivate\u65b9\u6cd5\uff0c\u7981\u7528\u6e38\u6807\uff0c\u5f53Activity\u91cd\u65b0\u56de\u5230\u5c4f\u5e55\u65f6\u5b83\u4f1a\u8c03\u7528Cursor\u7684requery\u65b9\u6cd5\u518d\u6b21\u67e5\u8be2\uff0c\u5f53Activity\u6467\u6bc1\u65f6\uff0c\u88ab\u7ba1\u7406\u7684Cursor\u90fd\u4f1a\u81ea\u52a8\u5173\u95ed\u91ca\u653e\u3002<br \/>\n\u5982\u4f55\u5305\u88c5Cursor\uff1a\u6211\u4eec\u4f1a\u4f7f\u7528\u5230CursorWrapper\u5bf9\u8c61\u53bb\u5305\u88c5\u6211\u4eec\u7684Cursor\u5bf9\u8c61\uff0c\u5b9e\u73b0\u6211\u4eec\u9700\u8981\u7684\u6570\u636e\u8f6c\u6362\u5de5\u4f5c\uff0c\u8fd9\u4e2aCursorWrapper\u5b9e\u9645\u4e0a\u662f\u5b9e\u73b0\u4e86Cursor\u63a5\u53e3\u3002\u6211\u4eec\u67e5\u8be2\u83b7\u53d6\u5230\u7684Cursor\u5176\u5b9e\u662fCursor\u7684\u5f15\u7528\uff0c\u800c\u7cfb\u7edf\u5b9e\u9645\u8fd4\u56de\u7ed9\u6211\u4eec\u7684\u5fc5\u7136\u662fCursor\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u6211\u4eec\u7528CursorWrapper\u5305\u88c5\u8fd9\u4e2a\u5b9e\u4f8b\uff0c\u7136\u540e\u518d\u4f7f\u7528SimpleCursorAdapter\u5c06\u7ed3\u679c\u663e\u793a\u5230\u5217\u8868\u4e0a\u3002<br \/>\nCursor\u7ed3\u679c\u96c6\u9700\u8981\u6ce8\u610f\u4e9b\u4ec0\u4e48\uff1a\u4e00\u4e2a\u6700\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u6211\u4eec\u7684\u7ed3\u679c\u96c6\u4e2d\u5fc5\u987b\u8981\u5305\u542b\u4e00\u4e2a\u201c_id\u201d\u7684\u5217\uff0c\u5426\u5219SimpleCursorAdapter\u5c31\u4f1a\u7ffb\u8138\u4e0d\u8ba4\u4eba\uff0c\u4e3a\u4ec0\u4e48\u4e00\u5b9a\u8981\u8fd9\u6837\u5462\uff1f\u56e0\u4e3a\u8fd9\u6e90\u4e8eSQLite\u7684\u89c4\u8303\uff0c\u4e3b\u952e\u4ee5\u201c_id\u201d\u4e3a\u6807\u51c6\u3002\u89e3\u51b3\u529e\u6cd5\u6709\u4e09\uff1a\u7b2c\u4e00\uff0c\u5efa\u8868\u65f6\u6839\u636e\u89c4\u8303\u53bb\u505a\uff1b\u7b2c\u4e8c\uff0c\u67e5\u8be2\u65f6\u7528\u522b\u540d\uff0c\u4f8b\u5982\uff1aSELECT id AS _id FROM person\uff1b\u7b2c\u4e09\uff0c\u5728CursorWrapper\u91cc\u505a\u6587\u7ae0\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nCursorWrapper cursorWrapper = new CursorWrapper(c) {\n    @Override\n    public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {\n        if (columnName.equals(&quot;_id&quot;)) {\n            return super.getColumnIndex(&quot;id&quot;);\n        }\n        return super.getColumnIndexOrThrow(columnName);\n    }\n};\n<\/pre>\n<p>\u5982\u679c\u8bd5\u56fe\u4eceCursorWrapper\u91cc\u83b7\u53d6\u201c_id\u201d\u5bf9\u5e94\u7684\u5217\u7d22\u5f15\uff0c\u6211\u4eec\u5c31\u8fd4\u56de\u67e5\u8be2\u7ed3\u679c\u91cc\u201cid\u201d\u5bf9\u5e94\u7684\u5217\u7d22\u5f15\u5373\u53ef\u3002<br \/>\n\u6700\u540e\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u7ed3\u679c\u5982\u4f55\uff1a<br \/>\n<a href=\"http:\/\/www.prayerlaputa.com\/wp-content\/uploads\/2016\/12\/1.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-242\" src=\"http:\/\/www.prayerlaputa.com\/wp-content\/uploads\/2016\/12\/1-200x300.png\" alt=\"\" width=\"200\" height=\"300\" \/><\/a> <a href=\"http:\/\/www.prayerlaputa.com\/wp-content\/uploads\/2016\/12\/2.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-243\" src=\"http:\/\/www.prayerlaputa.com\/wp-content\/uploads\/2016\/12\/2-300x119.png\" alt=\"\" width=\"300\" height=\"119\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6253\u7b97\u6574\u7406\u4e00\u4e0b\u6700\u8fd1\u4e24\u5e74\u505aandroid\u6240\u5b66\u5230\u7684\u4e1c\u897f\uff0c\u4ee3\u7801\u3001\u6587\u7ae0\u914d\u5957\u6574\u7406\u4e00\u4e0b\uff0c\u4f46\u662f\u53d1\u73b0\u81ea\u5df1\u5176\u5b9e\u7814\u7a76\u7684\u8fd8\u5f88\u80a4\u6d45\uff0c\u5199\u51fa <a href='http:\/\/prayerlaputa.com\/?p=237' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[2,6],"tags":[26,33],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/237"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=237"}],"version-history":[{"count":0,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/237\/revisions"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}