如何用vba连接mysql数据库?

x33g5p2x  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(437)

选项显式
sub excelmysql()'连接变量dim conn as new adodb.connection dim server \u name as string dim database \u name as string dim user \u id as string dim password as string'表操作变量dim i as long'计数器dim strsql as string'执行各种操作的sql dim table1 as string dim field1 as string,字段2作为字符串dim rs as adodb.recordset dim vtype as variant
server_name=“localhost”'在此处输入服务器名称-如果从本地计算机运行,请使用127.0.0.1
database_name=“prmjira”'在此处输入您的数据库名称
user\u id=“root”'在此处输入您的用户id
密码=“pradee@1234“'在此处输入密码

Set conn = New ADODB.Connection

conn.Open "DRIVER={MySQL ODBC 8.0 Unicode Driver}" _
          & ";SERVER=" & Server_Name _
          & ";DATABASE=" & Database_Name _
          & ";UID=" & User_ID _
          & ";PWD=" & Password _
          & ";OPTION=16427"

'conn.Open

vtype = Array("varchar(255)", "Text", "LongText", "Int(10)", "Float", "Double", "Date", "Time")    ' array of commonly used MySQL variable types
table1 = "P" & Range("H1").Value

field1 = "field1text"

field2 = "field2text"

strSQL = "CREATE TABLE `" & table1 & "` (`" _
         & field1 & "` " & vtype(0) & ",`" _
         & field2 & "` " & vtype(4) _
         & ")"

conn.Execute strSQL

On Error Resume Next

rs.Close

Set rs = Nothing

conn.Close

Set conn = Nothing

On Error GoTo 0

末端接头

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题