做二手房网站有哪些,网站开发工程师制作kpi,最高法律网站是做啥的,建设企业网站得花多少钱目录
一、基础环境
#xff08;一#xff09;建立虚拟环境
#xff08;二#xff09;安装依赖
二、连接数据库
三、操作数据库
#xff08;一#xff09;查询
#xff08;二#xff09;单笔更新/插入
#xff08;三#xff09;批量更新/插入
#xff08;四…目录
一、基础环境
一建立虚拟环境
二安装依赖
二、连接数据库
三、操作数据库
一查询
二单笔更新/插入
三批量更新/插入
四遇到的问题
1. SQL语句 - 栏位名报错
1具体问题
2解决
2. SQL语句 - 表不存在
1具体问题
2解决
3. SQL语句 - 插入的数据格式不正确
1具体问题
2解决 一、基础环境
一建立虚拟环境 Python 3.10.7
二安装依赖 psycopg22.9.9 (在虚拟环境中执行安装)
二、连接数据库
import psycopg2
from psycopg2.extras import execute_values
class ExcuteDB:# ————————————————————————————————————————————— 连接数据库def __init__(self, host, dbname, user, port, password):self.host hostself.dbname dbnameself.user userself.port portself.password password# self.conn self.__postgres # 连接数据库放在各种功能方法中比如exc_query方法propertydef __postgres(self):conn psycopg2.connect(hostself.host, # 主机地址dbnameself.dbname, # 数据库名称userself.user, # 用户名passwordself.password, # 密码portself.port) # 端口号return conn# ————————————————————————————————————————————— 查询def exc_query(self, sql):try:conn self.__postgres # 连接数据库cur conn.cursor() # 创建游标对象cur.execute(sql) # 执行SQL语句res_list Noneres_list cur.fetchall() # 获取记录值结果是列表包元组:[(1F002, 数据结构怎么学), (1F001, 数据库原理)]cols_list [row[0] for row in cur.description] # 获取栏位名称[card, name]data_list [dict(map(lambda key, value: [key, value], cols_list, val_list)) for val_list in res_list] # 组装列表包字典data_list [{card: 1F002, name: 数据结构怎么学}, {card: 1F001, name: 数据库原理}]except Exception as e:print(str(e))finally:cur.close() # 关闭游标conn.close() # 关闭数据库连接return {栏位名栏位值: data_list, 只有栏位值: res_list}# ————————————————————————————————————————————— 单笔更新/插入def exc_edit(self, sql):try:conn self.__postgres # 连接数据库cur conn.cursor() # 创建游标对象cur.execute(sql) # 执行SQL语句conn.commit() # 提交事务except Exception as e:conn.rollback() # 回滚commit事务print(str(e))finally:cur.close() # 关闭游标conn.close() # 关闭数据库连接# ————————————————————————————————————————————— 批量更新/插入def exc_bulk_edit(self, sql_list):try:conn self.__postgres # 连接数据库cur conn.cursor() # 创建游标对象for l in sql_list: # 循环不同的sqlsql l[sql] # sql语句insert into auth_group (card, name, author, btype, price, num) values %s args_list l[args_list] # 批量插入的值[(2F0001, 高等数学, 小星星, 教育, 35, 10), (2F0002, 线性代数, 小星星, 教育, 40, 12)]execute_values(cur, sql, args_list) # 批量执行conn.commit() # 提交事务except Exception as e:conn.rollback() # 回滚commit事务print(str(e))finally:cur.close() # 关闭游标conn.close() # 关闭数据库连接三、操作数据库
一查询
exc_db ExcuteDB(XX.XX.XX.XX, bookDB, admin, 5432, 123) # 连接数据库
res exc_db.exc_query(select card, name from myApp_book limit 10) # 执行查询
二单笔更新/插入
exc_db ExcuteDB(XX.XX.XX.XX, bookDB, admin, 5432, 123) # 连接数据库
res2 exc_db.exc_edit(update myApp_book set name\数据结构\ where card\1F002\ ) # 执行更新
三批量更新/插入
exc_db ExcuteDB(XX.XX.XX.XX, bookDB, admin, 5432, 123) # 连接数据库
res2 exc_db.exc_bulk_edit([{sql: insert into auth_group (name) values %s , args_list: [(2F0001,), (2F0002,)]}, # 批量插入auth_group表{sql: insert into myApp_book (card, name, author, btype, price, num) values %s , args_list: [(2F0001, 高等数学, 小星星, 教育, 35, 10), (2F0002, 线性代数, 小星星, 教育, 40, 12)]} # 批量插入myApp_book表])
四遇到的问题
1. SQL语句 - 栏位名报错
1具体问题
column 数据结构 does not exist
LINE 1: update myApp_book set name数据结构 where card1F002
2解决 数据值不能用双引号括起来而是用 \。 错误写法
exc_db.exc_query(update myApp_book set name数据结构 where card\1F002\ ) 正确写法
exc_db.exc_query(update myApp_book set name\数据结构\ where card\1F002\ )
2. SQL语句 - 表不存在
1具体问题
relation myapp_book does not exist
2解决 表名要用双引号包裹。 正确写法
insert into myApp_book … 错误写法
insert into myApp_book …
3. SQL语句 - 插入的数据格式不正确
1具体问题
INSERT has more expressions than target columns
LINE 1: insert into auth_group (name) values (2,F,0,0,0,1…
2解决 元组里只有一个数据时得用逗号结尾。 正确写法
[(2F0001,), (2F0002,)] # 列表包元组 错误写法
[(2F0001), (2F0002)] # 会自动转变为[2F0001,2F0002]