国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中用psycopg2模塊操作PostgreSQL方法

Python中用psycopg2模塊操作PostgreSQL方法

2020-12-20 01:06pcent Python

python可以操作多種數據庫,本篇文章給大家介紹了用psycopg2模塊操作PostgreSQL方法,一起來學習下。

其實在Python中可以用來連接PostgreSQL的模塊很多,這里比較推薦psycopg2。psycopg2安裝起來非常的簡單(pip install psycopg2),這里主要重點介紹下如何使用。

安裝psycopg2模塊:

怎么驗證是否已經安裝過psycopy2?

Python中用psycopg2模塊操作PostgreSQL方法

編寫上面代碼,運行看是否拋出缺少psycopg2模塊。

Python中用psycopg2模塊操作PostgreSQL方法

安裝方法1:

1)使用psycopg2-2.4.2.win-amd64-py2.7-pg9.0.4-release.exe安裝,下載地址:http://vdisk.weibo.com/s/Cd8pPaw56Ozys

直接運行exe,不出錯誤,運行上邊代碼驗證代碼無錯誤,基本算是安裝完成了。

2)怎么卸載?

2.1)找到安裝目錄:C:\Python27,發現下邊包含文件:Removepsycopg2.exe,運行,來刪除;

2.2)如果運行失敗的話,進入目錄:C:\Python27\Lib\site-packages下,找到psycopg2文件夾和psycopg2-2.4.2-py2.7.egg-info文件,右鍵刪除。

2.3)運行上邊的代碼,確認是否刪除成功。

安裝方法2:

使用.whl安裝,下載地址:https://pypi.python.org/pypi/psycopg2/

Python中用psycopg2模塊操作PostgreSQL方法

下載文件:psycopg2-2.6.2-cp27-none-win_amd64.whl

我這里把psycopg2-2.6.2-cp27-none-win_amd64.whl拷貝到安裝目錄下Scripts文件夾中。

cmd中運行代碼:pip install C:\Python27\Scripts\psycopg2-2.6.2-cp27-none-win_amd64.whl

Python中用psycopg2模塊操作PostgreSQL方法

運行上邊的代碼,確認是否刪除成功。

通過psycopg2操作數據庫:

使用賬戶postgres,創建測試數據庫testdb。

Python中用psycopg2模塊操作PostgreSQL方法

參考yiibai.comAPI:

S.N. API & 描述

1 psycopg2.connect(database="testdb", user="postgres", password="cohondob", host="127.0.0.1", port="5432")

這個API打開一個連接到PostgreSQL數據庫。如果成功打開數據庫時,它返回一個連接對象。

2 connection.cursor()

該程序創建一個光標將用于整個數據庫使用Python編程。

3 cursor.execute(sql [, optional parameters])

此例程執行SQL語句。可被參數化的SQL語句(即占位符,而不是SQL文字)。 psycopg2的模塊支持占位符用%s標志

例如:cursor.execute("insert into people values (%s, %s)", (who, age))

4 curosr.executemany(sql, seq_of_parameters)

該程序執行SQL命令對所有參數序列或序列中的sql映射。

5 curosr.callproc(procname[, parameters])

這個程序執行的存儲數據庫程序給定的名稱。該程序預計為每一個參數,參數的順序必須包含一個條目。

6 cursor.rowcount

這個只讀屬性,它返回數據庫中的行的總數已修改,插入或刪除最后 execute*().

7 connection.commit()

此方法提交當前事務。如果不調用這個方法,無論做了什么修改,自從上次調用commit()是不可見的,從其他的數據庫連接。

8 connection.rollback()

此方法會回滾任何更改數據庫自上次調用commit()方法。

9 connection.close()

此方法關閉數據庫連接。請注意,這并不自動調用commit()。如果你只是關閉數據庫連接而不調用commit()方法首先,那么所有更改將會丟失!

10 cursor.fetchone()

