簡體   English   中英

Python-用來自Excel的數據填充表格MSSQL

[英]Python - Fill a table MSSQL with data from 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秒。 是否存在更快地執行此操作的方法?

初步的想法 :使用Begin Transaction告訴您要添加插入內容

在開始cursor.execute之前放置此行

cursor.begin()

包裝插頁

沒有直接的證據,但是打包值有時可以幫助批量上傳插入內容。 (基本上,這是cursor.begin()的作用。

我無法測試,但這就是想法。 您將所有值都收集在一個tuples list中,並將它們轉換為字符串,最后執行該字符串以提供所有值

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()

如何調用python直接執行.sql文件? 您有一個.sql文件來將Excel導入數據庫,例如此處https://docs.microsoft.com/zh-cn/sql/relational-databases/import-export/import-data-from-excel-to-sql

抱歉,這與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在無數事物上都很棒,但在這類事情上卻不是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM