无法使用python在mysql数据库中正确插入NULL值

m1m5dgzv  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(145)

我使用json格式发送数据,这里是我的代码片段,它返回一个错误

def blocks_files(self, db, order_id, op_id, file_ids, images):
        """Creating record in blocks_files table."""
        files_list = []
        keys = list(images.keys())
        for file in file_ids:
            if str(file) in keys:
                new = str()
                for i in range(len(images[str(file)])):
                    new += '%s, ' % str((images[str(file)])[i])
                files_list.append((order_id, op_id, file, new))
            else:
                files_list.append((order_id, op_id, file, None))
        try:
            logging.debug(
                "Inserting data in blocks_files table."
            )
            cursor = db.cursor()
            query = (
                f"INSERT INTO {self.FILES_TABLE}"
                f"(order_id, op_id, file_id, imgs)"
                f"VALUES {(', '.join(map(str, files_list)))};"
            )
            cursor.execute(query)
            db.commit()
            cursor.close()
            logging.debug(
                "Data inserted successfully!"
            )
        except connector.Error as err:
            logging.error(err)

问题出在files_list.append((order_id, op_id, file, None))部分。起初似乎一切正常,但我在日志中得到1054 (42S22): Unknown column 'None' in 'field list'错误。但是我希望它应该在imgs列中放置NULL...我使用下面的topic作为灵感,但没有用。尝试发送“NULL”作为一个字符串不是我想要的,我需要正确的SQL NULL值,差异是在截图proper NULL value"NULL" value

iyr7buue

iyr7buue1#

我发现了一个不那么优雅的解决方案,通过添加一个变量和两个if语句

def blocks_files(self, db, order_id, op_id, file_ids, images):
            """Creating record in blocks_files table."""
            files_list = []
            files_list_no_imgs = []
            keys = list(images.keys())
            for file in file_ids:
                if str(file) in keys:
                    images_list = str()
                    for i in range(len(images[str(file)])):
                        images_list += '%s, ' % str((images[str(file)])[i])
                    files_list.append((order_id, op_id, file, images_list))
                else:
                    files_list_no_imgs.append((order_id, op_id, file))
            try:
                logging.debug(
                    "Inserting data in blocks_files table."
                )
                cursor = db.cursor()
                if files_list:
                    query = (
                        f"INSERT INTO {self.FILES_TABLE}"
                        f"(order_id, op_id, file_id, imgs)"
                        f"VALUES {(', '.join(map(str, files_list)))};"
                    )
                    cursor.execute(query)
                if files_list_no_imgs:
                    query = (
                        f"INSERT INTO {self.FILES_TABLE}"
                        f"(order_id, op_id, file_id)"
                        f"VALUES {(', '.join(map(str, files_list_no_imgs)))};"
                    )
                    cursor.execute(query)
                db.commit()
                cursor.close()
                logging.debug(
                    "Data inserted successfully!"
                )
            except connector.Error as err:
                logging.error(err)

相关问题