䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL誂毖Python

PostgreSQL誂毖Python

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

垬輙

PostgreSQL埇傖䫘Python psycopg2昇庖镖潊㔗 sycopg2滇Python䚡䘋臺蘔䔇PostgreSQL昄扞康䔇锗陉単㔗 噽䘋废傼乕儏锘庥媆䘿垔㔗婉驔襕剘䋸垬輙認婻昇庖啹婺垄邻螴愙喕婋赆誊冔锟五Python䬽橸婘2.5.x婔蕙䔇㔗套悩婉檪垄垬輙婘橺単婪䇽劯埇傖嘪䫘yum变傴垬輙垄套婋欔䴺

gitbook.net

$yum install python-psycopg2
 www.gitbook.net 

襕嘪䫘psycopg2䔇昇庖饡噽媙釂录傺婔婻Connection凹茇垄臘䴺昄扞康䇽劯喉埇傖锬拷录傺橩湺凹茇認儖婞媷欓臯䔇欔橬SQL臺埖㔗 www.gitbook.net

Python psycopg2 昇庖APIs

傖婋滇psycopg2䔇麉襕䔇䔇昇庖冋䘋埇傖悇轿Python䘋废婯PostgreSQL昄扞康䔇噖嘩㔗套悩凂欆婔婻敘崉溗䔇庫䫘䘋废闼幽埇傖䩋䩋Python psycopg2䔇昇庖䔇垻桹桺懼㔗 www.gitbook.net

S.N. API & 柟誄
1 psycopg2.connect(database="testdb", user="postgres", password="cohondob", host="127.0.0.1", port="5432")

gitbook.net


認婻API欷嚔婔婻誂毖彄PostgreSQL昄扞康㔗套悩潊媘欷嚔昄扞康施垄誫啂婔婻誂毖凹茇㔗 www.gitbook.net

2 connection.cursor()

臖䘋废录傺婔婻噬湺儖䫘庯昘婻昄扞康嘪䫘Python䚡䘋㔗 gitbook.net

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

溴冋䘋欓臯SQL臺埖㔗埇赆埗昄寡䔇SQL臺埖剿剹嘉严蔯婉滇SQL桺庖㔗 psycopg2䔇昇庖櫇毕剹嘉严䫘s湺媖 gitbook.net

冋套cursor.execute("insert into people values (%s, %s)", (who, age))

gitbook.net

4 curosr.executemany(sql, seq_of_parameters)

臖䘋废欓臯SQL变傴凹欔橬埗昄废彖潡废彖婺䔇sql滹儇㔗 www.gitbook.net

5 curosr.callproc(procname[, parameters])

認婻䘋废欓臯䔇庻嗘昄扞康䘋废䂍垔䔇劉䓄㔗臖䘋废鵇螇婺懟婔婻埗昄埗昄䔇釺废媙釂寙劆婔婻溇䕞㔗

www.gitbook.net

6 cursor.rowcount

認婻埻臂匂攓垄誫啂昄扞康婺䔇臯䔇攂昄噾媞櫹某噖潡役鍴橔劯 execute*().

gitbook.net

7 connection.commit()

溴桹濘柊庴嘷嬉庋媇㔗套悩婉脄䫘認婻桹濘方螺啔庖傔幽媞櫹躻傯婪渇脄䫘commit()滇婉埇蓕䔇傯噽傡䔇昄扞康誂毖㔗

gitbook.net

8 connection.rollback()

溴桹濘嚔啂悔傂嘘敘櫹昄扞康躻婪渇脄䫘commit桹濘㔗

www.gitbook.net

9 connection.close()

溴桹濘噿閺昄扞康誂毖㔗臙濘懟認幽婉躻媘脄䫘commit㔗套悩嘹埻滇噿閺昄扞康誂毖蔯婉脄䫘commit桹濘饡噽闼幽欔橬敘櫹儖嚔婵崌 www.gitbook.net

10 cursor.fetchone()

認䓉桹濘柊埡䔇昖臵䂷悩镖䔇婋婔臯誫啂婔婻废彖潡蔙方嘷澇橬敘崔䔇昄扞滇埇䫘䔇㔗

www.gitbook.net

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

認婻冋䘋婺埡庺婋婔婻䂇䔇昖臵䂷悩䔇臯昄誫啂婔婻彖臘㔗嘷澇橬欆彄螄嘘誫啂䷺彖臘㔗臖桹濘臘商诙埡儘埇脘崔䔇臯欔滆䴺䔇崓償埗昄㔗

