1

我看到过去也有类似的问题,但不幸的是我在那里找不到解决方案。我对 Python 比较陌生。

我有一个熊猫数据框,我想使用 SQLalchemy dataframe.to_sql() 将其写入 SQL Server

数据框具有完全相同的列数,列名与 sql 表中的相同。我无法指出此错误背后的原因。另外,我注意到默认情况下它也导出数据框的索引。那么,我们是否还需要在 sql 表中添加一个对应于索引的列。我也试过了,但它仍然给出同样的错误。

这是我正在运行的代码:

import sqlalchemy
import pyodbc

engine = create_engine('mssql+pyodbc://workspace?driver=SQL+Server+Native+Client+11.0', echo=True)
campaign_table.to_sql("workspace.dbo.campaign_results", con= engine,if_exists = 'append')

我得到的错误:

ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Error converting data type varchar to bigint. (8114) (SQLExecDirectW)') [SQL: u'INSERT INTO [workspace.dbo.campaign_results] ([index], [Emailing_id], delivered, clickcount, [impressionCount], [errorCount], abusecount, unsubscribecount, [unknownCount], ctr, cto, destinationcount, firsttimestamp, lasttimestamp, unique_clicks, unique_impressions, [Open_Rate], campaign_id, template_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'] [parameters: ('Emailing_id', '27619', '10606', '241', '3658', '243', '0', '0', '7797', '2', '8', '10845', u'2017-02-03 06:30:15', u'2017-02-03 06:30:15', '216', '2589', '23', '1', '1')]

问题

  1. 这是数据类型转换的错误还是 Python 和 SQLServer 之间的一些连接问题?
  2. 我怎么知道哪个变量是这个麻烦的根源?反复试验是一种方法,但如果数据框中的变量太多怎么办?

请指导或指导我到相关线程,因为我仍在迷宫中寻找出路。

干杯!

更新

更改了一些变量的格式后,Python 似乎运行良好,但 SQL 表仍未更新。Stackoverflow 上有一个完全相同的问题,但那里说明的解决方案(手动向 SQL 表添加一行)也没有帮助。这是新的输出:

campaign_table_f.to_sql("workspace.dbo.campaign_results", con= engine,if_exists = 'append')
2017-02-15 09:46:51,773 INFO sqlalchemy.engine.base.Engine SELECT [COLUMNS_1].[TABLE_SCHEMA], [COLUMNS_1].[TABLE_NAME], [COLUMNS_1].[COLUMN_NAME], [COLUMNS_1].[IS_NULLABLE], [COLUMNS_1].[DATA_TYPE], [COLUMNS_1].[ORDINAL_POSITION], [COLUMNS_1].[CHARACTER_MAXIMUM_LENGTH], [COLUMNS_1].[NUMERIC_PRECISION], [COLUMNS_1].[NUMERIC_SCALE], [COLUMNS_1].[COLUMN_DEFAULT], [COLUMNS_1].[COLLATION_NAME] 
FROM [INFORMATION_SCHEMA].[COLUMNS] AS [COLUMNS_1] 
WHERE [COLUMNS_1].[TABLE_NAME] = CAST(? AS NVARCHAR(max)) AND [COLUMNS_1].[TABLE_SCHEMA] = CAST(? AS NVARCHAR(max))
2017-02-15 09:46:51,776 INFO sqlalchemy.engine.base.Engine (u'workspace.dbo.campaign_results', u'ashish.dang')
2017-02-15 09:46:51,877 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2017-02-15 09:46:51,881 INFO sqlalchemy.engine.base.Engine INSERT INTO [workspace.dbo.campaign_results] ([index], [Emailing_id], delivered, clickcount, [impressionCount], [errorCount], abusecount, unsubscribecount, [unknownCount], ctr, cto, destinationcount, firsttimestamp, lasttimestamp, unique_clicks, unique_impressions, [Open_Rate], campaign_id, template_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2017-02-15 09:46:51,882 INFO sqlalchemy.engine.base.Engine ((0L, '27610', '381', '41', '473', '5', '0', '0', '201', '9', '19', '386', u'2017-02-03 06:15:17', u'2017-02-03 06:15:17', '34', '146', '37', 1L, 0L), (1L, '27612', '153', '12', '251', '2', '0', '0', '58', '6', '9', '155', u'2017-02-03 06:15:18', u'2017-02-03 06:15:18', '9', '86', '55', 1L, 1L))
2017-02-15 09:46:51,977 INFO sqlalchemy.engine.base.Engine COMMIT
4

0 回答 0