开发示例
更新时间:2026-06-24
Python
1#!/bin/python3
2
3import psycopg2
4
5# psycopg2常用链接方式
6#conn = psycopg2.connect(dbname="palopgsql", user="sg", password="111111", host="localhost", port=34200)
7#conn = psycopg2.connect("dbname=palopgsql user=sg password=111111 host=localhost port=34200")
8
9## 创建连接对象
10conn=psycopg2.connect(database="palopgsql",user="sg",password="111111",host="localhost",port=34200)
11cur=conn.cursor()
12
13## 创建表
14cur.execute("CREATE TABLE student(id integer,name varchar,sex varchar);")
15
16## 插入数据
17cur.execute("INSERT INTO student(id,name,sex) VALUES(%s,%s,%s)",(1,'Aspirin','M'))
18cur.execute("INSERT INTO student(id,name,sex) VALUES(%s,%s,%s)",(2,'Taxol','F'))
19cur.execute("INSERT INTO student(id,name,sex) VALUES(%s,%s,%s)",(3,'Dixheral','M'))
20
21## 获取结果
22cur.execute('SELECT * FROM student')
23results=cur.fetchall()
24print (results)
25
26## 关闭连接
27conn.commit()
28cur.close()
29conn.close()
评价此篇文章
