快速上手python 操作Postgresql

import psycopg2

conn=psycopg2.connect(database='postgres',user='postgres',password='1234',host='127.0.0.1',port='5432')

cur=conn.cursor()

#增

name="SSS"

age=23

cur.execute("insert into test_table(name,age) values(%s,%s)",(name,age))

cur.mommit()

#批量增

data=pd.DataFrame({'name':['xzi','xil','xiy'],

                  'age':[12,43,54]})

data=np.array(data) #转为list

sql="insert into test_table(name,age) values(%s,%s)"

cur.executemany(sql,data)

#改

name='xil'

cur.execute("update test_table set age=23 where name=%s",(name,))  #当个值时,括号后面要加逗号

#删

cur.execute('delete from test_table where age<=23')

#查

cur.execute('select * from test_table')

cur.fetchall() 获取全部

#cur.fetchone()

#建表

cursor.execute('''create table public.member(

id integer not null primary key,

name varchar(32) not null,

password varchar(32) not null,

singal varchar(128)

)''')

conn.commit()

conn.close()

#conn.rollback()

#conn.commit()

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容