我的代码有什么问题
with connect_db() as conn:
cursor = conn.cursor()
stmt = """INSERT INTO ip_records ip_address VALUES (%s)"""
try:
cursor.execute(stmt, (ip))
except mysql.connector.Error as err:
print err
return
我正在尝试将 IP 地址作为字符串插入到 mysql 表中。但我收到此错误:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ip_address VALUES (192.168.11.111)' at line 1
最佳答案
您缺少将 IP 视为字符串的 ""
:
with connect_db() as conn:
cursor = conn.cursor()
stmt = """INSERT INTO ip_records (ip_address) VALUES (%s)"""
try:
cursor.execute(stmt, (ip,))
except mysql.connector.Error as err:
print err
return
编辑
您还需要一个逗号来将参数作为元组 (ip,)
而不是 (ip)
实际的错误是一个语法错误(因为 (ip_address)
周围没有括号,以及一个 python 参数错误,因为元组上没有逗号 (ip,)
.
引号不是必需的。
关于python - 尝试将 ip 地址作为字符串插入 mysql 表时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130433/