Why does execute () append null values and executemany() doesn't? - python

While using MySQL through python, on using execute() function null values are getting updated in the specific table, but on using executemany() the null values are returning errors.
The working commands:
mycursor = mydb.cursor()
mycursor.execute("INSERT INTO users (name, fav) VALUES('John', null)"
mydb.commit()
The code that doesn't work:
mycursor = mydb.cursor()
sql = "INSERT INTO users (name, fav) VALUES(%s, %s)"
val =[
('Samantha', 154),
('Thalia', 155),
('Jacobs', null),
('Jamie', null)
]
mycursor.executemany(sql, val)
mydb.commit()
The error generated is as follows:
Traceback (most recent call last):
File "demo_python.py", line 14, in
('Jacobs', null),
NameError: name 'null' is not defined

Related

ProgrammingError: while insert data in SQL using python using execute

Here is my code:
import mysql.connector
import datetime
import dateutil.parser
import soundfile as sf
mydb = mysql.connector.connect(
host="localhost",
user="py",
password="12345678",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (adress) VALUES (%s)"
val = ("Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
ProgrammingError Traceback (most recent call last)
in
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
This is due to MySQL INSERT statement syntax error. You can rewrite the query as follows:
val = "Highway 21"
sql = f'INSERT INTO customers (adress) VALUES ("{val}");'
print(sql)
# INSERT INTO customers (adress) VALUES ("Highway 21");
You have to add a , after the "Highway 21" value since val needs to be a tuple because mycursor.execute() expects a list of values:
val = ("Highway 21",)
you can also go through this code
sql = "INSERT INTO customers (adress) VALUES ('%s')"
val = ("Highway 21")
mycursor.execute(sql%(val))
val = ("Highway 21")
sql = "INSERT INTO customers (adress) VALUES ('{}') ".format(val)
print(sql)
use this you will not get error

ProgrammingError: Not all parameters were used in the SQL statement

Need a little help here, think I have tried everything to fix this issue but keep getting the same error message ProgrammingError: Not all parameters were used in the SQL statement
CODE:
mydb = mysql.connector.connect(host="localhost", user="xx", passwd="", database="xx")
mycursor = mydb.cursor()
sql = "INSERT INTO authors(id, first_name, last_name, photo) VALUES(%s, %s, %s,%s)"
values = ['''(1,'Herbert','Methley','NULL'),
(2,'Olive','Wellwood','NULL'),
(3,'Francis','Tugwell ','NULL'),(4,'Randolph','Henry Ash','NULL'),
(5,'Christabel','LaMotte ','NULL'),(7,'Robert','Dale Owen','NULL'),
(12,'Leonora','Stern ','NULL'),(14,'Mrs.','Lees ','NULL'),(19'Josephine','M. Bettany','NULL'),
(20,'Kester','Bellever ','NULL'),(22,'Sylvia','Leigh ','NULL'),(24,'Elizabeth','Temple ','NULL'),
(25,'Marsha','Patterson ','NULL'),(26,'Samuel','Humber ','NULL'),(27,'James','Fallon ','NULL'),
(28,'Beatrice','Quinn ','NULL'),(29,'Susan','Magar ','NULL'),(30,'Clinton','York ','NULL'),
(31,'Canton','Lee ','NULL'),(32,'Rod','Keen ','NULL'),(33,'Hilda','Simpson ','NULL'),
(34,'S.','M. Justice','NULL'),(35,'Charles','Green ','NULL'),(36,'Richard','Brautigan ','NULL'),
(37,'Bill','Lewis ','NULL'),(38,'Chuck',' ','NULL'),(39,'Doctor','O. ','NULL'),
(40,'Harlow','Blade ','NULL'),(41,'Barbara','Jones ','NULL'),(42,'Fred','Sinkus ','NULL'),
(43,'Thomas','Funnel ','NULL'),(44,'Patricia','Evens Summers','NULL'),
(45,'Reverend','Lincoln Lincoln','NULL'),(47,'Edward','Fox ','NULL')''']
mycursor.executemany(sql, values)
mydb.commit()
print(mycursor.rowcount, "was inserted")
ERROR MESSAGE:
Traceback (most recent call last):
File "C:\Users\xxx\Documents\Python Scripts\PYTHONMYSQL_TEST.py",
line 30, in
mycursor.executemany(sql, values)
File
"C:\Users\xxx\AppData\Local\Programs\Anaconda\lib\site-packages\mysql\connector\cursor.py",
line 668, in executemany
stmt = self._batch_insert(operation, seq_params)
File
"C:\Users\xxx\AppData\Local\Programs\Anaconda\lib\site-packages\mysql\connector\cursor.py",
line 614, in _batch_insert
"Not all parameters were used in the SQL statement")
ProgrammingError: Not all parameters were used in the SQL statement
Any help would be appreciated, I believe I have given everyone all the information needed. If I have forgot something please let me know
thanks

Python objects to JSON to MySQL

I've got a problem when trying to insert a json, which was converted from a python object with json.dumps, into a MySQL database. The connection to the database is working with another python file. I've already tried to just insert values, which was working, but with the json file it's not working.
My Python file:
import json
import dbConnection
cur = dbConnection.cursor
cnx = dbConnection.conn
DEVICES = {
"id": "1",
"isPoweredOn": "True",
"os": "Linux"
}
j = json.dumps(DEVICES)
print(j)
sql = "INSERT INTO DEVICES (id, isPoweredOn, os) VALUES (%s, %s, %s)"
val = (json.dumps(DEVICES))
cur.execute(sql, val)
cnx.commit()
print(cur.rowcount, "record inserted.")
Error code I get, when trying to execute:
"id": "1", "isPoweredOn": "True", "os": "Linux"}
Traceback (most recent call last):
File "dbInit.py", line 22, in <module>
cur.execute(sql, val)
File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/site-packages/mysql/connector/cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/site-packages/mysql/connector/connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/home/silvan/.virtualenvs/pyproj1/lib/python3.8/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, %s, %s)' at line 1
My CREATE TABLE code:
CREATE TABLE DEVICES(id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, isPoweredOn BOOLEAN NOT NULL, os VARCHAR(50) NOT NULL);
Thanks for any help in advance!
You need to json.loads(j) and assign it to a variable, then you can access the values properly.
Try :
import json
import dbConnection
cur = dbConnection.cursor
cnx = dbConnection.conn
DEVICES = {
"id": "1",
"isPoweredOn": False ,
"os": "Linux"
}
j = json.dumps(DEVICES)
values = json.loads(j)
'''
# Quick debugging
print(j , type(j))
print(values , type(values))
print(values['isPoweredOn'])
'''
sql = "INSERT INTO DEVICES (id, isPoweredOn, os) VALUES (%s, %s, %s)"
val = ( '' , values['isPoweredOn'] , values['os'])
cur.execute(sql, val)
cnx.commit()
print(cur.rowcount, "record inserted.")
Also since you defined id to be INT AUTO_INCREMENT PRIMARY KEY NOT NULL it , you can't insert device id wich is values['id'] to id column, you can alter DEVICES table and create a new column called device_id for storing the device id if you need really need to store values['id']
Firstly, cast the DEVICES to dict, then Here's the format.
sql = "INSERT INTO DEVICES (`id`, `isPoweredOn`, `os`) VALUES (%(id)s, %(isPoweredOn)s, %(os)s)"
Then Execute it :
try:
cur.execute(sql, DEVICES)
cnx.commit()
except error:
print(error)
Cheers!!

