[英]Python & MSSQL: Python Code to Delete All Rows of Data in MSSQL Table
[英]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
抱歉,没有回答您的Python问题,但是我强烈支持使用正确的工具来完成这项工作。 Python在无数事物上都很棒,但在这类事情上却不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.