How to ignore pymysql warnings?
How to ignore pymysql warnings?
Consider this code:
import pymysql
db= pymysql.connect("localhost","root","","raspi")
cursor = db.cursor()
cursor.execute("INSERT INTO access(username)VALUES('hello')")
db.commit()
db.close()
when I run it I'll get this error:
C:Python36libsite-packagespymysqlcursors.py:165: Warning: (1364,"Field'salt' doesn't have a default value")result = self._query(query)
How do I ignore pymysql warnings?
salt
null
banana
1 Answer
1
Reason why you're getting that error is because your salt
field is not allowing NULL values but you're trying to insert them, probably your table creation sql code looks something like this:
salt
CREATE TABLE `access` (
`username` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
...
`salt` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci'
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
If you just wanted to allow NULL values for that field maybe your SQL code would need to be change into something like:
...
`salt` VARCHAR(50) NULL COLLATE 'utf8_unicode_ci'
...
Anyway, if you still insist to keep your original db structure and want to learn how filter out warnings, here's a possible way to you could do it:
import pymysql
import warnings
db = pymysql.connect("localhost", "root", "", "raspi")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
cursor = db.cursor()
cursor.execute("INSERT INTO access(username)VALUES('hello')")
db.commit()
db.close()
For more information, take a look to the warnings docs
solve my issues thanks for the help
– ArchDevOps
Jul 1 at 10:50
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Set a default value for
salt
, be it empty,null
orbanana
.– Alex Karshin
Jun 30 at 9:44