gpt4 book ai didi

python 插入Null值数据到Postgresql的操作

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python 插入Null值数据到Postgresql的操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

数据库中最好插入Null值.

在python中,暂时没找到通过sql语句的方式插入Null值.

推荐使用轮子的方法

?
1
2
3
4
5
6
7
8
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连接数据库插入数据库数据所碰到的坑 。

Python中插入数据时执行后,没有报任何错误,但数据库中并没有出现新添加的数据 。

原因:

缺少提交操作.

解决方案:

Python操作数据库时,如果对数据表进行修改/删除/添加等控制操作,系统会将操作保存在内存,只有执行commit(),才会将操作提交到数据库.

但是总有你想不到的坑代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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呀,怎么一直在数据库没有,再仔细看看 。

?
1
2
3
4
5
try :
  self .connection()[ 1 ].execute(sql)
  self .connection()[ 0 ].commit()
except :
  self .connection()[ 0 ].rollback()

connection()调用方法方法返回的对象是同一个吗?

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

正确写法:

?
1
2
3
4
5
6
7
try :
  cons = self .connection()
  cons[ 1 ].execute(sql)
  cons[ 0 ].commit()
  cons[ 0 ].close()
except :
  cons[ 0 ].rollback()

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我。如有错误或未考虑完全的地方,望不吝赐教.

原文链接:https://blog.csdn.net/a5685263/article/details/103242430 。

最后此篇关于python 插入Null值数据到Postgresql的操作的文章就讲到这里了,如果你想了解更多关于python 插入Null值数据到Postgresql的操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com