MSSQLDatabaseException: (544, b“当 IDENTITY_INSERT 设置为 OFF 时,无法在 table 'RM_ST_FFA_Control_table' 中为标识列插入显式值
MSSQLDatabaseException: (544, b"Cannot insert explicit value for identity column in table 'RM_ST_FFA_Control_table' when IDENTITY_INSERT is set to OFF
我正在使用 Jupyter Notebook 并在 Python 中编写代码。我试图访问数据库 table 并将内容存储在数据框中:
engine_query="mssql+pymssql://"+username+":"+password+"@"+server+"/"+database
sql_engine = sqlalchemy.create_engine(engine_query).connect()
query = "SELECT * FROM RM_ST_FFA_Control_table"
mail_details = pd.read_sql(query, sql_engine)
此处,RM_ST_FFA_Control_table
有 5 列 'P_Key','SR_NO','Details','Value','Last_Update_Date'(It is in datatime format)
,值存储为:
| P_Key | SR_NO | Details | Value | Last_Update_Date
---------------------------------------------------
| 1 | 1 | server | 10.129.29.153 | 2021-11-18
| 2 | 2 | user | snt.marketdata | 2021-11-18
我错误地将此 table 截断为:
with sql_engine.connect() as con:con.execution_options(autocommit=True).execute("TRUNCATE TABLE RM_ST_FFA_Control_table;")
我想再次恢复此 table 的内容,所以我想制作一个新的数据帧并将数据传输到此 table 为:
data = [[1, 1,'server','10.129.29.153','2021-11-18'],
[2, 2,'user','snt.marketdata','2021-11-18'],
]
df = pd.DataFrame(data, columns = ['P_Key','SR_NO','Details','Value','Last_Update_Date'])
df['Last_Update_Date'] = pd.to_datetime(df['Last_Update_Date'], format='%Y-%m-%d')
df.to_sql(con=sql_engine, name='RM_ST_FFA_Control_table', if_exists='append', index=False)
但我收到的错误消息是:
MSSQLDatabaseException: (544, b"Cannot insert explicit value for identity column in table 'RM_ST_FFA_Control_table' when IDENTITY_INSERT is set to OFF.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
任何人都可以通过 IDENTITY_INSERT 设置为开来帮助我解决这个问题。我无权访问 SQL 服务器,我正在使用 SqlAlchemy。是否可以在不删除此 table 并创建另一个同名 table 的情况下解决此问题?
如有任何帮助,我们将不胜感激。
您可以在调用 .to_sql()
之前简单地发送 SET IDENTITY_INSERT RM_ST_FFA_Control_table ON
:
import pandas as pd
import sqlalchemy as sa
connection_url = (
"mssql+pyodbc://scott:tiger^5HHH@192.168.0.199/test"
"?driver=ODBC+Driver+17+for+SQL+Server"
)
engine = sa.create_engine(
connection_url,
fast_executemany=True,
)
table_name = "RM_ST_FFA_Control_table"
with engine.begin() as conn:
# create empty table for testing
conn.exec_driver_sql(f"DROP TABLE IF EXISTS {table_name}")
conn.exec_driver_sql(
f"CREATE TABLE {table_name} (id int identity primary key, x nvarchar(50))"
)
df = pd.DataFrame([(1, "foo"), (3, "baz")], columns=["id", "x"])
# enable inserting values into IDENTITY column
conn.exec_driver_sql(f"SET IDENTITY_INSERT {table_name} ON")
df.to_sql(table_name, conn, if_exists="append", index=False)
# check results
print(conn.exec_driver_sql(f"SELECT * FROM {table_name}").fetchall())
# [(1, 'foo'), (3, 'baz')]
我正在使用 Jupyter Notebook 并在 Python 中编写代码。我试图访问数据库 table 并将内容存储在数据框中:
engine_query="mssql+pymssql://"+username+":"+password+"@"+server+"/"+database
sql_engine = sqlalchemy.create_engine(engine_query).connect()
query = "SELECT * FROM RM_ST_FFA_Control_table"
mail_details = pd.read_sql(query, sql_engine)
此处,RM_ST_FFA_Control_table
有 5 列 'P_Key','SR_NO','Details','Value','Last_Update_Date'(It is in datatime format)
,值存储为:
| P_Key | SR_NO | Details | Value | Last_Update_Date
---------------------------------------------------
| 1 | 1 | server | 10.129.29.153 | 2021-11-18
| 2 | 2 | user | snt.marketdata | 2021-11-18
我错误地将此 table 截断为:
with sql_engine.connect() as con:con.execution_options(autocommit=True).execute("TRUNCATE TABLE RM_ST_FFA_Control_table;")
我想再次恢复此 table 的内容,所以我想制作一个新的数据帧并将数据传输到此 table 为:
data = [[1, 1,'server','10.129.29.153','2021-11-18'],
[2, 2,'user','snt.marketdata','2021-11-18'],
]
df = pd.DataFrame(data, columns = ['P_Key','SR_NO','Details','Value','Last_Update_Date'])
df['Last_Update_Date'] = pd.to_datetime(df['Last_Update_Date'], format='%Y-%m-%d')
df.to_sql(con=sql_engine, name='RM_ST_FFA_Control_table', if_exists='append', index=False)
但我收到的错误消息是:
MSSQLDatabaseException: (544, b"Cannot insert explicit value for identity column in table 'RM_ST_FFA_Control_table' when IDENTITY_INSERT is set to OFF.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
任何人都可以通过 IDENTITY_INSERT 设置为开来帮助我解决这个问题。我无权访问 SQL 服务器,我正在使用 SqlAlchemy。是否可以在不删除此 table 并创建另一个同名 table 的情况下解决此问题?
如有任何帮助,我们将不胜感激。
您可以在调用 .to_sql()
之前简单地发送 SET IDENTITY_INSERT RM_ST_FFA_Control_table ON
:
import pandas as pd
import sqlalchemy as sa
connection_url = (
"mssql+pyodbc://scott:tiger^5HHH@192.168.0.199/test"
"?driver=ODBC+Driver+17+for+SQL+Server"
)
engine = sa.create_engine(
connection_url,
fast_executemany=True,
)
table_name = "RM_ST_FFA_Control_table"
with engine.begin() as conn:
# create empty table for testing
conn.exec_driver_sql(f"DROP TABLE IF EXISTS {table_name}")
conn.exec_driver_sql(
f"CREATE TABLE {table_name} (id int identity primary key, x nvarchar(50))"
)
df = pd.DataFrame([(1, "foo"), (3, "baz")], columns=["id", "x"])
# enable inserting values into IDENTITY column
conn.exec_driver_sql(f"SET IDENTITY_INSERT {table_name} ON")
df.to_sql(table_name, conn, if_exists="append", index=False)
# check results
print(conn.exec_driver_sql(f"SELECT * FROM {table_name}").fetchall())
# [(1, 'foo'), (3, 'baz')]