Pit encountered by python connecting database and inserting database data

Posted by s_ff_da_b_ff on Mon, 16 Dec 2019 19:44:53 +0100

When inserting data in Python, no error is reported, but no newly added data appears in the database

Reason: the commit operation is missing.

Solution: when Python operates the database, if the data table is modified / deleted / added, the system will save the operation in memory. Only when commit() is executed, the operation will be submitted to the database.

But there are always unexpected pit codes as follows:

import pymysql

class Connection:

    def __init__(self):
        self.host = 'localhost'
        self.user = 'nameit'
        self.password = 'YES'
        self.port = 3306
        self.db = 'Xomai'


    def connection(self):

        db = pymysql.connect(host=self.host, user=self.user, password=self.password, port=self.port, db=self.db)
        cur = db.cursor()
        return db, cur

    def create_table(self, cur):

        sql = """CREATE TABLE `activity_feedback` (
                  `id` bigint(20) NOT NULL AUTO_INCREMENT,
                  `inst_id` bigint(20) DEFAULT NULL COMMENT 'ID',
                  `broadcast_id` bigint(20) DEFAULT NULL COMMENT 'Hello',
                  `student_id` bigint(20) DEFAULT NULL COMMENT 'Student ID',
                  `content` varchar(1024) DEFAULT NULL COMMENT 'Student content',
                  `comment` varchar(255) DEFAULT NULL COMMENT 'Notes',
                  `gmt_create` datetime DEFAULT NULL,
                  `gmt_modify` datetime DEFAULT NULL,
                  PRIMARY KEY (`id`),
                  KEY `activity_feedback_student_id_index` (`student_id`)
                ) ENGINE = InnoDB AUTO_INCREMENT = 1050 DEFAULT CHARSET = utf8mb4 COMMENT = 'Student table'"""

        cur.execute(sql)


    def insert(self, id, inst_id, broadcast_id, student_id, content, comment, gmt_create, gmt_modify):

        sql = """INSERT INTO `activity_feedback` (
                  `id`, `inst_id`, `broadcast_id`, `student_id`, `content`, `comment`, `gmt_create`, `gmt_modify`)
                  VALUES ('{}','{}','{}','{}','{}','{}','{}','{}')""".format(id, inst_id, broadcast_id, student_id, content, comment, gmt_create, gmt_modify)

        try:
            self.connection()[1].execute(sql)
            self.connection()[0].commit()
        except:
            self.connection()[0].rollback()

if __name__ == '__main__':
    conn = Connection()
    conn.insert(123, 123, 324, 3451, 'ajdf', 'sdfs', '2013-2-5', '2014-3-4')

At first glance, there seems to be a commit. Why hasn't there been a commit in the database? Take a closer look

        try:
            self.connection()[1].execute(sql)
            self.connection()[0].commit()
        except:
            self.connection()[0].rollback()

Is the same object returned by the connection() call method?

No, I'm tired. I've been working for a long time. I only blame myself for being too tender.

Correct writing:

        try:
            cons = self.connection()
            cons[1].execute(sql)
            cons[0].commit()
            cons[0].close()
        except:
            cons[0].rollback()

 

Topics: SQL Database Python