python 插入Null值數(shù)據(jù)到Postgresql的操作
數(shù)據(jù)庫(kù)中最好插入Null值。
在python中,暫時(shí)沒(méi)找到通過(guò)sql語(yǔ)句的方式插入Null值。
推薦使用輪子的方法
def insert_sample_data(self, values): # added self since you are referencing it below with self.con.cursor() as cur: sql = "insert into sampletable values (%s, %s, %s)" # Use %s for parameters cur.executemany(sql, values) # Pass the list of tuples directly self.con.commit() list1 = [(1100, 'abc', '{"1209": "Y", "1210": "Y"}'), (1100, 'abc', None)] self.insert_sample_data(list1) # pass the list directly
補(bǔ)充:python連接數(shù)據(jù)庫(kù)插入數(shù)據(jù)庫(kù)數(shù)據(jù)所碰到的坑
Python中插入數(shù)據(jù)時(shí)執(zhí)行后,沒(méi)有報(bào)任何錯(cuò)誤,但數(shù)據(jù)庫(kù)中并沒(méi)有出現(xiàn)新添加的數(shù)據(jù)
原因:
缺少提交操作。
解決方案:
Python操作數(shù)據(jù)庫(kù)時(shí),如果對(duì)數(shù)據(jù)表進(jìn)行修改/刪除/添加等控制操作,系統(tǒng)會(huì)將操作保存在內(nèi)存,只有執(zhí)行commit(),才會(huì)將操作提交到數(shù)據(jù)庫(kù)。
但是總有你想不到的坑代碼如下:
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 '你好', `student_id` bigint(20) DEFAULT NULL COMMENT '學(xué)生ID', `content` varchar(1024) DEFAULT NULL COMMENT '學(xué)員內(nèi)容', `comment` varchar(255) DEFAULT NULL COMMENT '注釋', `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 = '學(xué)員表'""" 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')
咋一看好像也有commit呀,怎么一直在數(shù)據(jù)庫(kù)沒(méi)有,再仔細(xì)看看
try: self.connection()[1].execute(sql) self.connection()[0].commit() except: self.connection()[0].rollback()
connection()調(diào)用方法方法返回的對(duì)象是同一個(gè)嗎?
并不是,心累,搞了半天,只怪自己還太嫩。
正確寫法:
try: cons = self.connection() cons[1].execute(sql) cons[0].commit() cons[0].close() except: cons[0].rollback()
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PyTorch中torch.nn.Linear實(shí)例詳解
torch.nn是包含了構(gòu)筑神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)基本元素的包,在這個(gè)包中可以找到任意的神經(jīng)網(wǎng)絡(luò)層,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.nn.Linear的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06基于OpenCV(python)的實(shí)現(xiàn)文本分割之垂直投影法
本文主要介紹了基于OpenCV(python)的實(shí)現(xiàn)文本分割之垂直投影法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08python接口測(cè)試返回?cái)?shù)據(jù)為字典取值方式
這篇文章主要介紹了python接口測(cè)試返回?cái)?shù)據(jù)為字典取值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)
這篇文章主要介紹了python 如何實(shí)現(xiàn)圍棋游戲,幫助大家利用tkinter制作圖形界面程序,感興趣的朋友可以了解下2020-11-11Python基礎(chǔ)之函數(shù)嵌套知識(shí)總結(jié)
今天帶大家回顧python基礎(chǔ)知識(shí),文中對(duì)Python函數(shù)嵌套作了非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05