0

我的代码有什么问题,

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
4

2 回答 2

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 参数错误是因为 tuple 上没有逗号(ip,)

引号是不必要的。

于 2019-12-01T22:12:07.260 回答
0

您需要在周围添加括号ip_address或将其删除。

于 2019-12-01T22:11:39.517 回答