'sql_require_primary_key' - 无法在不创建主键的情况下将我的数据帧发送到 mysql 服务器
'sql_require_primary_key' - Can't send my dataframe to mysql server without creating a primary key
范围:
我正在创建一个数据管道,它需要我通过 SSH 连接到 linux 虚拟机(我所有的代码都在那里),然后将数据发送到 MariaDB 服务器
我的 python 脚本从 CRM 中提取数据,将其保存到数据框中,然后将其发送到数据库。
当我 运行:
from sqlalchemy import create_engine
f= open("db credentials,txt", "r") # parse credentials in txt to hide sensitive data
sql_conn = f.read()
conn = create_engine(sql_conn)
df.to_sql(name='test_trx_1day', con=conn, if_exists='replace', index=False)
我收到以下错误:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (3750, "Unable to create or change a table without
a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset t
his variable to avoid this message. Note that tables without a primary key can cause performance problems in row-ba
sed replication, so please consult your DBA before changing this setting.")
我是运行宁:
python: 3.7.3
pyCharm IDE
使用 pymysql 作为 mysql 连接器 \
当我 运行 我的代码在 我的机器 并且目的地是 我的本地 MYSQL DB 它有效!
注意:我对 linux env 不是很熟悉,所以我边学边学。
感谢@gordthompson 的解决方案。写在这里,因为我无法在评论部分将其标记为已解决。
- 首先在 mysql 中创建具有适当数据类型的空 table。
- 创建您的主键。 (我有一个问题,我无法将现有列设置为 PK,因此创建了一个默认的 INT auto_increment。我后来更改了 table 以切换 PK)
- 运行 相同 df.to_sql 并且没有错误。
范围: 我正在创建一个数据管道,它需要我通过 SSH 连接到 linux 虚拟机(我所有的代码都在那里),然后将数据发送到 MariaDB 服务器
我的 python 脚本从 CRM 中提取数据,将其保存到数据框中,然后将其发送到数据库。
当我 运行:
from sqlalchemy import create_engine
f= open("db credentials,txt", "r") # parse credentials in txt to hide sensitive data
sql_conn = f.read()
conn = create_engine(sql_conn)
df.to_sql(name='test_trx_1day', con=conn, if_exists='replace', index=False)
我收到以下错误:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (3750, "Unable to create or change a table without
a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset t
his variable to avoid this message. Note that tables without a primary key can cause performance problems in row-ba
sed replication, so please consult your DBA before changing this setting.")
我是运行宁:
python: 3.7.3
pyCharm IDE
使用 pymysql 作为 mysql 连接器 \
当我 运行 我的代码在 我的机器 并且目的地是 我的本地 MYSQL DB 它有效!
注意:我对 linux env 不是很熟悉,所以我边学边学。
感谢@gordthompson 的解决方案。写在这里,因为我无法在评论部分将其标记为已解决。
- 首先在 mysql 中创建具有适当数据类型的空 table。
- 创建您的主键。 (我有一个问题,我无法将现有列设置为 PK,因此创建了一个默认的 INT auto_increment。我后来更改了 table 以切换 PK)
- 运行 相同 df.to_sql 并且没有错误。