PostgreSQL是一個輕量級的數據庫,用python操做時,用到了psycopg2模塊,其程序代碼少,速度快,並且穩定。html
參考:http://www.yiibai.com/html/postgresql/2013/080998.htmlpython
環境sql
ubuntu 16.04數據庫
python2編程
安裝ubuntu
1. 安裝postgresqlyii
sudo apt-get install postgresqlpost
(下面2步操做應該能夠在python代碼中實現)fetch
=》 在數據庫中建立userspa
sudo -u postgres createuser -P uuser, with password ppas
=》 建立數據庫DB
sudo -u postgres createdb -O uuser testdb
2. 安裝python psycopg2模塊
sudo apt-get install python-psycopg2
後面若是須要刪除數據庫,能夠執行:
sudo -u postgres dropdb testdb
python pscopg2 模塊中的APIs
SN | API &描述 |
1 | psycopg2.connect(database="testdb", user="uuser", password="ppas", host="127.0.0.1", port="5432") 這個API打開一個鏈接到PostgreSQL數據庫。若是成功打開數據庫時,它返回一個鏈接對象。 (默認狀況這裏能夠不用port這個參數) |
2 | connection.cursor() 該程序建立一個光標將用於整個數據庫使用Python編程。 |
3 | cursor.execute(sql [, optional parameters]) 此例程執行SQL語句。可被參數化的SQL語句(即佔位符,而不是SQL文字)。 psycopg2的模塊支持佔位符用%s標誌 yiibai.com 例如:cursor.execute("insert into people values (%s, %s)", (who, age)) |
4 | curosr.executemany(sql, seq_of_parameters) 該程序執行SQL命令對全部參數序列或序列中的sql映射。 |
5 | curosr.callproc(procname[, parameters]) 這個程序執行的存儲數據庫程序給定的名稱。該程序預計爲每個參數,參數的順序必須包含一個條目。 |
6 | cursor.rowcount 這個只讀屬性,它返回數據庫中的行的總數已修改,插入或刪除最後 execute*(). |
7 | connection.commit() 此方法提交當前事務。若是不調用這個方法,不管作了什麼修改,自從上次調用commit()是不可見的,從其餘的數據庫鏈接。 |
8 | connection.rollback() 此方法會回滾任何更改數據庫自上次調用commit()方法。 |
9 | connection.close() 此方法關閉數據庫鏈接。請注意,這並不自動調用commit()。若是你只是關閉數據庫鏈接而不調用commit()方法首先,那麼全部更改將會丟失 |
10 | cursor.fetchone() 這種方法提取的查詢結果集的下一行,返回一個序列,或者無當沒有更多的數據是可用的 |
11 | cursor.fetchmany([size=cursor.arraysize]) 這個例程中取出下一個組的查詢結果的行數,返回一個列表。當沒有找到記錄,返回空列表。該方法試圖獲取儘量多的行所顯示的大小參數。 |
12 | cursor.fetchall() 這個例程獲取全部查詢結果(剩餘)行,返回一個列表。空行時則返回空列表 |
鏈接到數據庫
python對數據庫的操做須要先鏈接到數據庫,若是該數據庫不存在,那麼它就會被建立,最終返回一個數據庫對象。
#!/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="uuser",password="ppas", host="127.0.0.1") print "connect success"
建立表
#!/bin/python import psycopg2 conn = psycopg2.connect(database = "testdb", user = "uuer", password = "ppas", host = "127.0.0.1") print "Open database success" 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)''' ) print "Table created successfully" conn.commit() conn.close()
這裏若是想讓ID爲主鍵,而且自增,能夠如此定義: ID SERIAL PRIMARY KEY
insert操做
在上面建立的表中插入記錄:
#!/bin/python import psycopg2 conn = psycopg2.connect(database = "testdb", user = "uuer", password = "ppas", host = "127.0.0.1") print "Open database success" cur = conn.cursor() cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS) \ VALUES (1, 'Paul', 32, 'California')"); cur.execute("INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS) \ VALUES (4, 'Mark', 25, 'Rich-Mond')") conn.commit() print "insert succcess" conn.close()
selete操做
#!/bin/python import psycopg2 conn = psycopg2.connect(database = "testdb", user = "uuer", password = "ppas", host = "127.0.0.1") print "Open database success" cur = conn.cursor() cur.execute("SELECT id, name, address from COMPANY") rows = cur.fetchall() for row in rows: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "Operation done successfully"; conn.close()
update操做
#!/bin/python import psycopg2 conn = psycopg2.connect(database = "testdb", user = "uuer", password = "ppas", host = "127.0.0.1") print "Open database success" cur = conn.cursor() cur.execute("UPDATE COMPANY set NAME = "newCompy" where ID=1") conn.commit print "Total number of rows updated :", cur.rowcount cur.execute("SELECT id, name, address from COMPANY") rows = cur.fetchall() for row in rows: print "ID = ", row[0] print "NAME = ", row[1] print "ADDRESS = ", row[2] print "Operation done successfully"; conn.close()
delete操做
刪除一行
#!/bin/python import psycopg2 conn = psycopg2.connect(database = "testdb", user = "uuer", password = "ppas", host = "127.0.0.1") print "Open database success" 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 "Operation done successfully"; conn.close()