標籤:password sql語句 重要 cursor text logs value from optional
pip install psycopg
Python psycopg2 模組APIs
以下是psycopg2的重要的的模組常式可以滿足Python程式與PostgreSQL資料庫的工作。
S.N. |
API & 描述 |
1 |
psycopg2.connect(database="testdb", user="postgres", password="cohondob", host="127.0.0.1", port="5432")
這個API開啟一個串連到PostgreSQL資料庫。如果成功開啟資料庫時,它返回一個連線物件。 www.yiibai.com |
2 |
connection.cursor()
該程式建立一個游標將用於整個資料庫使用Python編程。 yiibai.com |
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映射。 www.yiibai.com |
5 |
curosr.callproc(procname[, parameters])
這個程式執行的儲存資料庫程式給定的名稱。該程式預計為每一個參數,參數的順序必須包含一個條目。 |
6 |
cursor.rowcount
這個唯讀屬性,它返回資料庫中的行的總數已修改,插入或刪除最後 execute*(). |
7 |
connection.commit()
此方法提交當前事務。如果不調用這個方法,無論做了什麼修改,自從上次調用commit()是不可見的,從其他的資料庫連接。 |
8 |
connection.rollback()
此方法會復原任何更改資料庫自上次調用commit()方法。 |
9 |
connection.close()
此方法關閉資料庫連接。請注意,這並不自動調用commit()。如果你只是關閉資料庫連接而不調用commit()方法首先,那麼所有更改將會丟失! www.yiibai.com |
10 |
cursor.fetchone()
這種方法提取的查詢結果集的下一行,返回一個序列,或者無當沒有更多的資料是可用的。 |
11 |
cursor.fetchmany([size=cursor.arraysize])
這個常式中取出下一個組的查詢結果的行數,返回一個列表。當沒有找到記錄,返回空列表。該方法試圖擷取儘可能多的行所顯示的大小參數。 |
12 |
cursor.fetchall()
這個常式擷取所有查詢結果(剩餘)行,返回一個列表。空行時則返回空列表。 www.yiibai.com |
串連到資料庫
Python代碼顯示了如何串連到一個現有的資料庫。如果資料庫不存在,那麼它就會被建立,最終將返回一個資料庫物件
#!/usr/bin/pythonimport psycopg2conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")print "Opened database successfully"
建立表
以下Python程式將使用以前建立的資料庫中建立一個表:
#!/usr/bin/pythonimport psycopg2conn = 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()
INSERT 操作
Python程式顯示了我們如何建立表COMPANY 在上面的例子中建立表中的記錄:
#!/usr/bin/pythonimport psycopg2conn = 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()
SELECT 操作
Python程式,顯示如何擷取並顯示COMPANY 表在上面的例子中建立的記錄:
#!/usr/bin/pythonimport psycopg2conn = 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 successfullyID = 1NAME = PaulADDRESS = CaliforniaSALARY = 20000.0ID = 2NAME = AllenADDRESS = TexasSALARY = 15000.0ID = 3NAME = TeddyADDRESS = NorwaySALARY = 20000.0ID = 4NAME = MarkADDRESS = Rich-MondSALARY = 65000.0Operation done successfully
UPDATE 操作
Python代碼顯示如何,我們可以使用UPDATE語句來更新記錄,然後從COMPANY表擷取並顯示更新的記錄
#!/usr/bin/pythonimport psycopg2conn = 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.commitprint "Total number of rows updated :", cur.rowcountcur.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()
DELETE 操作
Python代碼顯示了如何我們可以使用DELETE語句刪除記錄,然後擷取並顯示COMPANY 表剩餘的記錄:
#!/usr/bin/pythonimport psycopg2conn = 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.commitprint "Total number of rows deleted :", cur.rowcountcur.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 操作PostgreSQL