内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

PostgreSql數(shù)據(jù)庫(kù)怎么利用Python實(shí)現(xiàn)操作-創(chuàng)新互聯(lián)

PostgreSql數(shù)據(jù)庫(kù)怎么利用Python實(shí)現(xiàn)操作?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

為長(zhǎng)安等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及長(zhǎng)安網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、長(zhǎng)安網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

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)于PostgreSql數(shù)據(jù)庫(kù)怎么利用Python實(shí)現(xiàn)操作問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

分享題目:PostgreSql數(shù)據(jù)庫(kù)怎么利用Python實(shí)現(xiàn)操作-創(chuàng)新互聯(lián)
地址分享:http://www.rwnh.cn/article2/ppdoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站制作、建站公司軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)
临颍县| 汤原县| 会同县| 德兴市| 义乌市| 东丽区| 黄大仙区| 武清区| 六枝特区| 铜川市| 漳平市| 哈尔滨市| 尉犁县| 广德县| 工布江达县| 历史| 襄垣县| 松滋市| 博野县| 房产| 调兵山市| 二连浩特市| 留坝县| 苏州市| 老河口市| 平塘县| 蓬溪县| 郎溪县| 平武县| 冀州市| 涞源县| 正安县| 长泰县| 望奎县| 花垣县| 会东县| 垣曲县| 扶余县| 大田县| 江都市| 宜都市|