最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

官方文檔傳送門: 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)文章

  • Python優(yōu)化算法之遺傳算法案例代碼

    Python優(yōu)化算法之遺傳算法案例代碼

    優(yōu)化算法,尤其是啟發(fā)式的仿生智能算法在最近很火,它適用于解決管理學(xué),運(yùn)籌學(xué),統(tǒng)計(jì)學(xué)里面的一些優(yōu)化問(wèn)題,這篇文章主要介紹了Python優(yōu)化算法—遺傳算法,需要的朋友可以參考下
    2023-02-02
  • ???????Python?入門學(xué)習(xí)之函數(shù)式編程

    ???????Python?入門學(xué)習(xí)之函數(shù)式編程

    這篇文章主要介紹了???????Python?入門學(xué)習(xí)之函數(shù)式編程,?Python?中的函數(shù)式編程技術(shù)進(jìn)行了簡(jiǎn)單的入門介紹,下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-05-05
  • django settings.py 配置文件及介紹

    django settings.py 配置文件及介紹

    Django的settings文件包含Django應(yīng)用的所有配置項(xiàng)。接下來(lái)通過(guò)本文給大家介紹django settings.py 配置文件的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧
    2019-07-07
  • pandas每次多Sheet寫入文件的方法

    pandas每次多Sheet寫入文件的方法

    今天小編就為大家分享一篇pandas每次多Sheet寫入文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python中pywifi的具體使用

    python中pywifi的具體使用

    本文主要介紹了python中pywifi的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 淺談Python用QQ郵箱發(fā)送郵件時(shí)授權(quán)碼的問(wèn)題

    淺談Python用QQ郵箱發(fā)送郵件時(shí)授權(quán)碼的問(wèn)題

    下面小編就為大家分享一篇淺談Python用QQ郵箱發(fā)送郵件時(shí)授權(quán)碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • python 如何讀、寫、解析CSV文件

    python 如何讀、寫、解析CSV文件

    這篇文章主要介紹了python 如何讀、寫、解析CSV文件,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • python依賴安裝兩種常用方式

    python依賴安裝兩種常用方式

    這篇文章主要給大家介紹了關(guān)于python依賴安裝兩種常用方式的相關(guān)資料,python本身做為一門解釋性語(yǔ)言,說(shuō)它功能強(qiáng)大,是因?yàn)樗兄S富的模塊或稱之為依賴(包),需要的朋友可以參考下
    2023-10-10
  • 詳解Python中的上下文管理器原理

    詳解Python中的上下文管理器原理

    這篇文章主要為大家詳細(xì)介紹了Python中的上下文管理器的原理與使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-03-03
  • 對(duì)python cv2批量灰度圖片并保存的實(shí)例講解

    對(duì)python cv2批量灰度圖片并保存的實(shí)例講解

    今天小編就為大家分享一篇對(duì)python cv2批量灰度圖片并保存的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11

最新評(píng)論

阳城县| 枣强县| 亚东县| 柳河县| 驻马店市| 肃南| 开封县| 吉首市| 渝北区| 平遥县| 包头市| 冀州市| 大同县| 容城县| 靖州| 米易县| 兰考县| 讷河市| 通榆县| 迁安市| 巴林左旗| 红桥区| 衡阳市| 浮山县| 德格县| 乐清市| 堆龙德庆县| 安新县| 甘孜县| 潜山县| 思南县| 城步| 德江县| 明星| 大港区| 平邑县| 汶川县| 阿坝| 神农架林区| 泰来县| 华安县|