Python Forum
mysql.connector - incorrect integer value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql.connector - incorrect integer value
#1
Hi all,

I've been playing around with mysql.connector and trying to insert data into a mysql database. When using the %s or %d syntax I receive a "Incorrect integer value" error but when using a value directly in the insert the code works as expected. I'm no Python buff so very likely something I'm missing but I have not idea what it might be, any help would be much appreciated.

Just to add, I am aware of sql-mode issue in the my.cnf/my.ini file that may cause this sort of issue and I have address it by changing the the mysql-mode to mysql-mode=""

The first code block below uses a value directly in the insert statement and works correctly. The second code block uses the %d syntax and throws the below error

import mysql.connector

cnx = mysql.connector.connect(user='foo', password='bar', database='db', host='ipaddress')
cursor = cnx.cursor()

stmt = ("INSERT INTO schedule (gid) VALUES ('12345')")

cursor.execute(stmt)

cnx.commit()
cursor.close()
cnx.close()
import mysql.connector

cnx = mysql.connector.connect(user='foo', password='bar', database='db', host='ipaddress')
cursor = cnx.cursor()

stmt = ("INSERT INTO schedule (gid) VALUES ('%d')")
value = 12345

cursor.execute(stmt, value)

cnx.commit()
cursor.close()
cnx.close()
Error:
Traceback (most recent call last):  File "C:\Users\mmcsweeney\Desktop\test.py", line 9, in <module>    cursor.execute(stmt, value)  File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 515, in execute    self._handle_result(self._connection.cmd_query(stmt))  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 488, in cmd_query    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result    raise errors.get_exception(packet) mysql.connector.errors.DatabaseError: 1366 (HY000): Incorrect integer value: '%d' for column 'gid' at row 1
Reply
#2
Try:
cur.execute("INSERT INTO schedule (gid) VALUES (%s)", (value))
Reply
#3
Thanks for the response Snippsat.

I've given it a bash and no dice. Your suggestion (below) produces the bellow Traceback

I've had a look at the mysql.connector documentation and it seems to suggest the way I was doing it previously should be correct?


import mysql.connector

cnx = mysql.connector.connect(user='foo', password='bar', database='db', host='ipaddress')
cursor = cnx.cursor()

value = 12345

cursor.execute("INSERT INTO schedule (gid) VALUES (%s)", (value))

cnx.commit()
cursor.close()
cnx.close()
Error:
[b]Traceback (most recent call last):  File "C:\Users\mmcsweeney\Desktop\test.py", line 8, in <module>    cursor.execute("INSERT INTO schedule (gid) VALUES (%s)", (value))  File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 515, in execute    self._handle_result(self._connection.cmd_query(stmt))  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 488, in cmd_query    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))  File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result    raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1 [Finished in 0.1s][/b]

Ok so I had a quick look at it again and the below seems to work as expected.
Thanks Snippsat for getting me moving in the right direction

import mysql.connector

cnx = mysql.connector.connect(user='foo', password='bar', database='db', host='ipaddress')
cursor = cnx.cursor()

value = 12345

cursor.execute("INSERT INTO schedule (gid) VALUES ('%s')" % (value))

cnx.commit()
cursor.close()
cnx.close()
Reply
#4
(May-28-2017, 10:25 AM)Milo Wrote: Ok so I had a quick look at it again and the below seems to work as expected.

Well, no.  That's using string formatting and manual quoting, which bypasses mysql's sanitation.  So it works, but only if nobody ever tries to break your database.
This is the way you should be doing it:
cursor.execute("INSERT INTO schedule (gid) VALUES (%s)", (value, ))
More examples can be found in the docs: https://dev.mysql.com/doc/connector-pyth...elect.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 683 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Code is returning the incorrect values. syntax error 007sonic 6 1,231 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,711 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,088 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,690 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python ilknurg 3 5,630 Jan-18-2022, 06:25 PM
Last Post: ilknurg
  Program stuck at mysql.connector.connect zazas321 1 2,084 Jul-29-2021, 10:49 AM
Last Post: zazas321
  MYSQL.CONNECTOR ERROR DB1 8 3,848 Jul-23-2021, 03:31 AM
Last Post: DB1
  openpyxl incorrect delete rows VladislavM 6 4,123 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Incorrect Type Error milkycow 4 2,910 Jun-25-2021, 06:04 AM
Last Post: milkycow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020