PostgreSQL連接Python
安裝
PostgreSQL可以用Python psycopg2模塊集成。 sycopg2是Python編程語言的PostgreSQL數(shù)據(jù)庫的適配器。 其程序代碼少,速度快,穩(wěn)定。不需要單獨(dú)安裝這個模塊,因?yàn)樗J(rèn)情況下被運(yùn)往隨著Python版本在2.5.x一起的。如果不把它安裝在機(jī)器上,然后可 以使用yum命令安裝它,如下所示:
- $yum install python-psycopg2
要使用psycopg2的模塊,首先必須創(chuàng)建一個Connection對象,它表示數(shù)據(jù)庫然后再可以選擇創(chuàng)建游標(biāo)對象,這將幫助執(zhí)行的所有SQL語句。
Python psycopg2 模塊APIs
以下是psycopg2的重要的的模塊例程可以滿足Python程序與PostgreSQL數(shù)據(jù)庫的工作。如果尋找一個更復(fù)雜的應(yīng)用程序,那么可以看看Python psycopg2的模塊的官方文檔。
連接到數(shù)據(jù)庫
Python代碼顯示了如何連接到一個現(xiàn)有的數(shù)據(jù)庫。如果數(shù)據(jù)庫不存在,那么它就會被創(chuàng)建,最終將返回一個數(shù)據(jù)庫對象。
- #!/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"
在這里,也可以提供數(shù)據(jù)庫testdb的名稱,如果數(shù)據(jù)庫成功打開,那么它會給下面的消息:
- Open database successfully
創(chuàng)建表
以下Python程序?qū)⑹褂靡郧皠?chuàng)建的數(shù)據(jù)庫中創(chuàng)建一個表:
- #!/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()
上述程序執(zhí)行時(shí),它會創(chuàng)建表COMPANY 在數(shù)據(jù)庫test.db中,它會顯示以下消息:
- Opened database successfully
- Table created successfully
#p#
INSERT 操作
Python程序顯示了我們?nèi)绾蝿?chuàng)建表COMPANY 在上面的例子中創(chuàng)建表中的記錄:
- #!/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()
上述程序執(zhí)行時(shí),它會創(chuàng)建COMPANY表中的記錄,并顯示以下兩行:
- Opened database successfully
- Records created successfully
SELECT 操作
Python程序,顯示如何獲取并顯示COMPANY 表在上面的例子中創(chuàng)建的記錄:
- #!/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()
當(dāng)上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
- 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
UPDATE 操作
Python代碼顯示如何,我們可以使用UPDATE語句來更新記錄,然后從COMPANY表獲取并顯示更新的記錄:
- #!/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()
當(dāng)上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
- 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
#p#
DELETE 操作
Python代碼顯示了如何我們可以使用DELETE語句刪除記錄,然后獲取并顯示COMPANY 表剩余的記錄:
- #!/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()
當(dāng)上述程序執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:
- 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
原文鏈接:http://www.yiibai.com/html/postgresql/2013/080998.html