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 引数エラーでした(ip,)

引用符は必要ありませんでした。

于 2019-12-01T22:12:07.260 に答える
0

ブラケットを追加するip_addressか、削除する必要があります。

于 2019-12-01T22:11:39.517 に答える