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

Python 解析pymysql模塊操作數(shù)據(jù)庫的方法

 更新時間:2020年02月18日 15:05:14   作者:cici_vivi  
這篇文章主要介紹了Python 解析pymysql模塊操作數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

pymysql 是 python 用來操作MySQL的第三方庫,下面具體介紹和使用該庫的基本方法。

1.建立數(shù)據(jù)庫連接

通過 connect 函數(shù)中 parameter 參數(shù) 建立連接,連接成功返回Connection對象

import pymysql

#建立數(shù)據(jù)庫連接
connection = pymysql.connect(host = 'localhost',
        user = 'root',
        password = '123456',
        database = 'mydb',
        charset = 'utf8'
)
#print(connection)

pymysql.connect()函數(shù)中常用的連接參數(shù)有以下幾種:

  • host:數(shù)據(jù)庫主機(jī)名或者ip地址
  • port:端口號
  • user:數(shù)據(jù)庫的賬號
  • password 或 passwd:數(shù)據(jù)庫的密碼
  • database 或 db:數(shù)據(jù)庫的名字
  • charset:編碼方式

Connection對象的重要方法:

  • close() 關(guān)閉數(shù)據(jù)庫連接
  • commit() 提交數(shù)據(jù)庫事物
  • rollback() 回滾數(shù)據(jù)庫事務(wù)
  • cursor() 獲得 Cursor游標(biāo)對象

2.創(chuàng)建游標(biāo)

一個Cursor游標(biāo)對象,暫時保存了SQL操作所影響到的數(shù)據(jù),相同的數(shù)據(jù)庫連接創(chuàng)建的游標(biāo)所引起的數(shù)據(jù)變化,會馬上反應(yīng)到同一連接中的其它游標(biāo)對象。但是不同數(shù)據(jù)庫連接中的游標(biāo)對象,是否能及時反映出來,則與數(shù)據(jù)庫事物管理有關(guān)。

Cursor對象基本方法和屬性:

execute(operation,[parameters])

執(zhí)行一條SQL語句,operation時SQL語句,parameters是其參數(shù)。返回值是整數(shù),表示執(zhí)行SQL語句影響的行數(shù)

executemany(operation,[parameters])

批量執(zhí)行SQL語句

callproc(procname,[parameters])

執(zhí)行存儲過程,procname是存儲過程名

使用execute()和executemany()方法查詢后,通過以下提取方法提取結(jié)果集

fetchone()

從結(jié)果集當(dāng)中返回一條記錄的序列,無則返回None

fetchmany([size=cursor.arraysize])

從結(jié)果集當(dāng)中返回小于或等于size的記錄序列,無則返回空序列,size默認(rèn)是整個游標(biāo)的行數(shù)

fetchall()

從結(jié)果集當(dāng)中返回所有的行數(shù)

3.建立數(shù)據(jù)庫(這里我使用的是NaviCat)

創(chuàng)建一個名為pydb的數(shù)據(jù)庫,表名為user,字段name和userid

在這里插入圖片描述

在這里插入圖片描述

數(shù)據(jù)的查找

#建立數(shù)據(jù)庫連接
connection = pymysql.connect(host = 'localhost',
        user = 'root',
        password = '123456',
        database = 'mydb',
        charset = 'utf8'
)
#print(connection)
try:
 #創(chuàng)建游標(biāo)對象
 with connection.cursor() as cursor:
  #執(zhí)行SQL操作
  sql = 'select name, userid from user where userid >%(id)s'
  cursor.execute(sql, {'id':0})
  #提取數(shù)據(jù)集
  result_set = cursor.fetchall()
  for row in result_set:
   print('id:{0} - name:{1}'.format(row[1],row[0]))
  #游標(biāo)自動關(guān)閉
finally:
 #關(guān)閉連接
 connection.close()

數(shù)據(jù)插入

#數(shù)據(jù)增加
connection = pymysql.connect(host = 'localhost',
        user = 'root',
        password = '123456',
        database = 'mydb',
        charset = 'utf8'
)
try:
 with connection.cursor() as cursor:
  sql = 'insert into user (userid,name) values (%s,%s)'
  cursor.execute(sql,(3,'cc'))
  #affectcount = cursor.execute(sql,(3,'cc'))
  #print('影響的數(shù)據(jù)行數(shù):{0}'.format(affectcount))
  #提交數(shù)據(jù)庫事務(wù)
  connection.commit()
except pymysql.DatabaseError:
 #數(shù)據(jù)庫事務(wù)回滾
 connection.rollback()
finally:
 connection.close()

執(zhí)行結(jié)果:

在這里插入圖片描述

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

#數(shù)據(jù)更新
connection = pymysql.connect(host = 'localhost',
        user = 'root',
        password = '123456',
        database = 'mydb',
        charset = 'utf8'
)
#print(connection)
try:
 with connection.cursor() as cursor:
  sql = 'update user set name = %s where userid > %s'
  cursor.execute(sql,('Tom',2))
  #提交事務(wù)
  connection.commit()
  print('更新成功')
except pymysql.DatabaseError as e:
 connection.rollback()
 print(e)
finally:
 connection.close()

執(zhí)行結(jié)果:

在這里插入圖片描述

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

#數(shù)據(jù)刪除
connection = pymysql.connect(host = 'localhost',
        user = 'root',
        password = '123456',
        database = 'mydb',
        charset = 'utf8'
        )
try:
 with connection.cursor() as cursor:
  sql = 'delete from user where userid = %s'
  cursor.execute(sql,(1))
  #提交事務(wù)
  connection.commit()
  print("刪除成功")
except pymysql.DatabaseError as e:
 connection.rollback()
 print(e)
finally:
 connection.close()

執(zhí)行結(jié)果:

在這里插入圖片描述

總的來說和java進(jìn)行對比,在數(shù)據(jù)庫的連接 和對

數(shù)據(jù)集進(jìn)行的處理上,python體現(xiàn)的非常簡潔,最主要易于使用和理解。人生苦短,我用python!

總結(jié)

以上所述是小編給大家介紹的Python 解析pymysql模塊操作數(shù)據(jù)庫的方法,希望對大家有所幫助!

相關(guān)文章

最新評論

安阳县| 湖南省| 泰顺县| 特克斯县| 城口县| 进贤县| 嘉义县| 墨竹工卡县| 梁山县| 禄丰县| 铜山县| 泗水县| 郁南县| 澜沧| 英德市| 安新县| 万年县| 元氏县| 锡林郭勒盟| 锦屏县| 宁强县| 左云县| 多伦县| 南陵县| 佳木斯市| 章丘市| 苏州市| 五寨县| 时尚| 阿合奇县| 桐城市| 温泉县| 沿河| 武定县| 桓台县| 行唐县| 南郑县| 新乡市| 江孜县| 闸北区| 潮州市|