您好,登錄后才能下訂單哦!
PostgreSql數據庫怎么利用Python實現操作?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
操作數據庫最快的方式當然是直接用使用SQL語言直接對數據庫進行操作,但是偶爾我們也會碰到在代碼中操作數據庫的情況,我們可能用ORM類的庫對數控庫進行操作,但是當需要操作大量的數據時,ORM的數據顯的太慢了。在python中,遇到這樣的情況,我推薦使用psycopg2
操作postgresql
數據庫
官方文檔傳送門: http://initd.org/psycopg/docs/index.html
連接pg并創建表
PG_SQL_LOCAL = { 'database': 'postgres', 'user': 'postgres', 'password': "8dsa581", # 'host':'10.27.78.1', 'host': 'localhost' } def connectPostgreSQL(): conn = psycopg2.connect(**PG_SQL_LOCAL) print('connect successful!') cursor = conn.cursor() cursor.execute(''' create table public.members( id integer not null primary key, name varchar(32) not null, password varchar(32) not null, singal varchar(128) )''') conn.commit() conn.close() print('table public.member is created!')
一條一條的增加數據
def insertOperate(): conn = psycopg2.connect(**PG_SQL_LOCAL) cursor = conn.cursor() cursor.execute("insert into public.member(id,name,password,singal)\ values(1,'member0','password0','signal0')") cursor.execute("insert into public.member(id,name,password,singal)\ values(2,'member1','password1','signal1')") cursor.execute("insert into public.member(id,name,password,singal)\ values(3,'member2','password2','signal2')") cursor.execute("insert into public.member(id,name,password,singal)\ values(4,'member3','password3','signal3')") row = conn.fetchone() print(row) conn.commit() conn.close() print('insert records into public.memmber successfully')
fetchall() 一次性獲取所有數據
fetchmany() 一次值提取2000條數據(使用服務端的游標)
def selectOperate(): conn = psycopg2.connect(**PG_SQL_LOCAL) cursor = conn.cursor() cursor.execute("select id,name,password,singal from public.member where id>2") # rows = cursor.fetchall() # for row in rows: # print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3],) while True: rows = cursor.fetchmany(2000) if not rows: break for row in rows: # print('id=', row['id'], ',name=', row['name'], ',pwd=', row['pwd'], ',singal=', row['singal'],) rid,name,pwd,singal = row print(rid,name,pwd,singal) # print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], ) conn.close()
更新數據
def updateOperate(): conn = psycopg2.connect(**PG_SQL_LOCAL) cursor=conn.cursor() result = cursor.execute("update public.member set name='member X' where id=3") print(result) conn.commit() print("Total number of rows updated :", cursor.rowcount) cursor.execute("select id,name,password,singal from public.member") rows=cursor.fetchall() for row in rows: print('id=',row[0], ',name=',row[1],',pwd=',row[2],',singal=',row[3],'\n') conn.close()
刪除數據
def deleteOperate(): conn = psycopg2.connect(**PG_SQL_LOCAL) cursor = conn.cursor() cursor.execute("select id,name,password,singal from public.member") rows = cursor.fetchall() for row in rows: print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], '\n') print('begin delete') cursor.execute("delete from public.member where id=2") conn.commit() print('end delete') print("Total number of rows deleted :", cursor.rowcount) cursor.execute("select id,name,password,singal from public.member") rows = cursor.fetchall() for row in rows: print('id=', row[0], ',name=', row[1], ',pwd=', row[2], ',singal=', row[3], '\n') conn.close()
帶有時間格式是,只需要傳入時間格式的字符串(‘2017-05-27')即可,PG會自動識別
cur.execute("INSERT INTO Employee " "VALUES('Gopher', 'China Beijing', 100, '2017-05-27')") # 查詢數據 cur.execute("SELECT * FROM Employee") rows = cur.fetchall() for row in rows: print('name=' + str(row[0]) + ' address=' + str(row[1]) + ' age=' + str(row[2]) + ' date=' + str(row[3]), type(row[3])) # 插入數據 sql = """INSERT INTO Employees VALUES(%s, %s, %s,%s) """ var = [] var.append([row[0], row[1], row[2], row[3]]) cur.executemany(sql, var) # 提交事務 conn.commit() # 關閉連接 conn.close()
關于PostgreSql數據庫怎么利用Python實現操作問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。