濮阳杆衣贸易有限公司

主頁 > 知識庫 > python 插入Null值數(shù)據(jù)到Postgresql的操作

python 插入Null值數(shù)據(jù)到Postgresql的操作

熱門標簽:400電話 申請 條件 電銷機器人 金倫通信 汕頭電商外呼系統(tǒng)供應商 crm電銷機器人 鄭州智能外呼系統(tǒng)中心 云南地圖標注 南京crm外呼系統(tǒng)排名 北京外呼電銷機器人招商 賓館能在百度地圖標注嗎

數(shù)據(jù)庫中最好插入Null值。

在python中,暫時沒找到通過sql語句的方式插入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

補充:python連接數(shù)據(jù)庫插入數(shù)據(jù)庫數(shù)據(jù)所碰到的坑

Python中插入數(shù)據(jù)時執(zhí)行后,沒有報任何錯誤,但數(shù)據(jù)庫中并沒有出現(xiàn)新添加的數(shù)據(jù)

原因:

缺少提交操作。

解決方案:

Python操作數(shù)據(jù)庫時,如果對數(shù)據(jù)表進行修改/刪除/添加等控制操作,系統(tǒng)會將操作保存在內存,只有執(zhí)行commit(),才會將操作提交到數(shù)據(jù)庫。

但是總有你想不到的坑代碼如下:

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 '學生ID',
     `content` varchar(1024) DEFAULT NULL COMMENT '學員內容',
     `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 = '學員表'"""
  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ù)庫沒有,再仔細看看

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

connection()調用方法方法返回的對象是同一個嗎?

并不是,心累,搞了半天,只怪自己還太嫩。

正確寫法:

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

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 利用python為PostgreSQL的表自動添加分區(qū)
  • Python操作PostgreSql數(shù)據(jù)庫的方法(基本的增刪改查)
  • Python 操作 PostgreSQL 數(shù)據(jù)庫示例【連接、增刪改查等】
  • python連接PostgreSQL過程解析
  • python 兩個數(shù)據(jù)庫postgresql對比
  • Python 中創(chuàng)建 PostgreSQL 數(shù)據(jù)庫連接池

標簽:西寧 懷化 浙江 石家莊 昆明 錫林郭勒盟 梅州 文山

巨人網(wǎng)絡通訊聲明:本文標題《python 插入Null值數(shù)據(jù)到Postgresql的操作》,本文關鍵詞  python,插入,Null,值,數(shù)據(jù),;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python 插入Null值數(shù)據(jù)到Postgresql的操作》相關的同類信息!
  • 本頁收集關于python 插入Null值數(shù)據(jù)到Postgresql的操作的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    昭通市| 山东省| 定兴县| 新疆| 石台县| 理塘县| 钦州市| 武义县| 新巴尔虎右旗| 舟山市| 浦东新区| 湟中县| 绩溪县| 武鸣县| 柘荣县| 叶城县| 洪泽县| 德昌县| 察哈| 绥滨县| 奉新县| 武宁县| 三明市| 门源| 南投市| 区。| 固原市| 西青区| 阿巴嘎旗| 饶河县| 金塔县| 江口县| 安庆市| 夏河县| 广宁县| 辛集市| 怀宁县| 台江县| 静乐县| 大庆市| 于都县|