Python:mysql-connector-Python模塊對MySQL數(shù)據(jù)庫進行增刪改查

      網(wǎng)友投稿 865 2025-04-02

      Mysql文檔:https://dev.Mysql.com/doc/connector-python/en/


      PYPI: https://pypi.org/project/mysql-connector-python/

      mysql-connector-python 是MySQL官方的Python語言MySQL連接模塊

      安裝

      $ pip install mysql-connector-python

      1

      代碼示例

      連接管理

      # -*- coding: utf-8 -*- import mysql.connector db_config = { "database": "mydata", "user": "root", "password": "123456", "host": "127.0.0.1", "port": 3306, } # 連接數(shù)據(jù)庫獲取游標,可以設(shè)置返回數(shù)據(jù)的格式,元組,命令元組,字典等... connect = mysql.connector.Connect(**db_config) cursor = connect.cursor(dictionary=True) # 關(guān)閉游標和連接 cursor.close() connect.close()

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      寫操作

      數(shù)據(jù) 寫入 和 更新 都可以使用 execute 和 executemany

      刪除就使用execute

      寫操作都需要 commit 才會生效

      # 插入元組數(shù)據(jù) insert_tuple_sql = "insert into student(name, age) values(%s, %s)" data = ("Tom", 23) cursor.execute(insert_tuple_sql, data) connect.commit() print(cursor.lastrowid) # 一般插入一條時使用,獲取插入id # 插入多條元組數(shù)據(jù) insert_tuple_sql = "insert into student(name, age) values(%s, %s)" data = [ ("Tom", 23), ("Jack", 25), ] cursor.executemany(insert_tuple_sql, data) connect.commit() # 插入字典數(shù)據(jù) insert_dict_sql = "insert into student(name, age) values(%(name)s, %(age)s)" data = { "name": "Tom", "age": 25 } cursor.execute(insert_dict_sql, data) connect.commit() # 插入多條字典數(shù)據(jù) insert_dict_sql = "insert into student(name, age) values(%(name)s, %(age)s)" data = [ { "name": "Tom", "age": 26 }, { "name": "Jack", "age": 27 } ] cursor.executemany(insert_dict_sql, data) connect.commit()

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      Python:mysql-connector-python模塊對MySQL數(shù)據(jù)庫進行增刪改查

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      讀操作

      cursor.execute("select * from student where name=%s limit 3", ("Tom",)) rows = cursor.fetchall() print(rows) # 因為開始設(shè)置的返回數(shù)據(jù)格式是字典 dictionary ,所以直接返回字典數(shù)據(jù) # [ # {'id': 1, 'name': 'Tom', 'age': 23}, # {'id': 2, 'name': 'Tom', 'age': 23}, # {'id': 3, 'name': 'Tom', 'age': 25} #] # 獲取一些元數(shù)據(jù)信息 print(cursor.column_names) # ('id', 'name', 'age') print(cursor.description) # [ # ('id', 3, None, None, None, None, 0, 16899), # ('name', 253, None, None, None, None, 1, 0), # ('age', 3, None, None, None, None, 1, 0) #] print(cursor.rowcount) # 3 print(cursor.statement) # select * from student where name='Tom' limit 3 print(cursor.with_rows) # True

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      MySQL Python 數(shù)據(jù)庫

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:WPS工作表標簽怎么添加顏色?
      下一篇:word系統(tǒng)中默認輸入的字體是幾號宋體字?(在word文檔中默認中文字體為宋體)
      相關(guān)文章
      亚洲国产中文在线视频| 亚洲酒色1314狠狠做| 亚洲另类精品xxxx人妖| 亚洲高清在线播放| 亚洲av无码潮喷在线观看| 亚洲精品成人片在线播放| 亚洲欧洲无码AV电影在线观看| 亚洲片国产一区一级在线观看| 亚洲精品无码久久久| 亚洲毛片不卡av在线播放一区| 亚洲AV无码之日韩精品| 亚洲av无码成人精品区| 亚洲äv永久无码精品天堂久久 | 亚洲国产成人高清在线观看| 亚洲精品美女久久久久99| 精品亚洲视频在线观看| 在线精品亚洲一区二区三区| 亚洲综合无码AV一区二区 | 亚洲熟妇中文字幕五十中出| 亚洲精品无码久久一线| 人人狠狠综合久久亚洲88| 亚洲AV无码专区电影在线观看| 亚洲AV永久无码精品| 亚洲天天在线日亚洲洲精| 亚洲综合激情视频| 亚洲欧洲精品一区二区三区| 亚洲乱码中文论理电影| 亚洲愉拍一区二区三区| mm1313亚洲国产精品无码试看| 成人亚洲综合天堂| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产精一品亚洲二区在线播放| 亚洲国产精品一区二区成人片国内| 亚洲AV无码乱码在线观看富二代 | 久久噜噜噜久久亚洲va久| 久久亚洲国产精品成人AV秋霞| 亚洲午夜精品一区二区公牛电影院| 激情综合亚洲色婷婷五月| 亚洲乱妇熟女爽到高潮的片| 免费亚洲视频在线观看| 青青草原亚洲视频|