{"id":676,"date":"2009-05-14T17:57:54","date_gmt":"2009-05-14T08:57:54","guid":{"rendered":"http:\/\/blog.bitmeister.jp\/?p=676"},"modified":"2014-07-08T18:47:27","modified_gmt":"2014-07-08T09:47:27","slug":"python%e3%81%a7sqlite%e3%81%ae%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae%e3%82%ab%e3%83%a9%e3%83%a0%e5%90%8d%e5%8f%96%e5%be%97","status":"publish","type":"post","link":"https:\/\/blog.bitmeister.jp\/?p=676","title":{"rendered":"python\u3067sqlite\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u540d\u53d6\u5f97"},"content":{"rendered":"<p>DB\u306b\u3042\u308b\u4efb\u610f\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u30c4\u30fc\u30eb\u306f\u3001DB\u3092\u4f7f\u3063\u305f\u30b7\u30b9\u30c6\u30e0\u958b\u767a\u3092\u7d4c\u9a13\u3055\u308c\u305f\u65b9\u306f\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u3088\u3046\u306a\u30c4\u30fc\u30eb\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u540d\u524d\u3084\u542b\u307e\u308c\u308b\u30ab\u30e9\u30e0\u306e\u540d\u524d\u3068\u3044\u3063\u305f\u901a\u5e38\u306e\u30af\u30a8\u30ea\u30fc\u3067\u306f\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\nPostgreSQL\u3084Oracle\u3067\u306f\u30b7\u30b9\u30c6\u30e0\u30ab\u30bf\u30ed\u30b0\u3068\u3044\u3046\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u306a\u3069\u3092\u683c\u7d0d\u3059\u308bDB\u30b7\u30b9\u30c6\u30e0\u306e\u5185\u90e8\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u3001\u305d\u306e\u3088\u3046\u306a\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>python\u306esqlite3\u3067\u305d\u306e\u3088\u3046\u306a\u52d5\u4f5c\u3092\u3055\u305b\u305f\u304f\u3066\u8abf\u3079\u305f\u7d50\u679c\u3092\u30e1\u30e2\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><br \/>\n\u7d50\u8ad6\u304b\u3089\u3044\u3048\u3070\u30c6\u30fc\u30d6\u30eb\u540d\u3092\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306a\u4ed5\u7d44\u307f\u306f<strong><del datetime=\"2009-05-14T09:14:22+00:00\">\u304a\u305d\u3089\u304f\u306a\u3055\u305d\u3046<\/del>sqlite_master\u3068\u3044\u3046\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308c\u3070\u3067\u304d\u305d\u3046\uff08\u3061\u3083\u3093\u3068\u8abf\u3079\u305f\u3089\u3042\u308a\u307e\u3057\u305f\u3002\u5225\u9014\u8a18\u4e8b\u306b\u3057\u307e\u3059\u2026\uff09<\/strong>\u3067\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u540d\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u306f\u3044\u304f\u3064\u304b\u3042\u308b\u3088\u3046\u3067\u3059\u3002<br \/>\n\uff11\u3064\u306f<a href=\"http:\/\/www.sqlite.org\/pragma.html\">http:\/\/www.sqlite.org\/pragma.html<\/a>\u306b\u3042\u308b\u3088\u3046\u306b\u3001<br \/>\nPRAGMA table_info(table-name);<br \/>\n\u3092\u4f7f\u3063\u3066\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30bf\u30ed\u30b0\u60c5\u5831\u304b\u3089\u53d6\u5f97\u3059\u308b\u3084\u308a\u304b\u305f\u3068\u3001<br \/>\n<a href=\"http:\/\/docs.python.org\/library\/sqlite3.html#sqlite3.Cursor.description\">http:\/\/docs.python.org\/library\/sqlite3.html#sqlite3.Cursor.description<\/a><br \/>\n\u306ecursor.description\u3092\u4f7f\u3046\u3084\u308a\u65b9\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u524d\u8005\u306f\u6b21\u306e\u3088\u3046\u306b\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre style=\"margin: 0 1em; padding: 1ex 2ex; color: white; background-color: #000000; border: solid 1px gray;\">C:\\Users\\oaka&gt;python\r\nPython 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n&gt;&gt;&gt; import sqlite3\r\n&gt;&gt;&gt; con = sqlite3.connect(\"dummy.db\")\r\n&gt;&gt;&gt; con.executescript(\"\"\"create table my_tbl(a integer, b integer);\"\"\")\r\n&lt;sqlite3.Cursor object at 0x01A36860&gt;\r\n&gt;&gt;&gt; cur = con.cursor()\r\n&gt;&gt;&gt; cur.execute(\"PRAGMA table_info(my_tbl)\")\r\n&lt;sqlite3.Cursor object at 0x01A36830&gt;\r\n&gt;&gt;&gt; print cur.fetchall()\r\n[(0, u'a', u'integer', 0, None, 0), (1, u'b', u'integer', 0, None, 0)]\r\n&gt;&gt;&gt;<\/pre>\n<p>\u5f8c\u8005\u306b\u3064\u3044\u3066\u306f\u30c6\u30fc\u30d6\u30eb\u3092\u8868\u793a\uff06\u7de8\u96c6\u3059\u308bCGI\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u306e\u3067\u3054\u53c2\u8003\u307e\u3067\u3002<\/p>\n<p><a href=\"\/wp-content\/uploads\/2009\/05\/sqtest_py.txt\">sqtest.py<\/a><\/p>\n<p>\u629c\u7c8b<\/p>\n<pre class=\"c\">cur = con.cursor()\r\ntry:\r\n \u0080 cur.execute(\"select * from %s\" % tableName)\r\nexcept:\r\n \u0080 cur.close()\r\n \u0080 cur = None\r\n\r\nprint '&lt;th&gt;ColumnName&lt;\/th&gt;'\r\ndesc = None\r\nif cur != None:\r\n \u0080 desc = cur.description\r\nfor columnIndex in range(0,6):\r\n \u0080 columnName = ''\r\n \u0080 if desc != None:\r\n \u0080 \u0080 \u0080 if columnIndex &lt; len(desc):\r\n \u0080 \u0080 \u0080 \u0080 \u0080 columnName = desc[columnIndex][0]\r\n \u0080 print '&lt;th&gt;&lt;input type=\"text\" name=\"columnName%d\" size=\"10\" value=\"%s\"&gt;&lt;\/th&gt;' % (columnIndex,columnName)\r\nprint '&lt;\/tr&gt;'\r\n<\/pre>\n<p>\u5b9f\u884c\u30a4\u30e1\u30fc\u30b8<\/p>\n<div id=\"attachment_678\" style=\"width: 160px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/blog.bitmeister.jp\/wp-content\/uploads\/2009\/05\/sqtest.jpg\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-678\" class=\"size-thumbnail wp-image-678\" title=\"\" src=\"https:\/\/blog.bitmeister.jp\/wp-content\/uploads\/2009\/05\/sqtest-150x150.jpg\" alt=\"sqtest.py\" width=\"150\" height=\"150\" \/><\/a><p id=\"caption-attachment-678\" class=\"wp-caption-text\">sqtest.py<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>DB\u306b\u3042\u308b\u4efb\u610f\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3092\u8868\u793a\u3059\u308b\u30c4\u30fc\u30eb\u306f\u3001DB\u3092\u4f7f\u3063\u305f\u30b7\u30b9\u30c6\u30e0\u958b\u767a\u3092\u7d4c\u9a13\u3055\u308c\u305f\u65b9\u306f\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002 \u305d\u306e\u3088\u3046\u306a\u30c4\u30fc\u30eb\u3092\u5b9f\u88c5\u3059\u308b\u306b\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u540d\u524d\u3084\u542b\u307e\u308c\u308b\u30ab\u30e9\u30e0\u306e\u540d\u524d\u3068\u3044\u3063\u305f\u901a\u5e38\u306e\u30af\u30a8\u30ea\u30fc\u3067\u306f\u53d6\u5f97\u3059 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[40],"class_list":["post-676","post","type-post","status-publish","format-standard","hentry","category-tech","tag-40"],"_links":{"self":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=676"}],"version-history":[{"count":18,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":3330,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/676\/revisions\/3330"}],"wp:attachment":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}