欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python操作PostgreSql數(shù)據(jù)庫(kù)的方法(基本的增刪改查)

 更新時(shí)間:2020年12月29日 16:02:51   作者:Mark Huo  
這篇文章主要介紹了Python操作PostgreSql數(shù)據(jù)庫(kù)(基本的增刪改查),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Python操作PostgreSql數(shù)據(jù)庫(kù)(基本的增刪改查)

操作數(shù)據(jù)庫(kù)最快的方式當(dāng)然是直接用使用SQL語(yǔ)言直接對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作,但是偶爾我們也會(huì)碰到在代碼中操作數(shù)據(jù)庫(kù)的情況,我們可能用ORM類的庫(kù)對(duì)數(shù)控庫(kù)進(jìn)行操作,但是當(dāng)需要操作大量的數(shù)據(jù)時(shí),ORM的數(shù)據(jù)顯的太慢了。在python中,遇到這樣的情況,我推薦使用psycopg2操作postgresql數(shù)據(jù)庫(kù)

psycopg2

官方文檔傳送門(mén): http://initd.org/psycopg/docs/index.html

簡(jiǎn)單的增刪改查

連接

連接pg并創(chuàng)建表

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!')

一條一條的增加數(shù)據(jù)

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() 一次性獲取所有數(shù)據(jù)
  • fetchmany() 一次值提取2000條數(shù)據(jù)(使用服務(wù)端的游標(biāo))
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()

更新數(shù)據(jù)

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

刪除數(shù)據(jù)

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

補(bǔ)充,增加的字段帶有時(shí)間格式

帶有時(shí)間格式是,只需要傳入時(shí)間格式的字符串(‘2017-05-27')即可,PG會(huì)自動(dòng)識(shí)別

cur.execute("INSERT INTO Employee "
  "VALUES('Gopher', 'China Beijing', 100, '2017-05-27')")
# 查詢數(shù)據(jù)
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]))

 # 插入數(shù)據(jù)
 sql = """INSERT INTO Employees VALUES(%s, %s, %s,%s) """
 var = []
 var.append([row[0], row[1], row[2], row[3]])
 cur.executemany(sql, var)

# 提交事務(wù)
conn.commit()

# 關(guān)閉連接
conn.close()

到此這篇關(guān)于Python操作PostgreSql數(shù)據(jù)庫(kù)(基本的增刪改查)的文章就介紹到這了,更多相關(guān)Python操作PostgreSql數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flask模板渲染與Get和Post請(qǐng)求詳細(xì)介紹

    Flask模板渲染與Get和Post請(qǐng)求詳細(xì)介紹

    這篇文章主要介紹了Flask模板渲染與Get和Post請(qǐng)求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Python中將變量按行寫(xiě)入txt文本中的方法

    Python中將變量按行寫(xiě)入txt文本中的方法

    下面小編就為大家分享一篇Python中將變量按行寫(xiě)入txt文本中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python處理圖片之PIL模塊簡(jiǎn)單使用方法

    python處理圖片之PIL模塊簡(jiǎn)單使用方法

    這篇文章主要介紹了python處理圖片之PIL模塊簡(jiǎn)單使用方法,涉及Python使用PIL模塊實(shí)現(xiàn)針對(duì)圖片的銳化、繪制直線、繪制橢圓等相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • python使用append合并兩個(gè)數(shù)組的方法

    python使用append合并兩個(gè)數(shù)組的方法

    這篇文章主要介紹了python使用append合并兩個(gè)數(shù)組的方法,涉及Python中append方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • 能讓你輕松的實(shí)現(xiàn)自然語(yǔ)言處理的5個(gè)Python庫(kù)

    能讓你輕松的實(shí)現(xiàn)自然語(yǔ)言處理的5個(gè)Python庫(kù)

    今天教大家如何你輕松的實(shí)現(xiàn)自然語(yǔ)言預(yù)處理,僅僅需要5個(gè)python庫(kù),文中介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • djano一對(duì)一、多對(duì)多、分頁(yè)實(shí)例代碼

    djano一對(duì)一、多對(duì)多、分頁(yè)實(shí)例代碼

    在本篇文章里小編給大家整理的是關(guān)于djano一對(duì)一,多對(duì)多,分頁(yè)實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • Python用61行代碼實(shí)現(xiàn)圖片像素化的示例代碼

    Python用61行代碼實(shí)現(xiàn)圖片像素化的示例代碼

    這篇文章主要介紹了Python用61行代碼實(shí)現(xiàn)圖片像素化的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 如何使用Django默認(rèn)的Auth權(quán)限管理系統(tǒng)

    如何使用Django默認(rèn)的Auth權(quán)限管理系統(tǒng)

    本文主要介紹了如何使用Django默認(rèn)的Auth權(quán)限管理系統(tǒng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 詳解Python中數(shù)據(jù)的多種存儲(chǔ)形式

    詳解Python中數(shù)據(jù)的多種存儲(chǔ)形式

    這篇文章主要介紹了Python中數(shù)據(jù)的多種存儲(chǔ)形式,主要有JSON?文件存儲(chǔ)、CSV?文件存儲(chǔ)、關(guān)系型數(shù)據(jù)庫(kù)存儲(chǔ)及非關(guān)系型數(shù)據(jù)庫(kù)存儲(chǔ),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Python?SQLAlchemy插入日期時(shí)間時(shí)區(qū)詳解

    Python?SQLAlchemy插入日期時(shí)間時(shí)區(qū)詳解

    SQLAlchemy是一個(gè)功能強(qiáng)大且流行的?Python?庫(kù),它提供了一種靈活有效的與數(shù)據(jù)庫(kù)交互的方式,在本文中,我們將了解SQLAlchemy如何更新日期、時(shí)間和時(shí)區(qū)并將其插入數(shù)據(jù)庫(kù),感興趣的可以了解下
    2023-09-09

最新評(píng)論