Python - 使用Excel中的数据填充表MSSQL

时间:2017-12-07 08:49:22

标签: python sql-server excel

我有一个包含4列和68k +行的xlsx文件。我想将所有数据放在MSSQL服务器的表中。我做到了。

def jb_treat_attributes(dicoval):
conn = pymssql.connect(dicoval['MSSQL_erver'],dicoval['MSSQL_Login'],dicoval['MSSQL_Password'],dicoval['MSSQL_Database_DELTA'])
    cursor = conn.cursor()
    cursor.execute('TRUNCATE TABLE delta.Attribute')
    for row in load_workbook(dicoval['path_root']+'Delta/attributes/excel.xlsx').worksheets[0].iter_rows():
        row = [cell.value.strip() if cell.value is not None else '' for cell in row]
        cursor.execute("INSERT INTO delta.Attribute VALUES (%s,%s,%s,%s)",(row[0],row[1],row[2],row[3]))
    conn.commit()
    conn.close()

它正在运行,但执行时间为340秒。它是否有办法更快地完成它?

3 个答案:

答案 0 :(得分:1)

  

第一个快速思考:使用Begin Transaction告诉您正在添加插入

在开始cursor.execute

之前放置此行
cursor.begin()
  

包装插页

没有直接证据,但打包值有时会帮助上传批量插入。 (这基本上是cursor.begin()的用途。

我无法对其进行测试,但这就是我的想法。您在list tuples中收集所有值并将其转换为字符串,最后执行该字符串即可提供所有值

def jb_treat_attributes(dicoval):
    values = []
    conn = pymssql.connect(dicoval['MSSQL_erver'],dicoval['MSSQL_Login'],dicoval['MSSQL_Password'],dicoval['MSSQL_Database_DELTA'])
    cursor = conn.cursor()
    cursor.execute('TRUNCATE TABLE delta.Attribute')
    for row in load_workbook(dicoval['path_root']+'Delta/attributes/excel.xlsx').worksheets[0].iter_rows():
        row = [cell.value.strip() if cell.value is not None else '' for cell in row]
        values.append((row[0],row[1],row[2],row[3]))
    valueTuples = str(values)[1:-1]
    cursor.execute("INSERT INTO delta.Attribute VALUES " + valueTuples)
    conn.commit()
    conn.close()

答案 1 :(得分:0)

如何调用python直接执行.sql文件? 你有.sql文件导入excel到数据库,如https://docs.microsoft.com/en-us/sql/relational-databases/import-export/import-data-from-excel-to-sql

答案 2 :(得分:0)

很抱歉,但这与Python有什么关系?!您应该尝试使用正确的工具来完成工作。如果你需要Python,R或C#,请使用它们;如果你不需要它们为什么要使用它们? SQL Server和Excel非常非常好地协同工作。您可以轻松使用SQL Server导入向导来移入或移出数据。

您可以使用SQL来完成这项工作。

SELECT * INTO EXCEL_IMPORT
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=C:\Excel\Spreadsheet.xls; HDR=YES; IMEX=1',
'SELECT * FROM [Sheet1$]');

You can just as well use VBA to get things done.

    Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=True;"

'Create a new Command object
Set cmd = New adodb.Command

'Open the connection
cnn.Open
'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = 2013-01-13 WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL

'Open the Connection to the database
cnn.Open

'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Excel%20Data%20Export%20to%20SQL%20Server%20Test%20Code

很抱歉没有回答你的Python问题,但我非常支持使用正确的工具来完成工作。 Python对于无数的东西来说很棒,但不是这种东西。