91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python如何操作PostgreSQL數據庫

發布時間:2020-08-01 13:45:11 來源:億速云 閱讀:290 作者:小豬 欄目:開發技術

小編這次要給大家分享的是Python如何操作PostgreSQL數據庫,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

我使用的是 Python 3.7.0

PostgreSQL可以使用psycopg2模塊與Python集成。

sycopg2是用于Python編程語言的PostgreSQL數據庫適配器。

psycopg2是非常小,快速,穩定的。 您不需要單獨安裝此模塊,因為默認情況下它會隨著Python 2.5.x版本一起發布。

pip3 install python-psycopg2
pip3 install psycopg2-binary

連接到數據庫

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

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

Open database successfully

創建表

以下Python程序將用于在先前創建的數據庫(testdb)中創建一個表:

#!/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()

當執行上述程序時,它將在數據庫testdb中創建COMPANY表,并顯示以下消息:

Opened database successfully
Table created successfully

插入操作

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

當執行上述程序時,它將在COMPANY表中創建/插入給定的記錄,并顯示以下兩行:

Opened database successfully
Records created successfully

SELECT操作

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

執行上述程序時,會產生以下結果:

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

Python

執行上述程序時,會產生以下結果:

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

執行上述程序時,會產生以下結果:

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

看完這篇關于Python如何操作PostgreSQL數據庫的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

瑞金市| 平南县| 垣曲县| 白玉县| 尼玛县| 宁乡县| 陆川县| 元朗区| 团风县| 溧阳市| 大城县| 当阳市| 疏勒县| 上高县| 永仁县| 洮南市| 嵊泗县| 犍为县| 新晃| 铁力市| 东方市| 伊金霍洛旗| 清新县| 甘德县| 庆安县| 南宫市| 海城市| 锡林郭勒盟| 苏尼特左旗| 建宁县| 富锦市| 宜川县| 定安县| 上高县| 花莲县| 庄浪县| 卫辉市| 塔河县| 重庆市| 余江县| 彭山县|