這種方法提取的查詢結果集的下一行,返回一個序列,或者無當沒有更多的數據是可用的。

11 cursor.fetchmany([size=cursor.arraysize])

這個例程中取出下一個組的查詢結果的行數,返回一個列表。當沒有找到記錄,返回空列表。該方法試圖獲取盡可能多的行所顯示的大小參數。

12 cursor.fetchall()

這個例程獲取所有查詢結果(剩余)行,返回一個列表。空行時則返回空列表。

打開數據庫連接:

?
1
2
3
4
5
6
7
8
import os
import sys
import psycopg2
def connectPostgreSQL():
conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
print 'connect successful!'
if __name__=='__main__':
connectPostgreSQL()

創建表操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import sys
import psycopg2
def connectPostgreSQL():
conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
print 'connect successful!'
cursor=conn.cursor()
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()
print 'table public.member is created!'
if __name__=='__main__':
connectPostgreSQL()

Insert 操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
 import sys
 import psycopg2
 
 def connectPostgreSQL():
   conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
   print 'connect successful!'
   cursor=conn.cursor()
   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()
   print 'table public.member is created!'
 def insertOperate():
   conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
   cursor=conn.cursor()
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(1,'member0','password0','signal0')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(2,'member1','password1','signal1')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(3,'member2','password2','signal2')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(4,'member3','password3','signal3')")
   conn.commit()
   conn.close()
   
   print 'insert records into public.memmber successfully'
   
 if __name__=='__main__':
   #connectPostgreSQL()
insertOperate()

Select 操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
 import sys
 import psycopg2
 
 def connectPostgreSQL():
   conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
   print 'connect successful!'
   cursor=conn.cursor()
   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()
   print 'table public.member is created!'
 
 def insertOperate():
   conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
   cursor=conn.cursor()
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(1,'member0','password0','signal0')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(2,'member1','password1','signal1')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(3,'member2','password2','signal2')")
   cursor.execute("insert into public.member(id,name,password,singal)\
 values(4,'member3','password3','signal3')")
   conn.commit()
   conn.close()
   
   print 'insert records into public.memmber successfully'
 
 def selectOperate():
   conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
   cursor=conn.cursor()
   cursor.execute("select id,name,password,singal from public.member where id>2")
   rows=cursor.fetchall()
   for row in rows:
     print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
   conn.close()
   
 if __name__=='__main__':
   #connectPostgreSQL()
   #insertOperate()
   selectOperate()

結果:

?
1
2
3
4
5
6
7
8
9
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========
id= 3 ,name= member2 ,pwd= password2 ,singal= signal2
 
id= 4 ,name= member3 ,pwd= password3 ,singal= signal3
 
>>>

update操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import sys
import psycopg2
 
def connectPostgreSQL():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  print 'connect successful!'
  cursor=conn.cursor()
  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()
  print 'table public.member is created!'
 
def insertOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("insert into public.member(id,name,password,singal)\
values(1,'member0','password0','signal0')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(2,'member1','password1','signal1')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(3,'member2','password2','signal2')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(4,'member3','password3','signal3')")
  conn.commit()
  conn.close()
  
  print 'insert records into public.memmber successfully'
 
def selectOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("select id,name,password,singal from public.member where id>2")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
  conn.close()
 
def updateOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("update public.member set name='update ...' where id=2")
  conn.commit()
  print "Total number of rows updated :", cursor.rowcount
 
  cursor.execute("select id,name,password,singal from public.member")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
  conn.close()
  
if __name__=='__main__':
  #connectPostgreSQL()
  #insertOperate()
  #selectOperate()
  updateOperate()

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========
Total number of rows updated : 1
id= 1 ,name= member0 ,pwd= password0 ,singal= signal0
 
id= 3 ,name= member2 ,pwd= password2 ,singal= signal2
 
id= 4 ,name= member3 ,pwd= password3 ,singal= signal3
 
id= 2 ,name= update ... ,pwd= password1 ,singal= signal1
 
