﻿{"id":783,"date":"2012-04-15T22:07:50","date_gmt":"2012-04-15T14:07:50","guid":{"rendered":"http:\/\/nick.workao.org\/?p=783"},"modified":"2012-04-15T22:07:50","modified_gmt":"2012-04-15T14:07:50","slug":"%e4%b8%80%e4%ba%9b-sqlite%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/nick.txtcc.com\/index.php\/db\/783","title":{"rendered":"\u4e00\u4e9b SQLite\u6280\u5de7"},"content":{"rendered":"<p><strong>SQLite\u81ea\u589eID\u81ea\u6bb5<\/strong>\n\u4f7f\u7528\u65b9\u6cd5\u4e3a <strong>INTEGER PRIMARY KEY AUTOINCREMENT<\/strong>\n\u5982:<\/p>\n<div>CREATE\u00a0TABLE\u00a021andy\u00a0(\u00a0id\u00a0INTEGER\u00a0PRIMARY\u00a0KEY\u00a0AUTOINCREMENT,\u00a021andy\u00a0VARCHAR(100)\u00a0NOT\u00a0NULL,\u00a0date\u00a0DATE\u00a0);<\/div>\n<p>\u6ce8\u610f\u662fAUTOINCREMENT, \u548cMySQL\u7684\u5199\u6cd5\u4e0d\u4e00\u6837<\/p>\n<p><strong>\n<\/strong><\/p>\n<p><strong>SQLite\u6e05\u7a7a\u8868<\/strong>\nSQLite\u6ca1\u6709TRUNCATE\u6e05\u7a7a\u8868\u547d\u4ee4,\u6240\u4ee5\u53ea\u80fd\u8fd9\u6837<\/p>\n<div>DELETE\u00a0FROM\u00a021andy;<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>SQLite \u5220\u9664\u8bb0\u5f55\u540e, \u81ea\u589eID\u7f6e0<\/strong><\/p>\n<p>\u53ea\u80fd\u8fd9\u6837<\/p>\n<div>DELETE\u00a0FROM\u00a0sqlite_sequence;<\/div>\n<p>\u53e6\u5916, \u8fd9\u4e2asqlite_sequence\u53ef\u4ee5CRUD<\/p>\n<p><strong>\n<\/strong><\/p>\n<p><strong>SQLite \u5220\u9664\u8bb0\u5f55\u540e, \u4e0d\u4f1a\u91ca\u653e\u7a7a\u95f4<\/strong>\n\u5fc5\u987b\u50cf\u8fd9\u6837<\/p>\n<div>VACUUM<\/div>\n<p>SQLite \u5206\u9875\u67e5\u8be2\n\u6709\u4e24\u79cd\u5199\u6cd5:<\/p>\n<div>Select\u00a0*\u00a0From\u00a0Account\u00a0Limit\u00a09\u00a0Offset\u00a010;<\/div>\n<p>&nbsp;<\/p>\n<div>SELECT\u00a0*\u00a0FROM\u00a0Account\u00a0LIMIT\u00a010,\u00a09<\/div>\n<p>\u4ed6\u4eec\u4e24\u4e2a\u7684\u6548\u679c\u90fd\u662f\u4e00\u6837\u7684,\u5176\u4e2d\u7b2c\u4e00\u79cd\u5199\u6cd5\u6bd4\u8f83\u6e05\u6670\u660e\u4e86,\u5373\u8df3\u8fc710\u884c,\u8bfb\u53d6\u5176\u540e\u76849\u884c\u6570\u636e.<\/p>\n<p>SQLite\u6279\u91cf\u63d2\u5165\u6570\u636e\n\u5f88\u4e0d\u5e78\u7684\u4e8b\u60c5\u662f\u8c8c\u4f3cSQLite\u53ea\u80fd\u4e00\u6761\u4e00\u6761\u7684 \u6267\u884c\u63d2\u5165,\u4f46\u662f\u8fd9\u662f\u975e\u5e38\u975e\u5e38\u6162\u7684\u884c\u4e3a,\u6267\u884c\u4e00\u6761\u5c31\u662f\u6267\u884c\u4e00\u6b21\u5199\u5165\u78c1\u76d8\u7684\u64cd\u4f5c,\u8fd9\u5b9e\u5728\u662f\u592a\u53ef\u6015\u4e86.\u5728SQLite\u91cc\u9762\u6267\u884c\u6279\u91cf\u63d2\u5165,\u53ea\u80fd\u5c06\u63d2\u5165\u64cd\u4f5c\u653e\u5165\u5230\u4e8b \u52a1\u5f53\u4e2d\u53bb.\u793a\u4f8b\u5982\u4e0b:<\/p>\n<div>BEGIN;\nCREATE\u00a0TABLE\u00a0t2(a\u00a0INTEGER,\u00a0b\u00a0INTEGER,\u00a0c\u00a0VARCHAR(100));\nINSERT\u00a0INTO\u00a0t2\u00a0VALUES(1,59672,&#8217;fifty\u00a0nine\u00a0thousand\u00a0six\u00a0hundred\u00a0seventy\u00a0two&#8217;);\nINSERT\u00a0INTO\u00a0t2\u00a0VALUES(24999,89569,&#8217;eighty\u00a0nine\u00a0thousand\u00a0five\u00a0hundred\u00a0sixty\u00a0nine&#8217;);\nINSERT\u00a0INTO\u00a0t2\u00a0VALUES(25000,94666,&#8217;ninety\u00a0four\u00a0thousand\u00a0six\u00a0hundred\u00a0sixty\u00a0six&#8217;);\nCOMMIT;<\/div>\n<p>\u5728SQLite\u5f53\u4e2d\u4e00\u7cfb\u5217\u8981\u8fdb\u884c\u591a\u6b21\u5199\u5165\u64cd\u4f5c\u7684\u65f6\u5019,\u5efa\u8bae\u653e\u5165\u5230\u4e8b\u52a1\u5f53\u4e2d\u53bb,\u8fd9\u4e2a\u4f18\u5316\u7684\u6027\u80fd\u63d0\u5347\u662f\u53ef\u4ee5\u5f88\u660e\u663e\u611f\u89c9\u5230\u7684.\u7528\u4e0e\u4e0d\u7528\u7684\u5dee\u522b\u662f\u975e\u5e38\u5927\u7684.<\/p>\n<p><strong>SQLite \u6570\u636e\u4e0d\u5b58\u5728INSERT,\u5b58\u5728UPDATE<\/strong>\nSQLite\u7684SQL\u8bed\u6cd5\u7c7bMySQL,\u5728SQLite\u91cc\u9762\u6709\u4e00\u4e2a\u5173\u952e\u5b57 REPLACE,\u53ef\u4ee5\u4f7f\u7528\u5b83\u8fbe\u5230\u76ee\u7684:<\/p>\n<div>REPLACE\u00a0INTO\u00a0[table]\u00a0(row1,\u00a0row2)\u00a0VALUES\u00a0(2,\u00a03);<\/div>\n<p>\u5982\u679c\u8be5\u8868\u6709\u4e00\u4e2a\u4e3b\u952e,\u90a3\u4e48\u5f53\u4e3b\u952e\u503c\u76f8\u7b49\u7684\u65f6\u5019,\u8be5\u884c\u6570\u636e\u4e0d\u5b58\u5728\u6267\u884c\u63d2\u5165,\u5b58\u5728\u5219\u6267\u884c\u66f4\u65b0\u64cd\u4f5c.\u4f46\u662f\u5982\u679c\u6ca1\u6709\u4e3b\u952e,\u90a3\u4e48\u5b83\u5c31\u6267\u884c\u7684\u6c38\u8fdc\u90fd\u662f\u63d2\u5165\u64cd\u4f5c\u4e86.<\/p>\n<p><strong>SQLite \u4f7f\u7528ISNULL\u51fd\u6570\n<\/strong>SQLite\u7684SQL\u8bed\u6cd5\u7c7b\u4e0d\u540c\u4e8eMySQL, \u4e0d\u53ef\u4ee5\u7528ISNULL,\u4f46\u53ef\u4ee5\u7528 IFNULL \u76f4\u63a5\u4ee3\u66ff<\/p>\n<div>ifnull(null,0)<\/div>\n<p>&nbsp;<\/p>\n<p><strong>SQLite\u505a\u67e5\u8be2\u5206\u9875<\/strong><\/p>\n<p>\u7528SQLite\u5206\u9875\u662f\u4e16\u754c\u4e0a\u6700\u7b80\u5355\u7684\u5206\u9875\u65b9\u6cd5\u3002SQLite \u5206\u9875\u5904\u7406\u53ef\u901a\u8fc7. limit \u548c offset \u5b50\u53e5\u5b9e\u73b0\u3002\u4f8b\u5982\u6211\u8981\u53d6Account\u8868\u4e2d\u7684\u7b2c11-20\u884c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u5982\u4e0b\u5b9e\u73b0\uff1a<\/p>\n<p>Select * From Account Limit 9 Offset 10<\/p>\n<p>\u4ee5\u4e0a\u8bed\u53e5\u8868\u793a\u4eceAccount\u8868\u83b7\u53d6\u6570\u636e\uff0c\u8df3\u8fc710\u884c\uff0c\u53d69\u884c\u3002<\/p>\n<p>Selectcount(*) From Account<\/p>\n<p>\u4ee5\u4e0a\u8bed\u53e5\u8fd4\u56de\u67e5\u8be2\u4e2d\u7684\u603b\u8bb0\u5f55\u6570\u3002<\/p>\n<p><strong>\u00a0\u5982\u4f55\u8ba9sqlite\u652f\u6301\u6d6e\u70b9\u8fd0\u7b97<\/strong><\/p>\n<p>sqlite> select 1.0\/7;\n0.142857142857143\nsqlite> select 1\/7;\n0\nsqlite><\/p>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/p>\n<p>.head on<\/p>\n<p>.mode column<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003<\/p>\n<ol>\n<li>\n<p><a href=\"http:\/\/www.21andy.com\/blog\/20090512\/1272.html\">http:\/\/www.21andy.com\/blog\/20090512\/1272.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/i.cnsoftware.cn\/space-3-do-thread-id-35.html\">http:\/\/i.cnsoftware.cn\/space-3-do-thread-id-35.html<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"http:\/\/www.cnblogs.com\/devsdk\/archive\/2009\/04\/03\/1428615.html\">http:\/\/www.cnblogs.com\/devsdk\/archive\/2009\/04\/03\/1428615.html<\/a><\/p>\n<\/li>\n<\/ol>\n<p>4\u3001<a href=\"http:\/\/jsjx.jsit.edu.cn\/sxsoftservice\/Article_Show.asp?ArticleID=666\">http:\/\/jsjx.jsit.edu.cn\/sxsoftservice\/Article_Show.asp?ArticleID=666<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>SQLite\u81ea\u589eID\u81ea\u6bb5 \u4f7f\u7528\u65b9\u6cd5\u4e3a INTEGER PRIMARY KEY AUTOINCREMENT \u5982: CREATE\u00a0TABLE\u00a021andy\u00a0(\u00a0id\u00a0INTEGER\u00a0PRIMARY\u00a0KEY&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-db"],"_links":{"self":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":2,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/783\/revisions\/785"}],"wp:attachment":[{"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}