{"id":3001,"date":"2020-01-14T14:23:41","date_gmt":"2020-01-14T05:23:41","guid":{"rendered":"https:\/\/okamurax.com\/?p=3001"},"modified":"2020-05-10T17:39:11","modified_gmt":"2020-05-10T08:39:11","slug":"vba-%e3%82%a8%e3%82%af%e3%82%bb%e3%83%ab%e3%81%8b%e3%82%89sql%e5%ae%9f%e8%a1%8csqlite","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=3001","title":{"rendered":"VBA SQL\u5b9f\u9a13\u7528\u2463(SQLite)"},"content":{"rendered":"<p>\u6700\u8fd1\u3067\u306f\u5c02\u9580\u5bb6\u3067\u306f\u306a\u304f\u3066\u3082\u30c7\u30fc\u30bf\u5206\u6790\u304c\u3067\u304d\u305f\u307b\u3046\u304c\u3044\u3044\u3068\u3044\u3046\u8a71\u3092\u3088\u304f\u805e\u304d\u307e\u3059\u3002\u305d\u3046\u3044\u3046\u8a71\u306e\u4e2d\u3067\u5229\u7528\u3055\u308c\u3066\u3044\u308b\u30bd\u30d5\u30c8\u306f\u3060\u3044\u305f\u3044\u5c02\u7528\u30bd\u30d5\u30c8\u304b\u30a8\u30af\u30bb\u30eb\u3068\u3044\u3063\u305f\u611f\u3058\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u305f\u3060\u6570\u306f\u5c11\u306a\u3044\u3067\u3059\u304cSQL\u3092\u4f7f\u304a\u3046\u3068\u3044\u3046\u306e\u3082\u3042\u308b\u3088\u3046\u3067\u3059\u3002\u5b9f\u969bSQL\u306f\u975e\u5e38\u306b\u4fbf\u5229\u3067\u30a8\u30af\u30bb\u30eb\u3088\u308a\u591a\u5c11\u6577\u5c45\u306f\u9ad8\u3044\u3067\u3059\u304c\u3001\u5c11\u3057\u4f7f\u3048\u308c\u3070\u30a8\u30af\u30bb\u30eb\u3067\u306f\u9762\u5012\u306a\u51e6\u7406\u3082\u7c21\u5358\u306b\u3067\u304d\u305f\u308a\u3057\u307e\u3059\u3002\u7d4c\u9a13\u4e0a\u3001\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u95a2\u6570\u3001VBA\u3001SQL\u3092\u4f7f\u3044\u5206\u3051\u308c\u3070\u307b\u3068\u3093\u3069\u306e\u51e6\u7406\u304c\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u3059\u304c\u3001\u3044\u3056SQL\u3092\u4f7f\u304a\u3046\u3068\u3059\u308b\u3068\u3069\u306e\u30bd\u30d5\u30c8\u3092\u4f7f\u3063\u305f\u3089\u3044\u3044\u306e\u304b\u8ff7\u3046\u3068\u601d\u3044\u307e\u3059\u3002\u4e00\u822c\u7684\u306b\u306fAccess\u3001SQL Server\u3001MySQL(MariaDB)\u3001SQLite\u3042\u305f\u308a\u304c\u304a\u52e7\u3081\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3082\u3057PC\u306bAccess\u304c\u5165\u3063\u3066\u3044\u308b\u306a\u3089Access\u304c\u4e00\u756a\u624b\u8efd\u306b\u59cb\u3081\u3089\u308c\u308b\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002\u4eca\u56de\u306fSQLite\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u79c1\u3082\u6700\u521d\u306fAccess\u3092\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u4e2d\u5c0f\u4f01\u696d\u3067\u50cd\u3044\u3066\u3044\u308b\u3068\u30c7\u30fc\u30bf\u5206\u6790\u3084IT\u306e\u5c02\u9580\u5bb6\u304c\u3044\u306a\u3044\u5834\u5408\u3082\u591a\u3044\u306e\u3067\u3001\u81ea\u5206\u305f\u3061\u3067\u5c11\u3057\u3067\u3082\u30c7\u30fc\u30bf\u5206\u6790\u304c\u3067\u304d\u308b\u3068\u5f79\u306b\u7acb\u3064\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:vb decode:true\">Sub auto_open()\r\n\r\nApplication.CommandBars(\"cell\").Reset\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"CreateTable\"\r\n.Caption = \"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\"\r\nEnd With\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"QuerySelect\"\r\n.Caption = \"SQL\u5b9f\u884c\"\r\nEnd With\r\n\r\nEnd Sub\r\n\r\nSub auto_close()\r\n\r\nOn Error Resume Next\r\n\r\nFor i = 1 To 2\r\n  Application.CommandBars(\"cell\").Controls(\"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\").Delete\r\n  Application.CommandBars(\"cell\").Controls(\"SQL\u5b9f\u884c\").Delete\r\nNext i\r\n\r\nEnd Sub\r\n\r\nFunction GetCon()\r\n\r\nGetCon = \"DRIVER=SQLite3 ODBC Driver; DataBase=\" &amp; ThisWorkbook.Path &amp; \"\\dat\"\r\n\r\nEnd Function\r\n\r\nFunction GetType(s)\r\n\r\n'\u578b\u306fSQLite\u4ee5\u5916\u306e\u4e92\u63db\u6027\u306e\u305f\u3081\r\n\r\nSelect Case s\r\n  Case \"s\"\r\n  tmp = \"varchar(255)\"\r\n  Case \"i\"\r\n  tmp = \"bigint\"\r\n  Case \"m\"\r\n  tmp = \"money\"\r\n  Case \"d\"\r\n  tmp = \"datetime\"\r\nEnd Select\r\n\r\nGetType = tmp\r\n\r\nEnd Function\r\n\r\nSub CreateTable()\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\ncn.Open GetCon\r\n\r\ntbl = InputBox(\"\u30c6\u30fc\u30d6\u30eb\u540d\")\r\nIf tbl = \"\" Then Exit Sub\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u524a\u9664\r\n\r\nOn Error Resume Next\r\ncn.Execute \"drop table \" &amp; tbl\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\r\n\r\nOn Error GoTo 0\r\n\r\nfld = \"\"\r\n\r\nFor c = Selection(1).Column To Selection(Selection.Count).Column\r\n\r\n  tmp = GetType(Right(ActiveSheet.Cells(Selection(1).Row, c).Value, 1))\r\n  fld = fld &amp; ActiveSheet.Cells(Selection(1).Row, c).Value &amp; \" \" &amp; tmp &amp; \",\"\r\n\r\nNext c\r\n\r\nfld = Left(fld, Len(fld) - 1)\r\n\r\nq = \"create table \" &amp; tbl &amp; \" (id integer primary key,\" &amp; fld &amp; \")\"\r\ncn.Execute q: Debug.Print q\r\n\r\n'\u30c7\u30fc\u30bf\u767b\u9332\r\n\r\nfld = \"\"\r\n\r\nFor c = Selection(1).Column To Selection(Selection.Count).Column\r\n  fld = fld &amp; ActiveSheet.Cells(Selection(1).Row, c).Value &amp; \",\"\r\nNext c\r\n\r\nfld = Left(fld, Len(fld) - 1)\r\n\r\nFor r = (Selection(1).Row) + 1 To Selection(Selection.Count).Row\r\n  For cc = Selection(1).Column To Selection(Selection.Count).Column\r\n    rec = rec &amp; \"'\" &amp; ActiveSheet.Cells(r, cc).Value &amp; \"'\" &amp; \",\"\r\n  Next cc\r\n\r\n  rec = Left(rec, Len(rec) - 1)\r\n  \r\n  q = \"insert into \" &amp; tbl &amp; \" (\" &amp; fld &amp; \") values (\" &amp; rec &amp; \")\"\r\n  cn.Execute q: Debug.Print q\r\n\r\n  rec = \"\"\r\nNext r\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nMsgBox \"done\"\r\n\r\nEnd Sub\r\n\r\nSub ExecuteQuery(q)\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\nSet rn = CreateObject(\"adodb.recordset\")\r\n\r\ncn.Open GetCon\r\nrn.Open q, cn\r\n\r\n'\u30d5\u30a3\u30fc\u30eb\u30c9\u4f5c\u6210\r\n\r\ni = 0\r\n\r\nFor c = Selection(1).Column To (Selection(1).Column + rn.Fields.Count) - 1\r\n  Cells(Selection(Selection.Count).Row + 1, c).Value = rn.Fields(i).Name\r\n  i = i + 1\r\nNext c\r\n\r\n'\u30c7\u30fc\u30bf\u8aad\u8fbc\r\n\r\nCells(Selection(Selection.Count).Row + 2, Selection(1).Column).CopyFromRecordset rn\r\n\r\nIf rn.State = 1 Then rn.Close\r\nSet rn = Nothing\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nEnd Sub\r\n\r\nSub ExecuteNonQuery(q)\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\n\r\ncn.Open GetCon\r\ncn.Execute q\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nEnd Sub\r\n\r\nSub QuerySelect()\r\n\r\nIf Selection.Rows.Count = 1 Then\r\n  q = ActiveCell.Value\r\nElse\r\n  For r = 1 To Selection.Rows.Count\r\n    q = q &amp; \" \" &amp; Selection(r)\r\n  Next r\r\nEnd If\r\n\r\nIf Left(q, 1) = \"s\" Then\r\n  ExecuteQuery (q)\r\nElse\r\n  ExecuteNonQuery (q)\r\nEnd If\r\n\r\nEnd Sub\r\n<\/pre>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/YALDP3cs2Zw\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>\u8ffd\u8a18<\/p>\n<p>\u3061\u3087\u3063\u3068\u539f\u56e0\u306f\u5206\u304b\u3089\u306a\u3044\u3051\u3069\u3001SQLite\u6a19\u6e96\u306e\u30ab\u30e9\u30e0\u306e\u30c7\u30fc\u30bf\u578b(text\u3084integer)\u3067\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u3068CopyFromRecordset\u3067\u53d6\u5f97\u3067\u304d\u306a\u304f\u306a\u308b\u3002<\/p>\n<p>varchar(255)\u3092\u30ab\u30e9\u30e0\u306e\u30c7\u30fc\u30bf\u578b\u306b\u6307\u5b9a\u3057\u3066\u3044\u308b\u3068CopyFromRecordset\u3067\u554f\u984c\u306a\u304f\u53d6\u5f97\u3067\u304d\u308b\u3002<\/p>\n<p>CopyFromRecordset\u306f\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u6642\u306e\u578b\u306e\u5f71\u97ff\u3092\u53d7\u3051\u3066\u3044\u308b\u3089\u3057\u3044\u3002\u3068\u308a\u3042\u3048\u305aCopyFromRecordset\u3092\u4f7f\u308f\u306a\u3044\u30d1\u30bf\u30fc\u30f3<\/p>\n<pre class=\"lang:vb decode:true \">Sub auto_open()\r\n\r\nApplication.CommandBars(\"cell\").Reset\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"CreateTable\"\r\n.Caption = \"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\"\r\nEnd With\r\n\r\nWith Application.CommandBars(\"cell\").Controls.Add\r\n.OnAction = \"QuerySelect\"\r\n.Caption = \"SQL\u5b9f\u884c\"\r\nEnd With\r\n\r\nEnd Sub\r\n\r\nSub auto_close()\r\n\r\nOn Error Resume Next\r\n\r\nFor i = 1 To 2\r\n  Application.CommandBars(\"cell\").Controls(\"\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\").Delete\r\n  Application.CommandBars(\"cell\").Controls(\"SQL\u5b9f\u884c\").Delete\r\nNext i\r\n\r\nEnd Sub\r\n\r\nFunction GetCon()\r\n\r\n'GetCon = \"DRIVER=SQLite3 ODBC Driver; DataBase=\" &amp; ThisWorkbook.Path &amp; \"\\dat\"\r\nGetCon = \"DRIVER=SQLite3 ODBC Driver; DataBase=\" &amp; Sheets(\"\u8a2d\u5b9a\").Cells(1, 2).Value\r\n\r\nEnd Function\r\n\r\nFunction GetType(s)\r\n\r\nSelect Case s\r\n  Case \"s\"\r\n  tmp = \"text\"\r\n  Case \"i\"\r\n  tmp = \"integer\"\r\nEnd Select\r\n\r\nGetType = tmp\r\n\r\nEnd Function\r\n\r\nSub CreateTable()\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\ncn.Open GetCon\r\n\r\ntbl = InputBox(\"\u30c6\u30fc\u30d6\u30eb\u540d\")\r\nIf tbl = \"\" Then Exit Sub\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u524a\u9664\r\n\r\nOn Error Resume Next\r\ncn.Execute \"drop table \" &amp; tbl\r\n\r\n'\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\r\n\r\nOn Error GoTo 0\r\n\r\nSet topLeft = Selection(1)\r\nSet bottomRight = Selection(Selection.Count)\r\n\r\nhead = \"\"\r\nFor c = topLeft.Column To bottomRight.Column\r\n  tmp = GetType(Right(ActiveSheet.Cells(topLeft.Row, c).Value, 1))\r\n  head = head &amp; ActiveSheet.Cells(topLeft.Row, c).Value &amp; \" \" &amp; tmp &amp; \",\"\r\nNext c\r\n\r\nhead = Left(head, Len(head) - 1)\r\n\r\nq = \"create table \" &amp; tbl &amp; \" (id integer primary key,\" &amp; head &amp; \")\"\r\ncn.Execute q\r\n\r\n'\u30c7\u30fc\u30bf\u767b\u9332\r\n\r\nhead = \"\"\r\nFor c = topLeft.Column To bottomRight.Column\r\n  head = head &amp; ActiveSheet.Cells(topLeft.Row, c).Value &amp; \",\"\r\nNext c\r\n\r\nhead = Left(head, Len(head) - 1)\r\nFor r = topLeft.Row + 1 To bottomRight.Row\r\n\r\n  For c = topLeft.Column To bottomRight.Column\r\n    body = body &amp; \"'\" &amp; ActiveSheet.Cells(r, c).Value &amp; \"'\" &amp; \",\"\r\n  Next c\r\n\r\n  body = Left(body, Len(body) - 1)\r\n  \r\n  q = \"insert into \" &amp; tbl &amp; \" (\" &amp; head &amp; \") values (\" &amp; body &amp; \")\"\r\n  cn.Execute q\r\n\r\n  body = \"\"\r\n  \r\nNext r\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nMsgBox \"done\"\r\n\r\nEnd Sub\r\n\r\nSub ExecuteQuery(q)\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\nSet rn = CreateObject(\"adodb.recordset\")\r\n\r\ncn.Open GetCon\r\nrn.Open q, cn\r\n\r\nSet topLeft = Selection(1)\r\nSet bottomRight = Selection(Selection.Count)\r\n\r\n'\u898b\u51fa\r\n\r\ni = 0\r\nFor c = topLeft.Column To (topLeft.Column + rn.Fields.Count) - 1\r\n  ActiveSheet.Cells(bottomRight.Row + 1, c).Value = rn.Fields(i).Name\r\n  i = i + 1\r\nNext c\r\n\r\n'\u672c\u4f53\r\n\r\nr = 2\r\nDo Until rn.EOF\r\n\r\n  i = 0\r\n  For c = topLeft.Column To (topLeft.Column + rn.Fields.Count) - 1\r\n    ActiveSheet.Cells(bottomRight.Row + r, c).Value = rn.Fields(i)\r\n    i = i + 1\r\n  Next c\r\n  \r\n  rn.MoveNext\r\n  r = r + 1\r\nLoop\r\n\r\nIf rn.State = 1 Then rn.Close\r\nSet rn = Nothing\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nEnd Sub\r\n\r\nSub ExecuteNonQuery(q)\r\n\r\nSet cn = CreateObject(\"adodb.connection\")\r\n\r\ncn.Open GetCon\r\ncn.Execute q\r\n\r\nIf cn.State = 1 Then cn.Close\r\nSet cn = Nothing\r\n\r\nEnd Sub\r\n\r\nSub QuerySelect()\r\n\r\nIf Selection.Rows.Count = 1 Then\r\n  q = ActiveCell.Value\r\nElse\r\n  For r = 1 To Selection.Rows.Count\r\n    q = q &amp; \" \" &amp; Selection(r)\r\n  Next r\r\nEnd If\r\n\r\nIf Left(q, 1) = \"s\" Then\r\n  ExecuteQuery (q)\r\nElse\r\n  ExecuteNonQuery (q)\r\nEnd If\r\n\r\nEnd Sub\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u3067\u306f\u5c02\u9580\u5bb6\u3067\u306f\u306a\u304f\u3066\u3082\u30c7\u30fc\u30bf\u5206\u6790\u304c\u3067\u304d\u305f\u307b\u3046\u304c\u3044\u3044\u3068\u3044\u3046\u8a71\u3092\u3088\u304f\u805e\u304d\u307e\u3059\u3002\u305d\u3046\u3044\u3046\u8a71\u306e\u4e2d\u3067\u5229\u7528\u3055\u308c\u3066\u3044\u308b\u30bd\u30d5\u30c8\u306f\u3060\u3044\u305f\u3044\u5c02\u7528\u30bd\u30d5\u30c8\u304b\u30a8\u30af\u30bb\u30eb\u3068\u3044\u3063\u305f\u611f\u3058\u3067\u3057\u3087\u3046\u304b\u3002 \u305f\u3060\u6570\u306f\u5c11\u306a\u3044\u3067\u3059\u304cSQL\u3092\u4f7f\u304a\u3046\u3068\u3044\u3046\u306e\u3082\u3042\u308b\u3088 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=3001\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;VBA SQL\u5b9f\u9a13\u7528\u2463(SQLite)&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5],"class_list":["post-3001","post","type-post","status-publish","format-standard","hentry","category-1","tag-vba"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3001"}],"version-history":[{"count":13,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3001\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/3001\/revisions\/3470"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}