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
傖婋滇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 successfullygitbook.net
傖婋Python䘋废儖嘪䫘傖嬉录傺䔇昄扞康婺录傺婔婻臘
#!/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 successfullygitbook.net
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臘婺䔇螄嘘幽滆䴺傖婋婴臯
Opened database successfully Records created successfully gitbook.net
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
嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩
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
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
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
嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩
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