【问题标题】:sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S22', "[42S22] [Microsoft].....SQL Server]Invalid column name '0'sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S22', "[42S22] [Microsoft].....SQL Server]无效的列名'0'
【发布时间】:2020-04-24 03:32:59
【问题描述】:

我正在尝试使用 to_sql() 函数更新现有的 SQL 数据库.....

当我尝试运行代码时,我发现了这个错误

Traceback (most recent call last):
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1225, in _execute_context
    self.dialect.do_executemany(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 399, in do_executemany
    super(MSDialect_pyodbc, self).do_executemany(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\default.py", line 578, in do_executemany
    cursor.executemany(statement, parameters)
pyodbc.ProgrammingError: ('42S22', "[42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '0'. (207) (SQLExecDirectW); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '1'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '2'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '3'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '4'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '5'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:/Tooling/20200206/MyDB_Update.py", line 22, in <module>
    new_df.to_sql('MNM_Rotterdam_5_Daily_Details-20191216081027', con=mydb, if_exists='append', index=True)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 2530, in to_sql
    sql.to_sql(self, name, con, schema=schema, if_exists=if_exists,
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\sql.py", line 458, in to_sql
    pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index,
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\sql.py", line 1174, in to_sql
    table.insert(chunksize, method=method)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\sql.py", line 686, in insert
    exec_insert(conn, keys, chunk_iter)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\sql.py", line 599, in _execute_insert
    conn.execute(self.table.insert(), data)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
    return meth(self, multiparams, params)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1095, in _execute_clauseelement
    ret = self._execute_context(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context
    self._handle_dbapi_exception(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1225, in _execute_context
    self.dialect.do_executemany(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 399, in do_executemany
    super(MSDialect_pyodbc, self).do_executemany(
  File "C:\Users\mwx825326\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\default.py", line 578, in do_executemany
    cursor.executemany(statement, parameters)
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S22', "[42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '0'. (207) (SQLExecDirectW); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '1'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '2'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '3'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '4'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name '5'. (207); [42S22] [Microsoft][SQL Server Native Client 11.0][SQL Server]Statement(s) could not be prepared. (8180)")
[SQL: INSERT INTO [MNM_Rotterdam_5_Daily_Details-20191216081027] ([index], [0], [1], [2], [3], [4], [5]) VALUES (?, ?, ?, ?, ?, ?, ?)]
[parameters: ((0, 'RTWAGB2', 'bts038831', 50660, 3180, 50660, 898), (1, 'RTWAGB2', 'bts005811', 11063, 3180, 11063, 1091), (2, 'RTWAGB2', 'bts089431', 53462, 3170, 53462, 1111), (3, 'RTWAGB2', 'bts088191', 47274, 3200, 47274, 1108), (4, 'RTWAGB2', 'bts026851', 49386, 3200, 49386, 1145), (5, 'RTWAGB2', 'bts026911', 52128, 3180, 52128, 1054), (6, 'RTWAGB2', 'bts088191', 45101, 3200, 45101, 1106), (7, 'RTWAGB2', 'bts005541', 10970, 3180, 10970, 1090)  ... displaying 10 of 1200 total bound parameter sets ...  (1198, 'RTWAGB2', 'bts005331', 10902, 3170, 10902, 1187), (1199, 'RTWAGB2', 'bts005331', 10902, 3170, 10902, 1187))]
(Background on this error at: http://sqlalche.me/e/f405)

这是我的代码:

from sqlalchemy import create_engine
import pandas as pd
from MyDB import Add_To_DB
mydb = create_engine(
    'mssql+pyodbc://WWX542337CDCD\SMARTRNO_EXPRESS/myDB?driver=SQL+Server+Native+Client+11.0')  # name of database
mycursor = mydb.raw_connection().cursor()
myDB = Add_To_DB.mydb
myCursor = Add_To_DB.mycursor
df = pd.read_excel('MNM_Rotterdam_5_Daily_Details-20191216081027 - Copy (2).xlsx',sheet_name='GSM Details')
#pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 100000)

print(df)
date=df['Date']
GBSC=df['GBSC']
Site_Name=df['Site Name']
Cell_CI=df['Cell CI']
cell_LAC=df['Cell LAC']
cell_name=df['Cell Name']
cell_index=df['CellIndex']
site_name = df['Site Name']


comp_zip=zip(GBSC,Site_Name,Cell_CI,cell_LAC,cell_name,cell_index)
new_df=pd.DataFrame(comp_zip)
new_df.columns  = new_df.columns.str.replace('\s+','')
new_df.to_sql('MNM_Rotterdam_5_Daily_Details-20191216081027', con=mydb, if_exists='append', index=True)

数据库部分:

from sqlalchemy import create_engine

class Add_To_DB():
    # connect db
    mydb = create_engine('mssql+pyodbc://WWX542337CDCD\SMARTRNO_EXPRESS/myDB?driver=SQL+Server+Native+Client+11.0')
    mycursor = mydb.raw_connection().cursor()

    def insert_into_db(insert_zip, mycursor, mydb):
        sql = "Insert INTO [myDB].[dbo].[mnm_rotterdam_5_daily_details-20191216081027] (GBSC, SiteName, CellCI, CellLAC, CellName, CellIndex) values (?,?,?,?,?,?)"
        val = insert_zip
        for v in val:
            print(v)

注意,Excel 文件和数据库 sql 的 Columns name 不同...不同之处在于 SQL Data-Base 中没有空格,而 Excel 文件中有空格

【问题讨论】:

  • 两个可能的问题,一个你的列是整数,转换它们df.columns = df.columns.astype(str) 第二个,你在MNM_Rotterdam_5_Daily_Details-20191216081027 中没有匹配的目标列检查列匹配完全匹配
  • @Datanovice 实际上我不明白那是什么意思:(
  • 删除应该可以解决问题的df.columns.astype(str)
  • @Datanovice AttributeError: Can only use .str accessor with string values (i.e. inferred_type is 'string', 'unicode' or 'mixed') 同样的错误:(

标签: python pandas sqlalchemy pandas-to-sql


【解决方案1】:

请注意,Excel 文件和数据库 sql 之间的列名称存在差异... 不同之处在于 SQL 数据库中没有空格,而 Excel 文件中有空格

这是您的问题,您的目标列必须与您的目标数据库列匹配,否则它怎么知道要匹配哪些列?

查看This答案了解更多信息。

可以通过以下两种方式之一解决您的问题,

new_df.columns  = new_df.columns.str.replace('\s+','')

或者你可以调用你的数据库

df = pd.read_sql("SELECT * TOP 1 from MNM_Rotterdam_5_Daily_Details-20191216081027",con=mydb) 

然后你可以通过调用df.columns来比较这两列

【讨论】:

  • 我试过你的代码,发现这个错误AttributeError: Can only use .str accessor with string values (i.e. inferred_type is 'string', 'unicode' or 'mixed'),我会在你更新后重试:)
  • 我刚刚在自己的数据集上尝试过,效果很好。你确定你打电话给new_df.columns 吗?
  • 什么是df print(type(df))
  • 它是&lt;class 'pandas.core.frame.DataFrame'&gt;
【解决方案2】:

我在编写 Python 代码时遇到错误。如果有人像我一样使用Python连接SQL,把'换成",问题就解决了。

这行得通:

cursor.execute("SELECT * from names where n like 'AA' ")

这不是:

cursor.execute('SELECT * from names where n like 'AA' ')

【讨论】:

  • 注意单引号也可以转义:\'AA\'
【解决方案3】:
[SQL: INSERT INTO [MNM_Rotterdam_5_Daily_Details-20191216081027] ([index], [0], [1], [2], >[3], [4], [5]) VALUES (?, ?, ?, ?, ?, ?, ?)]

您不能使用0, 1, 2, 3 ... 作为列名。

【讨论】:

  • 非常感谢您的回答,但请您检查我的数据库部分,我现在才发布:)
  • 1. “if_exists='append'”表示当表'MNM_Rotterdam_5_Daily_Details-20191216081027'已经存在时,插入sql必须使用该表中完全相同的列。
  • 2. to_sql 语句不使用“index_lable”,在插入 sql 中会使用索引名作为列名。并且您的 df 的索引名称是 [0, 1, 2...]。与表“MNM_Rotterdam_5_Daily_Details-20191216081027”的列不匹配。这就是它失败的原因
  • 3.纠正它的两种方法。第一个是用表的列名重命名 df 的索引名称。二是在to_sql语句中设置“index_lable”。
  • 4.我不是说英语的人。如果我的回复让你觉得不礼貌或有什么奇怪的地方,我很抱歉。希望这可以帮助你。
猜你喜欢
  • 2021-06-05
  • 2013-06-23
  • 2021-08-06
  • 2019-11-24
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多