Ad

How Fix "pymysql.err.IntegrityError: (1048, "Column 'ProTitre' Cannot Be Null")" Error In Python?

- 1 answer

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")

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad