what is wrong with my code,
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
I am trying to insert ip address as string to mysql table. But I am getting this error:
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
You are missing the ""
for the IP to be considered a string:
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
EDIT
You also need a coma to pass parameters as tuple (ip,)
instead of (ip)
The actual errors were a syntax error (for not having brackets around (ip_address)
and a python arguments error for not having the coma on the tuple (ip,)
.
The quotes were not necessary.