0

有没有办法做到这一点?我在excel中有6000行,将它导入sql数据库需要几个月的时间。

例如有: excel中的A列和B列,我想将它导入A列和B列到一个sql表中。

谢谢!

4

2 回答 2

0

这是我用于书籍数据库的 VBA 方法。只要您可以建立与数据库的连接,就不会太难。显然,您必须稍微调整一下才能使其与您的数据库和数据一起使用。特别是您必须调整 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
于 2012-05-02T20:07:41.807 回答
0

如果您正在使用MySQL,它提供了一种将数据导出到数据库的直接方式:https ://dev.mysql.com/doc/mysql-for-excel/en/mysql-for-excel-export.html

如果您使用SQLite/ PostGres,Devart 提供了一个商业产品来做同样的事情:

https://www.devart.com/excel-addins/postgresql.html

https://www.devart.com/excel-addins/sqlite.html

于 2018-08-19T02:30:19.667 回答