简体   繁体   中英

How fix “pymysql.err.IntegrityError: (1048, ”Column 'ProTitre' cannot be null“)” error in python?

I'm pretty new with sql in Python. I have some data that I want to send to my SQL DB. But I've got a pymysql.err.IntegrityError: (1048, "Column 'ProTitre' cannot be null") error.

I've tried putting titre and descr on a var data[] and then put data[] on cursor.execute(sql, data) .

And put directly sql = ("INSERT INTO projet (ProTitre, ProDescription) VALUES (%s, %s)", (self.sqlEntryTitreProjet, self.sqlEntryDescrProjet)) or cursor.execute(sql, self.sqlEntryTitreProjet, self.sqlEntryDescrProjet) .

def exemple(self):
    self.sqlEntryTitreProjet = "test1"
    self.sqlEntryDesrcrProjet = "test descr"

def myConnectin(self):
    self.connection = pymysql.connect(host='127.0.0.1',
                                 user='root',
                                 password='',
                                 db='bd_outiltesting',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)


def addItemProjet(self):
    self.myConnection()
    titre = self.sqlEntryTitreProjet
    descr = self.sqlEntryDesrcrProjet
    sql = ("INSERT INTO projet (ProTitre, ProDescription) VALUES (%s, 
    %s)", (titre, descr))
    cursor = self.connection.cursor()
    cursor.execute(sql)
    self.connection.commit()
    print("cursor.description:", cursor.description)
    cursor.close()
    if self.connection.is_connected():
        self.connection.close()
        print("MySQL connection is closed")

pymysql.err.IntegrityError: (1048, "Column 'ProTitre' cannot be null")

Try:

sql = "INSERT INTO projet (ProTitre, ProDescription) VALUES (%s, %s)"
cursor = self.connection.cursor()
cursor.execute(sql, (titre, descr))

args should be tuple, list or dict afaik.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM