码迷,mamicode.com
首页 > 数据库 > 详细

psycopg操作数据库

时间:2016-08-12 15:11:35      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

psycopg查询

#coding:utf-8
import psycopg2
conn=psycopg2.connect(host=127.0.0.1,port=5432,user=postgres,password=1,database=postgis)
print "opened db successfully"
cursor=conn.cursor()
cursor.execute("select name,type,state from wy")
rows=cursor.fetchall()
for row in rows:
   print "name:",row[0]
   print "type:",row[1]
   print "state:",row[2]
   print "*****"
print "Records select successfully";
conn.close()

psycopg插入

#coding:utf-8
import psycopg2
conn=psycopg2.connect(host=127.0.0.1,port=5432,user=postgres,password=1‘’,database=postgis)
print "opened db successfully"
cursor=conn.cursor()
cursor.execute("insert into wy(mid,name,psw,type,state,creat_time) values(‘7762cc64-5de1-11e6-ba75-000c2923b59f‘,‘admin2‘,‘1‘,‘测试组‘,‘1‘,CURRENT_TIMESTAMP)")
conn.commit()
print "Records created successfully";
conn.close()

psycopg修改

#coding:utf-8
import psycopg2
conn=psycopg2.connect(host=127.0.0.1,port=5432,user=postgres,password=‘1,database=postgis)
print "opened db successfully"
cursor=conn.cursor()
cursor.execute("update wy set type=‘UI组‘ where name=‘admin2‘")
conn.commit()
print "Total number of rows updated:",cursor.rowcount
cursor.execute("select name,type,state from wy")
rows=cursor.fetchall()
for row in rows:
print "name:",row[0]
print "type:",row[1]
print "state:",row[2]
print "*******"
print "done";
conn.close()

 

psycopg删除

#coding:utf-8
import psycopg2
conn=psycopg2.connect(host=127。0.0.1,port=5432,user=postgres,password=1,database=postgis)
print "opened db successfully"
cursor=conn.cursor()
cursor.execute("delete from wy where name=‘admin2‘")
conn.commit()
print "Total number of rows updated:",cursor.rowcount
cursor.execute("select name,type,state from wy")
rows=cursor.fetchall()
for row in rows:
   print "name:",row[0]
   print "type:",row[1]
   print "state:",row[2]
   print "*******"
print "done";
conn.close()   

 

psycopg操作数据库

标签:

原文地址:http://www.cnblogs.com/shijingjing07/p/5764784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!