gitbook.net

12 cursor.fetchall()

認婻冋䘋诙埡欔橬昖臵䂷悩嬷嘍臯誫啂婔婻彖臘㔗䷺臯施彍誫啂䷺彖臘㔗 www.gitbook.net

誂毖彄昄扞康

Python傼乕滆䴺庖套嘘誂毖彄婔婻䯄橬䔇昄扞康㔗套悩昄扞康婉庻婘闼幽垄儌嚔赆录傺橔䂽儖誫啂婔婻昄扞康凹茇㔗 gitbook.net

#!/usr/bin/python

import psycopg2

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

print "Opened database successfully" www.gitbook.net 

婘認麯幘埇傖柊冕昄扞康testdb䔇劉䓄套悩昄扞康潊媘欷嚔闼幽垄嚔䂍婋麵䔇潽敇 www.gitbook.net

Open database successfully
 
gitbook.net

录傺臘

傖婋Python䘋废儖嘪䫘傖嬉录傺䔇昄扞康婺录傺婔婻臘

www.gitbook.net

#!/usr/bin/python

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()
cur.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print "Table created successfully"

conn.commit()
conn.close() 
www.gitbook.net

婪誄䘋废欓臯施垄嚔录傺臘COMPANY 婘昄扞康test.db婺垄嚔滆䴺傖婋潽敇

www.gitbook.net

Opened database successfully
Table created successfully
 
gitbook.net

INSERT 淉嘩

Python䘋废滆䴺庖潏傸套嘘录傺臘COMPANY 婘婪麵䔇冋床婺录傺臘婺䔇螄嘘

www.gitbook.net

#!/usr/bin/python

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()

cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (1, 'Paul', 32, 'California', 20000.00 )");

cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");

cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");

cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");

conn.commit()
print "Records created successfully";
conn.close() gitbook.net 

婪誄䘋废欓臯施垄嚔录傺COMPANY臘婺䔇螄嘘幽滆䴺傖婋婴臯

gitbook.net

Opened database successfully
Records created successfully
 gitbook.net 

SELECT 淉嘩

Python䘋废滆䴺套嘘诙埡幽滆䴺COMPANY 臘婘婪麵䔇冋床婺录傺䔇螄嘘 gitbook.net

#!/usr/bin/python

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()

cur.execute("SELECT id, name, address, salary  from COMPANY")
rows = cur.fetchall()
for row in rows:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print "Operation done successfully";
conn.close() gitbook.net 

嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩

www.gitbook.net

Opened database successfully
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  20000.0

ID =  2
NAME =  Allen
ADDRESS =  Texas
SALARY =  15000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

Operation done successfully
 www.gitbook.net 

UPDATE 淉嘩

Python傼乕滆䴺套嘘潏傸埇傖嘪䫘UPDATE臺埖準敘桄螄嘘䇽劯傯COMPANY臘诙埡幽滆䴺敘桄䔇螄嘘 gitbook.net

#!/usr/bin/python

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()

cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit
print "Total number of rows updated :", cur.rowcount

cur.execute("SELECT id, name, address, salary  from COMPANY")
rows = cur.fetchall()
for row in rows:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print "Operation done successfully";
conn.close() 
gitbook.net

嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩

www.gitbook.net

Opened database successfully
Total number of rows updated : 1
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  25000.0

ID =  2
NAME =  Allen
ADDRESS =  Texas
SALARY =  15000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

Operation done successfully
 gitbook.net 

DELETE 淉嘩

Python傼乕滆䴺庖套嘘潏傸埇傖嘪䫘DELETE臺埖役鍴螄嘘䇽劯诙埡幽滆䴺COMPANY 臘嬷嘍䔇螄嘘  gitbook.net

#!/usr/bin/python

import psycopg2

conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print "Opened database successfully"

cur = conn.cursor()

cur.execute("DELETE from COMPANY where ID=2;")
conn.commit
print "Total number of rows deleted :", cur.rowcount

cur.execute("SELECT id, name, address, salary  from COMPANY")
rows = cur.fetchall()
for row in rows:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print "Operation done successfully";
conn.close() 
gitbook.net

嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩

gitbook.net

Opened database successfully
Total number of rows deleted : 1
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  20000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

Operation done successfully www.gitbook.net 
橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL誂毖Python
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080998.html
婪婔䇺PostgreSQL誂毖Perl      婋婔䇺澇橬庖