>>>

Delete操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import sys
import psycopg2
 
def connectPostgreSQL():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  print 'connect successful!'
  cursor=conn.cursor()
  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()
  print 'table public.member is created!'
 
def insertOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("insert into public.member(id,name,password,singal)\
values(1,'member0','password0','signal0')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(2,'member1','password1','signal1')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(3,'member2','password2','signal2')")
  cursor.execute("insert into public.member(id,name,password,singal)\
values(4,'member3','password3','signal3')")
  conn.commit()
  conn.close()
  
  print 'insert records into public.memmber successfully'
 
def selectOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("select id,name,password,singal from public.member where id>2")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
  conn.close()
 
def updateOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432")
  cursor=conn.cursor()
  cursor.execute("update public.member set name='update ...' where id=2")
  conn.commit()
  print "Total number of rows updated :", cursor.rowcount
 
  cursor.execute("select id,name,password,singal from public.member")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
  conn.close()
 
def deleteOperate():
  conn = psycopg2.connect(database="testdb", user="postgres", password="new.1234", host="127.0.0.1", port="5432") 
  cursor=conn.cursor()
 
  cursor.execute("select id,name,password,singal from public.member")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
 
  print 'begin delete'
  cursor.execute("delete from public.member where id=2")
  conn.commit() 
  print 'end delete'
  print "Total number of rows deleted :", cursor.rowcount
  
  cursor.execute("select id,name,password,singal from public.member")
  rows=cursor.fetchall()
  for row in rows:
    print 'id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n'
  conn.close()
  
if __name__=='__main__':
  #connectPostgreSQL()
  #insertOperate()
  #selectOperate()
  #updateOperate()
  deleteOperate()

結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
========== RESTART: C:\Users\Administrator\Desktop\mutilpleTest.py ==========
id= 1 ,name= member0 ,pwd= password0 ,singal= signal0
 
id= 3 ,name= member2 ,pwd= password2 ,singal= signal2
 
id= 4 ,name= member3 ,pwd= password3 ,singal= signal3
 
id= 2 ,name= update ... ,pwd= password1 ,singal= signal1
 
begin delete
end delete
Total number of rows deleted : 1
id= 1 ,name= member0 ,pwd= password0 ,singal= signal0
 
id= 3 ,name= member2 ,pwd= password2 ,singal= signal2
 
id= 4 ,name= member3 ,pwd= password3 ,singal= signal3
 
>>>

Python中用psycopg2模塊操作PostgreSQL方法

 

原文鏈接:http://blog.csdn.net/pcent/article/details/78643611

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: www.天天操 | 在线黄av | 日韩av免费在线观看 | 国产免费网址 | 超碰8| 中文字幕一区二区三区日韩精品 | 91午夜理伦私人影院 | 中文字幕一区二区三区乱码在线 | 欧美激情综合五月色丁香小说 | 欧美精品久久久 | 午夜影院免费 | 亚洲国产精品一区二区三区 | 91欧美激情一区二区三区成人 | 日韩欧美大片在线观看 | 国产乱码精品一区二区三区中文 | 精品成人在线 | 在线视频亚洲 | 99久久久国产精品 | 国产在线观看一区 | 亚洲一区二区中文字幕 | 黄色的视频免费看 | 精品国产一区二区三区久久 | 午夜电影网站 | 久久国产精品一区 | 日日爱视频 | 久久久久国产精品免费免费搜索 | av午夜电影 | 精品成人久久 | 黄色电影在线免费观看 | 久久精品不卡 | 一区二区在线不卡 | 亚洲乱码国产乱码精品精98午夜 | 欧美久久久久久久久久 | 伊人久久艹 | 久久久久无码国产精品一区 | 国产一区中文字幕 | 国产99在线| 亚洲欧美日韩成人 | 一级一片免费看 | 国产成人欧美一区二区三区的 | 国产一区二区三区在线 |