PSYCOPG Query
#Coding:utf-8ImportPsycopg2conn=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 () forRowinchrows:Print "Name:", Row[0]Print "Type:", row[1] Print "State :", row[2] Print "*****"Print "Records Select successfully"; Conn.close ()
PSYCOPG Insertion
#Coding:utf-8ImportPsycopg2conn=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 ' , ' Test group ', ' 1 ', current_timestamp)") Conn.commit ()Print "Records created successfully"; Conn.close ()
PSYCOPG modification
#Coding:utf-8ImportPsycopg2conn=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 group ' where Name= ' admin2 '") Conn.commit ()Print "total number of rows updated:", Cursor.rowcountcursor.execute ("Select Name,type,state from WY") Rows=Cursor.fetchall () forRowinchrows:Print "Name:", Row[0]Print "Type:", row[1]Print "State :", row[2]Print "*******"Print " Done"; Conn.close ()
PSYCOPG Delete
#Coding:utf-8ImportPsycopg2conn=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.rowcountcursor.execute ("Select Name,type,state from WY") Rows=Cursor.fetchall () forRowinchrows:Print "Name:", Row[0]Print "Type:", row[1] Print "State :", row[2] Print "*******"Print " Done"; Conn.close ()
PSYCOPG Operating Database