有没有办法做到这一点?我喜欢excel中的6000行,将它导入sql数据库需要几个月的时间。
例如,excel中有 A列和 B列,我想将其导入 A列和 B列进入sql表。
谢谢!
答案 0 :(得分:0)
这是我用于图书数据库的VBA方法。只要您可以建立与DB的连接,就不会太难。显然,你必须稍微调整一下才能使用你的数据库和数据。特别是你必须调整sql字符串以连接你的数据库(更改“test.yourdatabase”)
Sub addBook(title As String, author As String, link As String, foundBy As String)
' for this code to work, you need a reference to the MS Activex
' data access objects
' Tools|References... microsoft activex data objects 2.8 (or newer)
Dim cn As New Connection
Dim cs As String
Dim un as String
Dim pw as String
Dim sql As String
cs = "DRIVER=SQL Server;SERVER=websql.org,5901;DATABASE=websql" 'connection string
un = username 'this is the username for the database
pw = password 'this is the password for the database account
cn.Open cs, Range("un").Value, Range("pw").Value
sql = "insert into test.yourdatabase(searchtitle, searchAuthor, link, foundBy, foundTime) values(" & _
"'<TITLE>', " & _
"'<AUTHOR>', " & _
"'<LINK>', " & _
"'<FOUNDBY>', " & _
"getdate());"
'replace statements are for correcting any ' that occure in the titles or names
sql = Replace(sql, "<TITLE>", Replace(title, "'", "''"))
sql = Replace(sql, "<AUTHOR>", Replace(author, "'", "''"))
sql = Replace(sql, "<LINK>", Replace(link, "'", "''"))
sql = Replace(sql, "<FOUNDBY>", Replace(foundBy, "'", "''"))
'Debug.Print sql
cn.Execute sql
cn.Close
End Sub
答案 1 :(得分:0)
如果您使用的是MySQL
,则可以直接将数据导出到数据库中:https://dev.mysql.com/doc/mysql-for-excel/en/mysql-for-excel-export.html
如果您使用的是SQLite
/ PostGres
,Devart会提供商业产品来执行相同的操作: