在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫/ Python連接PostgreSQL數(shù)據(jù)庫
PostgreSQL LIKE條件
PostgreSQL截斷表(TRUNCATE TABLE語句)
C/C++連接PostgreSQL數(shù)據(jù)庫
PostgreSQL別名
PostgreSQL日期和時間函數(shù)
PostgreSQL NOT IN條件
PostgreSQL歷史
PostgreSQL事務(wù)
PostgreSQL AND & OR條件
PostgreSQL NULL值
PostgreSQL教程
PostgreSQL更新數(shù)據(jù)(UPDATE語句)
PostgreSQL ORDER BY子句
PostgreSQL分組(GROUP BY子句)
PostgreSQL數(shù)據(jù)類型
PostgreSQL特點
PostgreSQL刪除數(shù)據(jù)庫
PostgreSQL UNIONS子句
PostgreSQL AND條件
PostgreSQL索引
PostgreSQL刪除表
Perl連接PostgreSQL數(shù)據(jù)庫
PostgreSQL視圖
PostgreSQL修改表(ALTER TABLE語句)
PostgreSQL全外連接
PostgreSQL命令語法大全
PostgreSQL查詢數(shù)據(jù)(SELECT語句)
PostgreSQL自動遞增
PostgreSQL左外連接
PostgreSQL創(chuàng)建表
PostgreSQL模式(架構(gòu))
PostgreSQL觸發(fā)器
PostgreSQL安裝(Windows)
PostgreSQL NOT條件
PostgreSQL Having子句
PostgreSQL函數(shù)(存儲過程)
Java連接PostgreSQL數(shù)據(jù)庫
PostgreSQL權(quán)限
PostgreSQL OR條件
PostgreSQL創(chuàng)建數(shù)據(jù)庫
PostgreSQL BETWEEN條件
PostgreSQL IN條件
Python連接PostgreSQL數(shù)據(jù)庫
PostgreSQL刪除數(shù)據(jù)(DELETE語句)
PostgreSQL子查詢
PostgreSQL右外連接
PostgreSQL插入數(shù)據(jù)(INSERT語句)
PostgreSQL是什么?
PostgreSQL連接(內(nèi)連接)
PHP連接PostgreSQL數(shù)據(jù)庫
PostgreSQL條件查詢
PostgreSQL鎖
PostgreSQL跨連接(CROSS JOIN)

Python連接PostgreSQL數(shù)據(jù)庫

PostgreSQL可以使用psycopg2模塊與Python集成。sycopg2是用于Python編程語言的PostgreSQL數(shù)據(jù)庫適配器。 psycopg2是非常小,快速,穩(wěn)定的。 您不需要單獨安裝此模塊,因為默認(rèn)情況下它會隨著Python 2.5.x版本一起發(fā)布。 如果還沒有在您的機器上安裝它,那么可以使用yum命令安裝它,如下所示:

$yum install python-psycopg2

要使用psycopg2模塊,必須首先創(chuàng)建一個表示數(shù)據(jù)庫的Connection對象,然后可以選擇創(chuàng)建可以幫助您執(zhí)行所有SQL語句的游標(biāo)對象。

連接到數(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"

在這里指定使用testdb作為數(shù)據(jù)庫名稱,如果數(shù)據(jù)庫已成功打開連接,則會提供以下消息:

Open database successfully

創(chuàng)建表

以下Python程序?qū)⒂糜谠谙惹皠?chuàng)建的數(shù)據(jù)庫(testdb)中創(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()

當(dāng)執(zhí)行上述程序時,它將在數(shù)據(jù)庫testdb中創(chuàng)建COMPANY表,并顯示以下消息:

Opened database successfully
Table created successfully

插入操作

以下Python程序顯示了如何在上述示例中創(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()

當(dāng)執(zhí)行上述程序時,它將在COMPANY表中創(chuàng)建/插入給定的記錄,并顯示以下兩行:

Opened database successfully
Records created successfully

SELECT操作

以下Python程序顯示了如何從上述示例中創(chuàng)建的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("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()

執(zhí)行上述程序時,會產(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

更新操作

以下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()

執(zhí)行上述程序時,會產(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

刪除操作

以下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()

執(zhí)行上述程序時,會產(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