{"id":6016,"date":"2017-09-26T22:35:41","date_gmt":"2017-09-26T13:35:41","guid":{"rendered":"http:\/\/excelwork.info\/excel\/?p=6016"},"modified":"2017-09-26T22:35:41","modified_gmt":"2017-09-26T13:35:41","slug":"databasemysql","status":"publish","type":"post","link":"https:\/\/excelwork.info\/excel\/databasemysql\/","title":{"rendered":"\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08MySQL\uff09\u306b\u63a5\u7d9a\u3059\u308b\uff08ADO\uff09"},"content":{"rendered":"<h2>MySQL ODBC\u30c9\u30e9\u30a4\u30d0<\/h2>\n<div class=\"m30-l\">\n<p>Windows\u306b\u306f\u6a19\u6e96\u3067\u3001\u3044\u304f\u3064\u304b\u306e ODBC \u30c9\u30e9\u30a4\u30d0\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u304c\u3001MySQL \u7528\u306e\u30c9\u30e9\u30a4\u30d0\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u306a\u3044\u3088\u3046\u3067\u3059\u3002\u5165\u624b\u3059\u308b\u306b\u306f\u3001<a href=\"https:\/\/dev.mysql.com\/downloads\/connector\/odbc\/\" target=\"_blank\" class=\"broken_link\">MySQL\u306e\u30b5\u30a4\u30c8<\/a>\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08MySQL\uff09\u3078\u306e\u63a5\u7d9a<\/h3>\n<br \/>\n<div class=\"kkbox\"><strong>\u3010ODBC \u63a5\u7d9a\u3011<\/strong><\/p>\n<p>    <span class=\"red\"><strong>\uff1c\u63a5\u7d9a\u6587\u5b57\u5217\uff1e<\/strong><\/span><br \/>\n    <b>Driver<\/b> = <b>{MySQL ODBC 5.3 Unicode Driver}<\/b>;<br \/>\n    <b> Server<\/b> = <em>HostName<\/em>;<br \/>\n    <b> Port<\/b> = <em>PortNumber<\/em>;<br \/>\n    <b> Database<\/b> = <em>DatabaseName<\/em>;<br \/>\n    <b> STMT=SET NAMES <\/b><em>CharacterCode<\/em>;<br \/>\n    <b> UID<\/b> = <em>UserName<\/em>;<br \/>\n    <b> PWD<\/b> = <em>Password<\/em>;<\/p><\/div>\n<br \/>\n<h4>\u5f15\u6570\u30fb\u623b\u308a\u5024<\/h4>\n<div class=\"m30-l\">\n<ul style=\"margin-top:0;margin-bottom:0;padding-top:0;padding-bottom:0;\">\n<li>HostName&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u30b5\u30fc\u30d0\u30fc\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\uff08\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u3042\u308b\u30b5\u30fc\u30d0\u30fc\u306e\u30db\u30b9\u30c8\u540d\u3001IP\u30a2\u30c9\u30ec\u30b9\u3001\u30ed\u30fc\u30ab\u30eb\u306e\u5834\u5408\u306f\u3001localhost\uff09<\/li>\n<li>PortNumber&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u30dd\u30fc\u30c8\u756a\u53f7\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u30013306\uff08\u7701\u7565\u53ef\uff09\u3002<\/li>\n<li>DatabaseName&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u63a5\u7d9a\u3059\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li>CharacterCode&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u3068\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u304c\u7570\u306a\u308b\u5834\u5408\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u5074\u306e\u6587\u5b57\u30b3\u30fc\u30c9\u3092\u6307\u5b9a\u3057\u307e\u3059\uff08Shift-JIS \u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u3001STMT=SET NAMES sjis \u3068\u6307\u5b9a\u3057\u307e\u3059\uff09\u3002<\/li>\n<li>UserName&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u30e6\u30fc\u30b6\u30fc\u540d\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li>Password&nbsp;&nbsp;\u30fb\u30fb\u30fb&nbsp;&nbsp;\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<br \/><\/div>\n<\/div>\n<br \/><br \/>\n<h2>MySQL \u63a5\u7d9a\uff08ODBC\uff09<\/h2>\n<div class=\"m30-l\">\n<h3 id=\"vba_sample\">\u30b5\u30f3\u30d7\u30ebVBA\u30bd\u30fc\u30b9<\/h3>\n<pre class=\"lang:vb mark:0 decode:true \" title=\"MySQL\u63a5\u7d9a\u4f8b\" data-url=\"http:\/\/example.co.jp\" >\r\n    \r\nSub Sample_ADO_MySQL()\r\n    \r\n    Const MYDRIVER = \"Driver={MySQL ODBC 5.3 Unicode Driver};\"      '\u30c9\u30e9\u30a4\u30d0\u30fc\r\n    Const MYSERVER = \"Server=localhost;\"                            '\u30b5\u30fc\u30d0\u30fc\r\n    Const MYPORT = \"Port=3306;\"                                     '\u30dd\u30fc\u30c8\r\n    Const MYDATABASE = \"Database=hpdb02;\"                           '\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\r\n    Const MYCCODE = \"STMT=SET NAMES sjis;\"                          '\u6587\u5b57\u30b3\u30fc\u30c9\uff08Excel\u5074\uff09\r\n    Const MYUSER = \"UID=hpdb02;\"                                    '\u30e6\u30fc\u30b6\u30fc\u540d\r\n    Const MYPASS = \"PWD=hpdb02;\"                                    '\u30d1\u30b9\u30ef\u30fc\u30c9\r\n\r\n    Dim cn As New ADODB.Connection\r\n    Dim rs As New ADODB.Recordset\r\n    Dim constr As String\r\n    Dim strSQL As String\r\n    Dim i As Long\r\n    Dim j As Long\r\n            \r\n    constr = MYDRIVER & MYSERVER & MYPORT & MYDATABASE & MYCCODE & MYUSER & MYPASS\r\n    \r\n    cn.ConnectionString = constr\r\n    cn.Open\r\n    \r\n    '\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\r\n    strSQL = \"Create Table \u4ed5\u5165\u5148\u4e00\u89a7(\" & _\r\n             \"\u4ed5\u5165\u5148ID nchar(5),\" & _\r\n             \"\u4ed5\u5165\u5148\u540d nvarchar(50),\" & _\r\n             \"\u53e3\u5ea7\u756a\u53f7 nchar(8));\"\r\n    rs.Source = strSQL\r\n    rs.ActiveConnection = cn\r\n    rs.Open\r\n    \r\n    '\u30c7\u30fc\u30bf\u8ffd\u52a0\r\n    strSQL = \"insert into \u4ed5\u5165\u5148\u4e00\u89a7 values \" & _\r\n             \"('00001', '\u5927\u9580\u5546\u5e97', '40012345'), \" & _\r\n             \"('00002', '\u4e2d\u9580\u5546\u5e97', '11012311'), \" & _\r\n             \"('00003', '\u5c0f\u9580\u5546\u5e97', '12012221'), \" & _\r\n             \"('00004', '\u65e5\u9032\u96fb\u5668', '13422900'), \" & _\r\n             \"('00005', '\u5c71\u672c\u96fb\u6c17', '11199923'), \" & _\r\n             \"('00006', '\u677e\u5c3e\u5546\u793e', '23247001'), \" & _\r\n             \"('00007', '\u4e38\u5f97\u5546\u5e97', '11223345'), \" & _\r\n             \"('00008', '\u9752\u8449\u5009\u5eab', '12120000'), \" & _\r\n             \"('00009', '\u7dd1\u9280\u884c', '12345678'), \" & _\r\n             \"('00010', '\u8d64\u9ec4\u4fe1\u7528\u91d1\u5eab', '87654321');\"\r\n    rs.Source = strSQL\r\n    rs.Open\r\n    \r\n    strSQL = \"Select * from \u4ed5\u5165\u5148\u4e00\u89a7;\"\r\n    rs.Source = strSQL\r\n    rs.Open\r\n    \r\n    '\u30c7\u30fc\u30bf\u8868\u793a\r\n    With Worksheets(\"mysql_odbc\")\r\n        .Cells.Clear\r\n        i = 1\r\n        Do Until rs.EOF\r\n            For j = 0 To rs.Fields.Count - 1\r\n                If i = 1 Then .Cells(i, j + 1) = rs.Fields(j).Name\r\n                .Cells(i + 1, j + 1) = rs(j).Value\r\n                .Cells(i + 1, j + 1).Errors(xlNumberAsText).Ignore = True\r\n            Next j\r\n            rs.MoveNext\r\n            i = i + 1\r\n        Loop\r\n    End With\r\n    \r\n    rs.Close\r\n    Set rs = Nothing\r\n    cn.Close\r\n    Set cn = Nothing\r\n    \r\nEnd Sub\r\n    \r\n  <\/pre>\n<br \/>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p><a href=\"https:\/\/i2.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/databaseoraclemysql_01b.jpg?ssl=1\" rel=\"lightbox[6016]\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/databaseoraclemysql_01b.jpg?resize=391%2C334&#038;ssl=1\" alt=\"\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\uff08MySQL\uff09\u63a5\u7d9a\" width=\"391\" height=\"334\" class=\"alignnone size-full wp-image-6021\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<\/div>\n<br \/><br \/>\n","protected":false},"excerpt":{"rendered":"<p>MySQL ODBC\u30c9\u30e9\u30a4\u30d0 Windows\u306b\u306f\u6a19\u6e96\u3067\u3001\u3044\u304f\u3064\u304b\u306e ODBC \u30c9 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":6021,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","spay_email":"","jetpack_publicize_message":""},"categories":[948],"tags":[950,954,961,958,956,952,951,814,955,949],"jetpack_featured_media_url":"https:\/\/i2.wp.com\/excelwork.info\/excel\/wp\/wp-content\/uploads\/2017\/07\/databaseoraclemysql_01b.jpg?fit=391%2C334&ssl=1","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4Ye9i-1z2","_links":{"self":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6016"}],"collection":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/comments?post=6016"}],"version-history":[{"count":8,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6016\/revisions"}],"predecessor-version":[{"id":6438,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/posts\/6016\/revisions\/6438"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/media\/6021"}],"wp:attachment":[{"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/media?parent=6016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/categories?post=6016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelwork.info\/excel\/wp-json\/wp\/v2\/tags?post=6016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}