{"id":275,"date":"2021-04-09T10:44:00","date_gmt":"2021-04-09T02:44:00","guid":{"rendered":"https:\/\/cn1.club\/?p=275"},"modified":"2025-03-15T22:50:32","modified_gmt":"2025-03-15T14:50:32","slug":"sqlite%e7%9a%84%e5%ad%97%e6%ae%b5%e7%b1%bb%e5%9e%8b%e5%8f%8a%e7%b1%bb%e5%9e%8b%e4%ba%b2%e5%92%8c%e6%80%a7","status":"publish","type":"post","link":"https:\/\/cn1.club\/?p=275","title":{"rendered":"sqlite\u7684\u5b57\u6bb5\u7c7b\u578b\u53ca\u7c7b\u578b\u4eb2\u548c\u6027"},"content":{"rendered":"\n<p><strong>\u5b58\u50a8\u7c7b(Storage Classes)<\/strong><br>\u5982\u524d\u6587\u6240\u8ff0\uff0cSQLite\u5728\u5904\u7406\u6570\u636e\u7c7b\u578b\u65f6\u4e0e\u5176\u5b83\u7684\u6570\u636e\u5e93\u4e0d\u540c\u3002\u533a\u522b\u5728\u4e8e\u5b83\u6240\u652f\u6301\u7684\u7c7b\u578b\u4ee5\u53ca\u8fd9\u4e9b\u7c7b\u578b\u662f\u5982\u4f55\u5b58\u50a8\u3001\u6bd4\u8f83\u3001\u5f3a\u5316(enforc)\u548c\u6307\u6d3e(assign)\u3002\u4e0b\u9762\u5404\u8282\u4ecb\u7ecdSQLite\u5904\u7406\u6570\u636e\u7c7b\u578b\u7684\u72ec\u7279\u65b9\u6cd5\u548c\u5b83\u4e0e\u57df\u5b8c\u6574\u6027\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u6570\u636e\u7c7b\u578b\uff0cSQLite\u7684\u57df\u5b8c\u6574\u6027\u88ab\u79f0\u4e3a\u57df\u4eb2\u548c\u6027(affinity)\u66f4\u5408\u9002\u3002\u5728SQLite\u4e2d\uff0c\u5b83\u88ab\u79f0\u4e3a\u7c7b\u578b\u4eb2\u548c\u6027(type affinity)\u3002\u4e3a\u4e86\u7406\u89e3\u7c7b\u578b\u4eb2\u548c\u6027\uff0c\u4f60\u5fc5\u987b\u5148\u8981\u7406\u89e3\u5b58\u50a8\u7c7b\u548c\u5f31\u7c7b\u578b(manifesttyping)\u3002<\/p>\n\n\n\n<p>SQLite\u67095\u4e2a\u539f\u59cb\u7684\u6570\u636e\u7c7b\u578b\uff0c\u88ab\u79f0\u4e3a\u5b58\u50a8\u7c7b\u3002\u5b58\u50a8\u7c7b\u8fd9\u4e2a\u8bcd\u8868\u660e\u4e86\u4e00\u4e2a\u503c\u5728\u78c1\u76d8\u4e0a\u5b58\u50a8\u7684\u683c\u5f0f\uff0c\u5176\u5b9e\u5c31\u662f\u7c7b\u578b\u6216\u6570\u636e\u7c7b\u578b\u7684\u540c\u4e49\u8bcd\u3002\u8fd95\u4e2a\u5b58\u50a8\u7c7b\u5728\u88684-6\u4e2d\u63cf\u8ff0\u3002<\/p>\n\n\n\n<p> <strong>SQLite\u5b58\u50a8\u7c7b<\/strong><\/p>\n\n\n\n<p>\u8bf4\u660e<br><strong>INTEGER<\/strong><br>\u6574\u6570\u503c\u662f\u5168\u6570\u5b57(\u5305\u62ec\u6b63\u548c\u8d1f)\u3002\u6574\u6570\u53ef\u4ee5\u662f1, 2, 3, 4, 6\u6216 8\u5b57\u8282\u3002\u6574\u6570\u7684\u6700\u5927\u8303\u56f4(8 bytes)\u662f{-9223372036854775808, 0, +9223372036854775807}\u3002SQLite\u6839\u636e\u6570\u5b57\u7684\u503c\u81ea\u52a8\u63a7\u5236\u6574\u6570\u6240\u5360\u7684\u5b57\u8282\u6570\u3002<br>\u7a7a\u6ce8\uff1a\u53c2\u53ef\u53d8\u957f\u6574\u6570\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p><strong>REAL<\/strong><br>\u5b9e\u6570\u662f10\u8fdb\u5236\u7684\u6570\u503c\u3002SQLite\u4f7f\u75288\u5b57\u8282\u7684\u7b26\u70b9\u6570\u6765\u5b58\u50a8\u5b9e\u6570\u3002<\/p>\n\n\n\n<p><strong>TEXT<\/strong><br>\u6587\u672c(TEXT)\u662f\u5b57\u7b26\u6570\u636e\u3002SQLite\u652f\u6301\u51e0\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u5305\u62ecUTF-8\u548cUTF-16\u3002\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u6ca1\u6709\u9650\u5236\u3002<\/p>\n\n\n\n<p><strong>BLOB<\/strong><br>\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61(BLOB)\u662f\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u3002BLOB\u7684\u5927\u5c0f\u6ca1\u6709\u9650\u5236\u3002<\/p>\n\n\n\n<p><strong>NULL<\/strong><\/p>\n\n\n\n<p>NULL\u8868\u793a\u6ca1\u6709\u503c\u3002SQLite\u5177\u6709\u5bf9NULL\u7684\u5b8c\u5168\u652f\u6301\u3002<br><br>SQLite\u901a\u8fc7\u503c\u7684\u8868\u793a\u6cd5\u6765\u5224\u65ad\u5176\u7c7b\u578b\uff0c\u4e0b\u9762\u5c31\u662fSQLite\u7684\u63a8\u7406\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>l&nbsp; SQL\u8bed\u53e5\u4e2d\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u6587\u5b57\u88ab\u6307\u6d3e\u4e3aTEXT\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u6587\u5b57\u662f\u672a\u7528\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u6570\u636e\uff0c\u5e76\u4e14\u6ca1\u6709\u5c0f\u6570\u70b9\u548c\u6307\u6570\uff0c\u88ab\u6307\u6d3e\u4e3aINTEGER\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u6587\u5b57\u662f\u672a\u7528\u5f15\u53f7\u62ec\u8d77\u6765\u7684\u6570\u636e\uff0c\u5e76\u4e14\u5e26\u6709\u5c0f\u6570\u70b9\u6216\u6307\u6570\uff0c\u88ab\u6307\u6d3e\u4e3aREAL\u3002<\/p>\n\n\n\n<p>l&nbsp; \u7528NULL\u8bf4\u660e\u7684\u503c\u88ab\u6307\u6d3e\u4e3aNULL\u5b58\u50a8\u7c7b\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u4e00\u4e2a\u503c\u7684\u683c\u5f0f\u4e3aX'ABCD'\uff0c\u5176\u4e2dABCD\u4e3a16\u8fdb\u5236\u6570\u5b57\uff0c\u5219\u8be5\u503c\u88ab\u6307\u6d3e\u4e3aBLOB\u3002X\u524d\u7f00\u5927\u5c0f\u5199\u7686\u53ef\u3002<\/p>\n\n\n\n<p>SQL\u51fd\u6570typeof()\u6839\u636e\u503c\u7684\u8868\u793a\u6cd5\u8fd4\u56de\u5176\u5b58\u50a8\u7c7b\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4e0b\u9762SQL\u8bed\u53e5\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>sqlite&gt; select typeof(3.14),typeof('3.14'), typeof(314), typeof(x'3142'), typeof(NULL);<\/p>\n\n\n\n<p>typeof(3.14)&nbsp; &nbsp;typeof('3.14')&nbsp; typeof(314)&nbsp; &nbsp; typeof(x'3142')&nbsp; &nbsp; &nbsp; &nbsp;typeof(NULL)<\/p>\n\n\n\n<p>real&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blob&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null<\/p>\n\n\n\n<p>SQLite\u5355\u72ec\u7684\u4e00\u4e2a\u5b57\u6bb5\u53ef\u80fd\u5305\u542b\u4e0d\u540c\u5b58\u50a8\u7c7b\u7684\u503c\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>sqlite&gt; DROP TABLE domain;<\/p>\n\n\n\n<p>sqlite&gt; CREATE TABLE domain(x);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(3.142);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES('3.142');<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(3142);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(x'3142');<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(NULL);<\/p>\n\n\n\n<p>sqlite&gt; SELECT ROWID, x, typeof(x) FROMdomain;<\/p>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<p>rowid&nbsp; &nbsp; &nbsp; x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; typeof(x)<\/p>\n\n\n\n<p>1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.142&nbsp; &nbsp; &nbsp; real<\/p>\n\n\n\n<p>2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.142&nbsp; &nbsp; &nbsp; text<\/p>\n\n\n\n<p>3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3142&nbsp; &nbsp; &nbsp;integer<\/p>\n\n\n\n<p>4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1B&nbsp; &nbsp; &nbsp; &nbsp; blob<\/p>\n\n\n\n<p>5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL&nbsp; &nbsp; null<\/p>\n\n\n\n<p>\u8fd9\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002\u8fd9\u79cd\u5b57\u6bb5\u4e2d\u7684\u503c\u5982\u4f55\u5b58\u50a8\u548c\u6bd4\u8f83\uff1f\u5982\u4f55\u5bf9\u4e00\u4e2a\u5305\u542b\u4e86INTEGER\u3001REAL\u3001TEXT\u3001BLOB\u548cNULL\u503c\u7684\u5b57\u6bb5\u6392\u5e8f\uff1f\u4e00\u4e2a\u6574\u6570\u548c\u4e00\u4e2aBLOB\u5982\u4f55\u6bd4\u8f83\uff1f\u54ea\u4e2a\u66f4\u5927\uff1f\u5b83\u4eec\u80fd\u76f8\u7b49\u5417\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\uff1a\u5177\u6709\u4e0d\u540c\u5b58\u50a8\u7c7b\u7684\u503c\u53ef\u4ee5\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u5b57\u6bb5\u4e2d\u3002\u53ef\u4ee5\u88ab\u6392\u5e8f\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u503c\u53ef\u4ee5\u76f8\u4e92\u6bd4\u8f83\u3002\u6709\u5b8c\u5584\u5b9a\u4e49\u7684\u89c4\u5219\u6765\u505a\u8fd9\u4ef6\u4e8b\u3002\u4e0d\u540c\u5b58\u50a8\u7c7b\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u5b83\u4eec\u5404\u81ea\u7c7b\u7684\u201c\u7c7b\u503c\u201d\u8fdb\u884c\u6392\u5e8f\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>1. NULL\u5b58\u50a8\u7c7b\u5177\u6709\u6700\u4f4e\u7684\u7c7b\u503c\u3002\u4e00\u4e2a\u5177\u6709NULL\u5b58\u50a8\u7c7b\u7684\u503c\u6bd4\u6240\u6709\u5176\u5b83\u503c\u90fd\u5c0f(\u5305\u62ec\u5176\u5b83\u5177\u6709NULL\u5b58\u50a8\u7c7b\u7684\u503c)\u3002\u5728NULL\u503c\u4e4b\u95f4\uff0c\u6ca1\u6709\u7279\u522b\u7684\u53ef\u6392\u5e8f\u503c\u3002<\/p>\n\n\n\n<p>2. INTEGER\u6216REAL\u5b58\u50a8\u7c7b\u503c\u9ad8\u4e8eNULL\uff0c\u5b83\u4eec\u7684\u7c7b\u503c\u76f8\u7b49\u3002INTEGER\u503c\u548cREAL\u503c\u901a\u8fc7\u5176\u6570\u503c\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>3. TEXT\u5b58\u50a8\u7c7b\u7684\u503c\u6bd4INTEGER\u548cREAL\u9ad8\u3002\u6570\u503c\u6c38\u8fdc\u6bd4\u5b57\u7b26\u4e32\u7684\u503c\u4f4e\u3002\u5f53\u4e24\u4e2aTEXT\u503c\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u5176\u503c\u5927\u5c0f\u7531\u201c\u6392\u5e8f\u6cd5\u201d\u51b3\u5b9a\u3002<\/p>\n\n\n\n<p>4. BLOB\u5b58\u50a8\u7c7b\u5177\u6709\u6700\u9ad8\u7684\u7c7b\u503c\u3002\u5177\u6709BLOB\u7c7b\u7684\u503c\u5927\u4e8e\u5176\u5b83\u6240\u6709\u7c7b\u7684\u503c\u3002BLOB\u503c\u4e4b\u95f4\u5728\u6bd4\u8f83\u65f6\u4f7f\u7528C\u51fd\u6570memcmp()\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5f53SQLite\u5bf9\u4e00\u4e2a\u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\u65f6\uff0c\u9996\u5148\u6309\u5b58\u50a8\u7c7b\u6392\u5e8f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u7c7b\u5185\u7684\u6392\u5e8f (NULL\u7c7b\u5185\u90e8\u5404\u503c\u4e0d\u5fc5\u6392\u5e8f) \u3002\u4e0b\u9762\u7684SQL\u8bf4\u660e\u4e86\u5b58\u50a8\u7c7b\u503c\u7684\u4e0d\u540c\uff1a<\/p>\n\n\n\n<p>sqlite&gt; SELECT 3 &lt; 3.142, 3.142 &lt;'3.142', '3.142' &lt; x'3000', x'3000' &lt; x'3001';<\/p>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<p>3 &lt; 3.142&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.142 &lt; '3.142'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '3.142'&lt; x'3000'&nbsp; &nbsp; &nbsp;x'3000' &lt; x'3001'<\/p>\n\n\n\n<p>1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1<\/p>\n\n\n\n<p>\u5f31\u7c7b\u578b(manifest typing)<br>SQLite\u4f7f\u7528\u5f31\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u770b\u4e0b\u9762\u7684\u8868\uff1a<\/p>\n\n\n\n<p>CREATE TABLE foo( x integer,<\/p>\n\n\n\n<p>&nbsp; &nbsp; y text, z real );<\/p>\n\n\n\n<p>\u5411\u8be5\u8868\u63d2\u5165\u4e00\u6761\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<p>INSERT INTO foo VALUES ('1', '1', '1');<\/p>\n\n\n\n<p>\u5f53SQLite\u521b\u5efa\u8fd9\u6761\u8bb0\u5f55\u65f6\uff0cx\u3001y\u548cz\u8fd93\u4e2a\u5b57\u6bb5\u4e2d\u5b58\u50a8\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u7b54\u6848\u662fINTEGER, TEXT\u548cREAL\u3002<\/p>\n\n\n\n<p>\u518d\u770b\u4e0b\u9762\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>CREATE TABLE foo(x, y, z);<\/p>\n\n\n\n<p>\u73b0\u5728\u6267\u884c\u540c\u6837\u7684\u63d2\u5165\u8bed\u53e5\uff1a<\/p>\n\n\n\n<p>INSERT INTO foo VALUES ('1', '1', '1');<\/p>\n\n\n\n<p>\u73b0\u5728\uff0cx\u3001y\u548cz\u4e2d\u5b58\u50a8\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u7b54\u6848\u662fTEXT\u3001TEXT\u548cTEXT\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\uff0c\u662f\u5426SQLite\u7684\u5b57\u6bb5\u7c7b\u578b\u9ed8\u8ba4\u4e3aTEXT\u5462\uff1f\u518d\u770b\uff0c\u8fd8\u662f\u7b2c2\u4e2a\u8868\uff0c\u6267\u884c\u5982\u4e0b\u63d2\u5165\u8bed\u53e5\uff1a<\/p>\n\n\n\n<p>INSERT INTO foo VALUES (1, 1.0, x'10');<\/p>\n\n\n\n<p>\u73b0\u5728\uff0cx\u3001y\u548cz\u4e2d\u5b58\u50a8\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5462\uff1f\u7b54\u6848\u662fINTEGER\u3001REAL\u548cBLOB\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u613f\u610f\uff0c\u53ef\u4ee5\u4e3aSQLite\u7684\u5b57\u6bb5\u5b9a\u4e49\u7c7b\u578b\uff0c\u8fd9\u770b\u8d77\u6765\u8ddf\u5176\u5b83\u6570\u636e\u5e93\u4e00\u6837\u3002\u4f46\u8fd9\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u4f60\u53ef\u4ee5\u5c3d\u7ba1\u8fdd\u53cd\u7c7b\u578b\u5b9a\u4e49\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\uff0cSQLite\u90fd\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u503c\u5e76\u63a8\u65ad\u5b83\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u603b\u4e4b\uff0cSQLite\u7684\u5f31\u7c7b\u578b\u53ef\u8868\u793a\u4e3a\uff1a1)\u5b57\u6bb5\u53ef\u4ee5\u6709\u7c7b\u578b\uff0c2)\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u503c\u6765\u63a8\u65ad\u3002\u7c7b\u578b\u4eb2\u548c\u6027\u4ecb\u7ecd\u8fd9\u4e24\u4e2a\u89c4\u5b9a\u5982\u4f55\u76f8\u4e92\u5173\u8054\u3002\u6240\u8c13\u7c7b\u578b\u4eb2\u548c\u6027\u5c31\u662f\u5728\u5f3a\u7c7b\u578b(strict typing)\u548c\u52a8\u6001\u7c7b\u578b(dynamic typing)\u4e4b\u95f4\u7684\u5e73\u8861\u827a\u672f\u3002<\/p>\n\n\n\n<p>\u7c7b\u578b\u4eb2\u548c\u6027(Type Affinity)<br>\u5728SQLite\u4e2d\uff0c\u5b57\u6bb5\u6ca1\u6709\u7c7b\u578b\u6216\u57df\u3002\u5f53\u7ed9\u4e00\u4e2a\u5b57\u6bb5\u58f0\u660e\u4e86\u7c7b\u578b\uff0c\u8be5\u5b57\u6bb5\u5b9e\u9645\u4e0a\u4ec5\u4ec5\u5177\u6709\u4e86\u8be5\u7c7b\u578b\u7684\u65b0\u548c\u6027\u3002\u58f0\u660e\u7c7b\u578b\u548c\u7c7b\u578b\u4eb2\u548c\u6027\u662f\u4e24\u56de\u4e8b\u3002\u7c7b\u578b\u4eb2\u548c\u6027\u9884\u5b9a SQLite\u7528\u4ec0\u4e48\u5b58\u50a8\u7c7b\u5728\u5b57\u6bb5\u4e2d\u5b58\u50a8\u503c\u3002\u5728\u5b58\u50a8\u4e00\u4e2a\u7ed9\u5b9a\u7684\u503c\u65f6\u5230\u5e95SQLite\u4f1a\u5728\u8be5\u5b57\u6bb5\u4e2d\u7528\u4ec0\u4e48\u5b58\u50a8\u7c7b\u51b3\u5b9a\u4e8e\u503c\u7684\u5b58\u50a8\u7c7b\u548c\u5b57\u6bb5\u4eb2\u548c\u6027\u7684\u7ed3\u5408\u3002\u6211\u4eec\u5148\u6765\u4ecb\u7ecd\u4e00\u4e0b\u5b57\u6bb5\u5982\u4f55\u83b7\u5f97\u5b83\u7684\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>\u5b57\u6bb5\u7c7b\u578b\u548c\u4eb2\u548c\u6027<br>\u9996\u5148\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u5177\u6709\u4e00\u79cd\u4eb2\u548c\u6027\u3002\u5171\u6709\u56db\u79cd\u4eb2\u548c\u6027\uff1aNUMERIC\u3001INTEGER\u3001TEXT\u548cNONE\u3002\u4e00\u4e2a\u5b57\u6bb5\u7684\u4eb2\u548c\u6027\u7531\u5b83\u9884\u58f0\u660e\u7684\u7c7b\u578b\u51b3\u5b9a\u3002\u6240\u4ee5\uff0c\u5f53\u4f60\u4e3a\u5b57\u6bb5\u58f0\u660e\u4e86\u7c7b\u578b\uff0c\u4ece\u6839\u672c\u4e0a\u8bf4\u662f\u4e3a\u5b57\u6bb5\u6307\u5b9a\u4e86\u4eb2\u548c\u6027\u3002SQLite\u6309\u4e0b\u9762\u7684\u89c4\u5219\u4e3a\u5b57\u6bb5\u6307\u6d3e\u4eb2\u548c\u6027\uff1a<\/p>\n\n\n\n<p>l&nbsp; \u9ed8\u8ba4\u7684\uff0c\u4e00\u4e2a\u5b57\u6bb5\u9ed8\u8ba4\u7684\u4eb2\u548c\u6027\u662fNUMERIC\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u4e0d\u662fINTEGER\u3001TEXT\u6216NONE\u7684\uff0c\u90a3\u5b83\u81ea\u52a8\u5730\u88ab\u6307\u6d3e\u4e3aNUMERIC\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u4e3a\u5b57\u6bb5\u58f0\u660e\u7684\u7c7b\u578b\u4e2d\u5305\u542b\u4e86'INT'(\u65e0\u8bba\u5927\u5c0f\u5199)\uff0c\u8be5\u5b57\u6bb5\u88ab\u6307\u6d3e\u4e3aINTEGER\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u4e3a\u5b57\u6bb5\u58f0\u660e\u7684\u7c7b\u578b\u4e2d\u5305\u542b\u4e86'CHAR'\u3001'CLOB'\u6216'TEXT'(\u65e0\u8bba\u5927\u5c0f\u5199)\uff0c\u8be5\u5b57\u6bb5\u88ab\u6307\u6d3e\u4e3aTEXT\u4eb2\u548c\u6027\u3002\u5982'VARCHAR'\u5305\u542b\u4e86'CHAR'\uff0c\u6240\u4ee5\u88ab\u6307\u6d3e\u4e3aTEXT\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5982\u679c\u4e3a\u5b57\u6bb5\u58f0\u660e\u7684\u7c7b\u578b\u4e2d\u5305\u542b\u4e86'BLOB'(\u65e0\u8bba\u5927\u5c0f\u5199)\uff0c\u6216\u8005\u6ca1\u6709\u4e3a\u8be5\u5b57\u6bb5\u58f0\u660e\u7c7b\u578b\uff0c\u8be5\u5b57\u6bb5\u88ab\u6307\u6d3e\u4e3aNONE\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u6ca1\u6709\u4e3a\u5b57\u6bb5\u58f0\u660e\u7c7b\u578b\uff0c\u8be5\u5b57\u6bb5\u7684\u4eb2\u548c\u6027\u4e3aNONE\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u503c\u90fd\u5c06\u4ee5\u5b83\u4eec\u672c\u8eab\u7684(\u6216\u4ece\u5b83\u4eec\u7684\u8868\u793a\u6cd5\u4e2d\u63a8\u65ad\u7684)\u5b58\u50a8\u7c7b\u5b58\u50a8\u3002\u5982\u679c\u4f60\u6682\u65f6\u8fd8\u4e0d\u786e\u5b9a\u8981\u5f80\u4e00\u4e2a\u5b57\u6bb5\u91cc\u653e\u4ec0\u4e48\u5185\u5bb9\uff0c\u6216\u51c6\u5907\u5c06\u6765\u4fee\u6539\uff0c\u7528NONE\u4eb2\u548c\u6027\u662f\u4e00\u4e2a\u597d\u7684\u9009\u62e9\u3002\u4f46SQLite\u9ed8\u8ba4\u7684\u4eb2\u548c\u6027\u662fNUMERIC\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e3a\u4e00\u5b9a\u5b57\u6bb5\u58f0\u660e\u4e86\u7c7b\u578bJUJYFRUIT\uff0c\u8be5\u5b57\u6bb5\u7684\u4eb2\u548c\u6027\u4e0d\u662fNONE\uff0c\u56e0\u4e3aSQLite\u4e0d\u8ba4\u8bc6\u8fd9\u79cd\u7c7b\u578b\uff0c\u4f1a\u7ed9\u5b83\u6307\u6d3e\u9ed8\u8ba4\u7684NUMERIC\u4eb2\u548c\u6027\u3002\u6240\u4ee5\uff0c\u4e0e\u5176\u7528\u4e00\u4e2a\u4e0d\u8ba4\u8bc6\u7684\u7c7b\u578b\u6700\u7ec8\u5f97\u5230NUMERIC\u4eb2\u548c\u6027\uff0c\u8fd8\u4e0d\u5982\u4e0d\u4e3a\u5b83\u6307\u5b9a\u7c7b\u578b\uff0c\u4ece\u800c\u4f7f\u5b83\u5f97\u5230NONE\u4eb2\u548c\u6027\u3002<\/p>\n\n\n\n<p>\u4eb2\u548c\u6027\u548c\u5b58\u50a8<br>\u4eb2\u548c\u6027\u5bf9\u503c\u5982\u4f55\u5b58\u50a8\u5230\u5b57\u6bb5\u6709\u5f71\u54cd\uff0c\u89c4\u5219\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>l&nbsp; \u4e00\u4e2aNUMERIC\u5b57\u6bb5\u53ef\u80fd\u5305\u62ec\u6240\u67095\u79cd\u5b58\u50a8\u7c7b\u3002\u4e00\u4e2aNUMERIC\u5b57\u6bb5\u5177\u6709\u6570\u5b57\u5b58\u50a8\u7c7b\u7684\u504f\u597d(INTEGER\u548cREAL)\u3002\u5f53\u4e00\u4e2aTEXT\u503c\u88ab\u63d2\u5165\u5230\u4e00\u4e2aNUMERIC\u5b57\u6bb5\uff0c\u5c06\u4f1a\u8bd5\u56fe\u5c06\u5176\u8f6c\u5316\u4e3aINTEGER\u5b58\u50a8\u7c7b\uff1b\u5982\u679c\u8f6c\u5316\u5931\u8d25\uff0c\u5c06\u4f1a\u8bd5\u56fe\u5c06\u5176\u8f6c\u5316\u4e3aREAL\u5b58\u50a8\u7c7b\uff1b\u5982\u679c\u8fd8\u662f\u5931\u8d25\uff0c\u5c06\u4f1a\u7528TEXT\u5b58\u50a8\u7c7b\u6765\u5b58\u50a8\u3002<\/p>\n\n\n\n<p>l&nbsp; \u4e00\u4e2aINTEGER\u5b57\u6bb5\u7684\u5904\u7406\u5f88\u50cfNUMERIC\u5b57\u6bb5\u3002\u4e00\u4e2aINTEGER\u5b57\u6bb5\u4f1a\u5c06REAL\u503c\u6309REAL\u5b58\u50a8\u7c7b\u5b58\u50a8\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u8fd9\u4e2aREAL\u503c\u6ca1\u6709\u5c0f\u6570\u90e8\u5206\uff0c\u5c31\u4f1a\u88ab\u8f6c\u5316\u4e3aINTEGER\u5b58\u50a8\u7c7b\u3002INTEGER\u5b57\u6bb5\u5c06\u4f1a\u8bd5\u7740\u5c06TEXT\u503c\u6309REAL\u5b58\u50a8\uff1b\u5982\u679c\u8f6c\u5316\u5931\u8d25\uff0c\u5c06\u4f1a\u8bd5\u56fe\u5c06\u5176\u8f6c\u5316\u4e3aINTEGER\u5b58\u50a8\u7c7b\uff1b\u5982\u679c\u8fd8\u662f\u5931\u8d25\uff0c\u5c06\u4f1a\u7528TEXT\u5b58\u50a8\u7c7b\u6765\u5b58\u50a8\u3002<\/p>\n\n\n\n<p>l&nbsp; \u4e00\u4e2aTEXT\u5b57\u6bb5\u5c06\u4f1a\u628a\u6240\u6709\u7684INTEGER\u6216REAL\u503c\u8f6c\u5316\u4e3aTEXT\u3002<\/p>\n\n\n\n<p>l&nbsp; \u4e00\u4e2aNONE\u5b57\u6bb5\u4e0d\u8bd5\u56fe\u505a\u4efb\u4f55\u7c7b\u578b\u8f6c\u5316\u3002\u6240\u6709\u503c\u6309\u5b83\u4eec\u672c\u8eab\u7684\u5b58\u50a8\u7c7b\u5b58\u50a8\u3002<\/p>\n\n\n\n<p>l&nbsp; \u6ca1\u6709\u5b57\u6bb5\u8bd5\u56fe\u5411NULL\u6216BLOB\u503c\u8f6c\u5316\u2014\u2014\u5982\u65e0\u8bba\u7528\u4ec0\u4e48\u4eb2\u548c\u6027\u3002NULL\u548cBLOB\u503c\u6c38\u8fdc\u90fd\u6309\u672c\u6765\u7684\u65b9\u5f0f\u5b58\u50a8\u5728\u6240\u6709\u5b57\u6bb5\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u89c4\u5219\u521d\u770b\u8d77\u6765\u6bd4\u8f83\u590d\u6742\uff0c\u4f46\u603b\u7684\u8bbe\u8ba1\u76ee\u6807\u5f88\u7b80\u5355\uff0c\u5c31\u662f\uff1a\u5982\u679c\u4f60\u9700\u8981\uff0cSQLite\u4f1a\u5c3d\u91cf\u6a21\u4eff\u5176\u5b83\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4f60\u5c06SQLite\u770b\u6210\u662f\u4e00\u4e2a\u4f20\u7edf\u6570\u636e\u5e93\uff0c\u7c7b\u578b\u4eb2\u548c\u6027\u5c06\u4f1a\u6309\u4f60\u7684\u671f\u671b\u6765\u5b58\u50a8\u503c\u3002\u5982\u679c\u4f60\u58f0\u660e\u4e86\u4e00\u4e2aINTEGER\u5b57\u6bb5\uff0c\u5e76\u5411\u91cc\u9762\u653e\u4e00\u4e2a\u6574\u6570\uff0c\u5c31\u4f1a\u6309\u6574\u6570\u6765\u5b58\u50a8\u3002\u5982\u679c\u4f60\u58f0\u660e\u4e86\u4e00\u4e2a\u5177\u6709 TEXT, CHAR\u6216VARCHAR\u7c7b\u578b\u7684\u5b57\u6bb5\u5e76\u5411\u91cc\u653e\u4e00\u4e2a\u6574\u6570\uff0c\u6574\u6570\u5c06\u4f1a\u8f6c\u5316\u4e3aTEXT\u3002\u53ef\u662f\uff0c\u5982\u679c\u4f60\u4e0d\u9075\u5b88\u8fd9\u4e9b\u89c4\u5b9a\uff0cSQLite\u4e5f\u4f1a\u627e\u5230\u529e\u6cd5\u6765\u5b58\u50a8\u4f60\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u4eb2\u548c\u6027\u7684\u8fd0\u884c<br>\u8ba9\u6211\u4eec\u770b\u4e00\u4e9b\u4f8b\u5b50\u6765\u4e86\u89e3\u4eb2\u548c\u6027\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a<\/p>\n\n\n\n<p>sqlite&gt; CREATE TABLE domain(i int, nnumeric, t text, b blob);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(3.142,3.142,3.142,3.142);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES('3.142','3.142','3.142','3.142');<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(3142,3142,3142,3142);<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(x'3142',x'3142',x'3142',x'3142');<\/p>\n\n\n\n<p>sqlite&gt; INSERT INTO domain VALUES(null,null,null,null);<\/p>\n\n\n\n<p>sqlite&gt; SELECT ROWID,typeof(i),typeof(n),typeof(t),typeof(b)FROM domain;<\/p>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<p>rowid&nbsp; &nbsp; &nbsp; typeof(i)&nbsp; typeof(n) typeof(t)&nbsp; typeof(b)<\/p>\n\n\n\n<p>1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; real&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;real&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;real<\/p>\n\n\n\n<p>2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; real&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;real&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text<\/p>\n\n\n\n<p>3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer&nbsp; &nbsp; integer&nbsp; &nbsp; text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer<\/p>\n\n\n\n<p>4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blob&nbsp; &nbsp; &nbsp; &nbsp; blob&nbsp; &nbsp; &nbsp; &nbsp; blob&nbsp; &nbsp; &nbsp; &nbsp; blob<\/p>\n\n\n\n<p>5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null<\/p>\n\n\n\n<p>\u4e0b\u9762\u7684SQL\u8bf4\u660e\u5b58\u50a8\u7c7b\u7684\u6392\u5e8f\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p>sqlite&gt; SELECT ROWID, b, typeof(b) FROMdomain ORDER BY b;<\/p>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<p>rowid b typeof(b)<\/p>\n\n\n\n<p>5 NULL null<\/p>\n\n\n\n<p>1 3.142 real<\/p>\n\n\n\n<p>3 3142 integer<\/p>\n\n\n\n<p>2 3.142 text<\/p>\n\n\n\n<p>4 1B blob<\/p>\n\n\n\n<p>sqlite&gt; SELECT ROWID, b, typeof(b),b&lt;1000 FROM domain ORDER BY b;<\/p>\n\n\n\n<p>\u8fd4\u56de\uff1a<\/p>\n\n\n\n<p>rowid b typeof(b) b&lt;1000<\/p>\n\n\n\n<p>NULL null NULL<\/p>\n\n\n\n<p>1 3.142 real 1<\/p>\n\n\n\n<p>3 3142 integer 1<\/p>\n\n\n\n<p>2 3.142 text 0<\/p>\n\n\n\n<p>4 1B blob 0<\/p>\n\n\n\n<p>\u5b58\u50a8\u7c7b\u548c\u7c7b\u578b\u8f6c\u6362<br>\u5173\u4e8e\u5b58\u50a8\u7c7b\uff0c\u9700\u8981\u5173\u6ce8\u7684\u53e6\u4e00\u4ef6\u4e8b\u662f\uff1a\u5b58\u50a8\u7c7b\u6709\u65f6\u4f1a\u5f71\u54cd\u5230\u503c\u5982\u4f55\u8fdb\u884c\u6bd4\u8f83\u3002\u7279\u522b\u662fSQLite\u6709\u65f6\u5728\u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u4f1a\u5c06\u503c\u5728\u6570\u5b57\u5b58\u50a8\u7c7b(INTEGER\u548cREAL)\u548cTEXT\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002\u4e3a\u8fdb\u884c\u4e8c\u8fdb\u5236\u7684\u6bd4\u8f83\uff0c\u9075\u5faa\u5982\u4e0b\u89c4\u5219\uff1a<\/p>\n\n\n\n<p>l&nbsp; \u5f53\u4e00\u4e2a\u5b57\u6bb5\u503c\u4e0e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5b57\u6bb5\u7684\u4eb2\u548c\u6027\u4f1a\u5728\u6bd4\u8f83\u4e4b\u524d\u5e94\u7528\u4e8e\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5f53\u4e24\u4e2a\u5b57\u6bb5\u503c\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u62e5\u6709INTEGER\u6216NUMERIC\u4eb2\u548c\u6027\u800c\u53e6\u4e00\u4e2a\u6ca1\u6709\uff0cNUMERIC\u4eb2\u548c\u6027\u4f1a\u5e94\u7528\u4e8e\u975eNUMERIC\u5b57\u6bb5\u7684TEXT\u503c\u3002<\/p>\n\n\n\n<p>l&nbsp; \u5f53\u4e24\u4e2a\u8868\u8fbe\u5f0f\u8fdb\u884c\u6bd4\u8f83\uff0cSQLite\u4e0d\u505a\u4efb\u4f55\u8f6c\u6362\u3002\u5982\u679c\u4e24\u4e2a\u8868\u8fbe\u5f0f\u6709\u76f8\u4f3c\u7684\u5b58\u50a8\u7c7b\uff0c\u5219\u76f4\u63a5\u6309\u5b83\u4eec\u7684\u503c\u8fdb\u884c\u6bd4\u8f83\uff1b\u5426\u5219\u6309\u7c7b\u503c\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<p>\u8bf7\u770b\u4e0b\u9762\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>sqlite&gt; selectROWID,b,typeof(i),i&gt;'2.9' from domain ORDER BY b;<\/p>\n\n\n\n<p>rowid b typeof(i i&gt;'2.9'<\/p>\n\n\n\n<p>5 NULL null NULL<\/p>\n\n\n\n<p>1 3.142 real 1<\/p>\n\n\n\n<p>3 3142 integer 1<\/p>\n\n\n\n<p>2 3.142 real 1<\/p>\n\n\n\n<p>4 1B blob 1<\/p>\n\n\n\n<p>\u4e5f\u7b97\u662f\u201c\u5f3a\u7c7b\u578b(STRICT TYPING)\u201d<br>\u5982\u679c\u4f60\u9700\u8981\u6bd4\u7c7b\u578b\u4eb2\u548c\u6027\u66f4\u5f3a\u7684\u57df\u5b8c\u6574\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528CHECK\u7ea6\u675f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684\u5185\u7f6e\u51fd\u6570\u548c\u4e00\u4e2aCHECK\u7ea6\u675f\u6765\u5b9e\u73b0\u4e00\u4e2a\u201c\u5047\u7684\u201d\u5f3a\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b58\u50a8\u7c7b(Storage Classes)\u5982\u524d\u6587\u6240\u8ff0\uff0cSQLite\u5728\u5904\u7406\u6570\u636e\u7c7b\u578b\u65f6\u4e0e\u5176\u5b83\u7684\u6570\u636e\u5e93\u4e0d\u540c\u3002\u533a\u522b\u5728\u4e8e\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"class_list":["post-275","post","type-post","status-publish","format-standard","hentry","category-xuexi"],"_links":{"self":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=275"}],"version-history":[{"count":0,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/275\/revisions"}],"wp:attachment":[{"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}