Resolved pymssql._mssql.MSSQLDatabaseException: (207, b"Invalid column name 'review'.DB-Lib error message



已解决pymssql._mssql.MSSQLDatabaseException: (207, b"Invalid column name ‘review’.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")











error code



A friend of the fan group wanted to use pandas and sqlalchemy to read the database information, but an error occurred (at that time, he felt a lot of cold, and he came to me for help, and then successfully helped him solve it, by the way, I hope it can help More friends who will not solve this bug), the error code is as follows:

import pandas as pd
import sqlalchemy

# 1. 读取Excel
df = pd.read_excel("./合并.xls")
print(df)

# 2. 入库
engine = sqlalchemy.create_engine('mssql+pymssql://账号:密码@服务器地址:端口号/库名')
df.to_sql("表名", con=engine, if_exists="append", index=False, chunksize=2000)

Screenshot of error message:


insert image description here







error translation



Error message translation :

PymSQL._mssql. MSSQLSDatabaseException: (207, b"Invalid column name 'review'. DB Lib Error Message 20018, Severity 16:\nGeneral SQL Server Error: Check message from SQL Server\n)





Reason for error



Reason for error : pymssql._mssql.MSSQLDatabaseException: (207, b"Invalid column name 'review'.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n") error, There are two possibilities: 1. The column name in the code is wrong, 2. The data table does not have this column name.



Solution


Carefully check the data table column names, and the column names in the code (if you make a mistake, you can modify the column name). Fans found that they forgot to add a new field. Just execute the following command to add a column name:

alter table table name add column name nvarchar(MAX) null

insert image description here


write at the end


This article has been included in: "Farewell to Bug" column

This column is used to record various difficult bugs encountered in study and work, as well as various problems raised by small partners in the fan group. Article format: error code + error translation + error reason + solution, including program installation, operation If you encounter other problems during the program, if you encounter other problems after subscribing to the column, you can privately chat to help solve them! ! !

Guess you like

Origin blog.csdn.net/yuan2019035055/article/details/126360581