pg使用python编写存储过程_Python操作PostgreSql数据库的方法(基本的增删改查)

Python操作PostgreSql数据库(基本的增删改查)

操作数据库最快的方式当然是直接用使用SQL语言直接对数据库进行操作,但是偶尔我们也会碰到在代码中操作数据库的情况,我们可能用ORM类的库对数控库进行操作,但是当需要操作大量的数据时,ORM的数据显的太慢了。在python中,遇到这样的情况,我推荐使用psycopg2操作postgresql数据库

psycopg2

官方文档传送门: http://initd.org/psycopg/docs/index.html

简单的增删改查

连接

连接pg并创建表

PG_SQL_LOCAL = {

'database': 'postgres',

'user': 'postgres',

'password': "8dsa581",

# 'host':'10.27.78.1',

'host': 'localhost'

}

def connectPostgreSQL():

conn = psycopg2.connect(**PG_SQL_LOCAL)

print('connect successful!')

cursor = conn.cursor()

cursor.execute('''

create table public.members(

id integer not null primary key,

name varchar(32) not

你可能感兴趣的:(pg使用python编写存储过程_Python操作PostgreSql数据库的方法(基本的增删改查))