python3 mysql connector module throws exception - ValueError: Could not process parameters

I am trying to get past this error that is haunting me. I built a simple script to populate a single column database.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS data(pass_word VARCHAR(20))")
val = 'test'
sql = "INSERT INTO data(pass_word) VALUES '%s'"
mycursor.execute(sql, (val))
mydb.commit()
It creates the table no problem, so I know the connector is working. But it refuses to insert the val into pass_word.
It throws the following exception
Press ENTER or type command to continue
Traceback (most recent call last):
File "sql-try.py", line 19, in <module>
mycursor.execute(sql, (val))
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/usr/local/lib/python3.6/dist-packages/mysql/connector/connection_cext.py", line 538, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
I think the issue was that my table only had one column, so when I was passing the val, val itself needed to be a tuple. Changing it to (sql, (val,)) did not address the issue. When I created a table with more than one column, the problem went away.
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="password",
database="mydatabase"
)
mycursor = mydb.cursor()
myname = 'jimmy'
password = 'grizwald'
mycursor.execute("CREATE TABLE IF NOT EXISTS data3(my_name VARCHAR(20), pass_word VARCHAR(20))")
val = (myname, password )
sql = "INSERT INTO data3(my_name, pass_word) VALUES (%s, %s)"
mycursor.execute(sql, val)
mydb.commit()

MariaDB executemany on duplicate key in Debian Stretch

Could anyone explain me is it bug or feature?
Debian Stretch
mariadb-server-10.1.26
mariadb-client-10.1.26
MySQLdb-1.2.5
This python code perfectlly works in Debian Jessie, but failed in Stretch with error:
Traceback (most recent call last):
File "bug_check.py", line 17, in <module>
cur.executemany(q, p)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 255, in executemany
self.errorhandler(self, TypeError, msg)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting
Python code:
#!/usr/bin/python
# -*- coding: UTF-8 *
import MySQLdb
db = MySQLdb.connect(host='192.168.1.183', user='root', passwd='password', db='test', charset='utf8')
cur = db.cursor()
q = """INSERT INTO test2 (id, value)
VALUES (%s, %s)
ON DUPLICATE KEY
UPDATE value=%s
"""
p = [(1, 7, 7)]
# failed
cur.executemany(q, p)
# working
for i in p:
cur.execute(q, i)
db.commit()
db.close()
Database:
CREATE TABLE `test2` (
`id` bigint(8) NOT NULL,
`value` float NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `test2`
ADD PRIMARY KEY (`id`);
ALTER TABLE `test2`
MODIFY `id` bigint(8) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
I am trying mariadb-10.2, pymysql but anyway error occurs in Stretch.
This may work: Change UPDATE value=%s to UPDATE value=VALUES(value) and get rid of the last 7 in the array.
If that does not work, then here is more discussion:
I think executemany is trying to build
INSERT ...
VALUES (...),
(...),
(...);
But it does not know how to convert the IODKU syntax into a repeated list like that. Bottom line: you can probably use executemany with INSERT, INSERT IGNORE, REPLACE, but not IODKU.
For IODKU to work, Stretch needs to be smart enough to do this:
INSERT INTO test2 (id, value)
VALUES
(%s, %s),
(%s, %s),
(%s, %s),
(%s, %s),
etc
ON DUPLICATE KEY
UPDATE value=VALUES(value)
Note that the repetition is in the middle, not on the end, as in the other cases. However you have to use the VALUES() pseudo-function to avoid the %s in the UPDATE clause.

Categories