溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用python怎么往Postgresql數(shù)據(jù)庫中插入一個Null值

發(fā)布時間:2021-03-08 11:18:48 來源:億速云 閱讀:623 作者:Leah 欄目:開發(fā)技術(shù)

使用python怎么往Postgresql數(shù)據(jù)庫中插入一個Null值?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

python是什么意思

Python是一種跨平臺的、具有解釋性、編譯性、互動性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計是用于編寫自動化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。

使用輪子的方法

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ù)庫插入數(shù)據(jù)庫數(shù)據(jù)所碰到的坑

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

原因:

缺少提交操作。

解決方案:

Python操作數(shù)據(jù)庫時,如果對數(shù)據(jù)表進(jìn)行修改/刪除/添加等控制操作,系統(tǒng)會將操作保存在內(nèi)存,只有執(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 '學(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ù)庫沒有,再仔細(xì)看看

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

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

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

正確寫法:

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

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI