无法使用 mysql.connector 将值插入 mysql 服务器
Trouble inserting values into mysql server using mysql.connector
我在 class 实现中使用以下函数来插入值 username_s
、 ipadress_s
等等 MYSQL 具有列 username
的数据库, ipadress
等等,但我不断收到以下错误
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column
'username_s' in 'field list'
混淆是列名与列值的互换
def insert_server(self , username_s , ipadress_s , BLACKLISTED_s , clientid_s):
self.connect.database = self.m_database
self.cursor.execute(
"INSERT INTO server"
" ( username , ipadress , blacklisted , clientid )"
"VALUES ( username_s , ip_adress_s , BLACKLISTED_s , clientid_s)")
MySQL 会将 'username_s' 和 VALUES 中的其他名称解释为文字列名称,因为没有格式代码表明这些实际上是变量。您可以查看 MySQL 文档 Inserting data using Connector/Python 中的示例。为了澄清,这是一个简短的 MWE:
import sqlalchemy
# insert connection details
engine = sqlalchemy.create_engine('<YourConnection>')
# insert statement with variable format coding in VALUES
add_client = (
'INSERT INTO server'
' (username, ipadress, blacklisted, clientid )'
' VALUES ( %s, %s, %s, %s)')
# invent some client data
username_s = 'Mary'
ipadress_s = '321.45.67.89'
BLACKLISTED_s = True
clientid_s = 123456
client_data = (username_s, ipadress_s, BLACKLISTED_s, clientid_s)
with engine.connect() as cnx:
cnx.execute(add_client, client_data)
我在 class 实现中使用以下函数来插入值 username_s
、 ipadress_s
等等 MYSQL 具有列 username
的数据库, ipadress
等等,但我不断收到以下错误
mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'username_s' in 'field list'
混淆是列名与列值的互换
def insert_server(self , username_s , ipadress_s , BLACKLISTED_s , clientid_s):
self.connect.database = self.m_database
self.cursor.execute(
"INSERT INTO server"
" ( username , ipadress , blacklisted , clientid )"
"VALUES ( username_s , ip_adress_s , BLACKLISTED_s , clientid_s)")
MySQL 会将 'username_s' 和 VALUES 中的其他名称解释为文字列名称,因为没有格式代码表明这些实际上是变量。您可以查看 MySQL 文档 Inserting data using Connector/Python 中的示例。为了澄清,这是一个简短的 MWE:
import sqlalchemy
# insert connection details
engine = sqlalchemy.create_engine('<YourConnection>')
# insert statement with variable format coding in VALUES
add_client = (
'INSERT INTO server'
' (username, ipadress, blacklisted, clientid )'
' VALUES ( %s, %s, %s, %s)')
# invent some client data
username_s = 'Mary'
ipadress_s = '321.45.67.89'
BLACKLISTED_s = True
clientid_s = 123456
client_data = (username_s, ipadress_s, BLACKLISTED_s, clientid_s)
with engine.connect() as cnx:
cnx.execute(add_client, client_data)