﻿{"id":1769,"date":"2012-02-09T16:41:46","date_gmt":"2012-02-09T08:41:46","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1769"},"modified":"2012-02-09T16:41:46","modified_gmt":"2012-02-09T08:41:46","slug":"android%e6%95%b0%e6%8d%ae%e5%ba%93sqlitedatabase%e4%bd%bf%e7%94%a8%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1769","title":{"rendered":"Android\u6570\u636e\u5e93(SQLiteDatabase)\u4f7f\u7528\u5c0f\u7ed3"},"content":{"rendered":"<p>\u4efb\u4f55\u7a0b\u5e8f\u79bb\u4e0d\u4e86\u6570\u636e\uff0c\u56e0\u6b64\u6570\u636e\u5b58\u50a8\u662f\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u73af\u8282\u3002<\/p>\n<p>Android\u4e2d\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u6709\u4e09\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u6587\u4ef6\u5b58\u50a8\uff0c\u4e00\u79cd\u662fSharedPreferences\uff0c\u6700\u540e\u4e00\u79cd\u5c31\u662fSQLiteDatabase.\u4e0b\u9762\u6765\u770b\u4e0b\u600e\u6837\u4f7f\u7528SQLiteDatabase.<\/p>\n<p>1.\u9996\u5148\u8981\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u8be5\u7c7b\u7ee7\u627f\u81eaandroid.database.sqlite.SQLiteOpenHelper\uff0c\u7531\u4e8e\u8fd9\u4e2a\u662fabstract class, \u56e0\u6b64\u4f60\u9700\u8981\u5b9e\u73b0\u8be5\u7c7b\u7684\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u4e2a\u662fonCreate(),\u4e00\u4e2a\u662fonUpgrade().\u6ce8\uff1a\u4e24\u4e2a\u65b9\u6cd5\u7684\u53c2\u6570\u90fd\u7701\u7565\u4e86\u3002<\/p>\n<p>\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: java\"> \r\npublic class MySQLhelper extends SQLiteOpenHelper {\r\n\tpublic static final String tableName = &quot;localContact&quot;;\r\n\tpublic static final String ID = &quot;_id&quot;;\r\n\tpublic static final String nameRow = &quot;name&quot;;\r\n\tpublic static final String numRow = &quot;mobileNum&quot;;\r\n\tpublic MySQLhelper(Context context, String name, CursorFactory factory, int version) {\r\n\t\tsuper(context, name, factory, version);\r\n\t}\r\n\t@Override\r\n\tpublic void onCreate(SQLiteDatabase db) {\r\n\t\tdb.execSQL(&quot;CREATE TABLE IF NOT EXISTS &quot;+tableName+&quot; (&quot;\/*+ID+&quot; INTEGER PRIMARY KEY,&quot;*\/+nameRow+&quot; VARCHAR,&quot;+numRow+&quot; VARCHAR)&quot;);\r\n\t}\r\n\t@Override\r\n\tpublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r\n\t\t\/\/ \u5220\u9664\u4ee5\u524d\u7684\u65e7\u8868\uff0c\u521b\u5efa\u4e00\u5f20\u65b0\u7684\u7a7a\u8868\r\n\t\tdb.execSQL(&quot;DROP TABLE IF EXISTS &quot; + tableName);\r\n\t\tonCreate(db);\r\n\t}\r\n}\r\n<\/pre>\n<p>2.\u5728activity\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aMySQLhelper\u7684\u5b9e\u4f8b<\/p>\n<pre class=\"brush: java\">\r\nMySQLhelper sqlHelper = new MySQLhelper(this,DBname,null,1);\r\nSQLiteDatabase sql = sqlHelper.getWritableDatabase();\r\n<\/pre>\n<p>\u6b64\u65f6\u4fbf\u83b7\u5f97\u4e86\u521a\u521a\u521b\u5efa\u7684\u6570\u636e\u5e93\u5b9e\u4f8b\u7684\u5199\u5165\u6743\u9650\u3002<\/p>\n<p>3.\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u5982Insert(),delete()\u7b49\uff0c\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u7684\u64cd\u4f5c<\/p>\n<p>4.\u793a\u4f8b\uff1a\u4f8b\u5982\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Insert()\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: java\">\r\nContentValues tcv = new ContentValues();\r\ntcv.put(MySQLhelper.nameRow, name);\r\ntcv.put(MySQLhelper.numRow, mobilePhone);\r\nsql.insert(MySQLhelper.tableName, null, tcv);\r\n<\/pre>\n<p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6267\u884cSQL\u8bed\u53e5\u7684\u65b9\u5f0f\u6765\u63d2\u5165:<\/p>\n<pre class=\"brush: java\">\r\nsql.execSQL(&quot;INSERT INTO &quot;+MySQLhelper.tableName+&quot; (&quot;+MySQLhelper.nameRow+&quot;,&quot;+MySQLhelper.numRow+&quot;) VALUES (&#039;&quot;+name+&quot;&#039;,&#039;&quot;+mobilePhone+&quot;&#039;)&quot;);\r\n<\/pre>\n<p>\u4e24\u79cd\u65b9\u5f0f\u6700\u540e\u7684\u6548\u679c\u90fd\u662f\u4e00\u6837\u7684\uff0c\u5176\u4ed6\u7684\u64cd\u4f5c\u90fd\u7c7b\u4f3c\uff0c\u4e0d\u4e00\u4e00\u5217\u4e3e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4efb\u4f55\u7a0b\u5e8f\u79bb\u4e0d\u4e86\u6570\u636e\uff0c\u56e0\u6b64\u6570\u636e\u5b58\u50a8\u662f\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u73af\u8282\u3002 Android\u4e2d\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u6709\u4e09\u79cd\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f\u6587\u4ef6\u5b58\u50a8\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[346],"tags":[500,499],"class_list":["post-1769","post","type-post","status-publish","format-standard","hentry","category-android","tag-sqlite","tag-sqlitedatabase"],"views":429,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1769"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1769\/revisions"}],"predecessor-version":[{"id":1770,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1769\/revisions\/1770"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}