日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python的mysql模块_python使用MySQLdb模块连接MySQL

發(fā)布時(shí)間:2025/10/17 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的mysql模块_python使用MySQLdb模块连接MySQL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.安裝驅(qū)動

目前有兩個(gè)MySQL的驅(qū)動,我們可以選擇其中一個(gè)進(jìn)行安裝:

MySQL-python:是封裝了MySQL C驅(qū)動的Python驅(qū)動;mysql-connector-python:是MySQL官方的純Python驅(qū)動。

MySQL-python:

mysql-connector-python:

2.測試連接

這里使用MySQL-python驅(qū)動,即MySQLdb模塊。

test_connect.py

1 #!/usr/bin/python

2 #-*- coding: UTF-8 -*-

3

4 importMySQLdb5

6 #打開數(shù)據(jù)庫連接

7 db = MySQLdb.connect("localhost", "root", "123456", "test")8

9 #使用cursor()方法獲取操作游標(biāo)

10 cursor =db.cursor()11

12 #使用execute方法執(zhí)行SQL語句

13 cursor.execute("SELECT VERSION()")14

15 #使用 fetchone() 方法獲取一條數(shù)據(jù)庫。

16 data =cursor.fetchone()17

18 print "Database version : %s" %data19

20 #關(guān)閉數(shù)據(jù)庫連接

21 db.close()

測試結(jié)果如下,連接成功:

3.創(chuàng)建數(shù)據(jù)庫表

測試成功后,我們可以在python中直接為MySQL創(chuàng)建表:

create_table.py

1 #!/usr/bin/python

2 #-*- coding: UTF-8 -*-

3

4 importMySQLdb5

6 #打開數(shù)據(jù)庫連接

7 db = MySQLdb.connect("localhost", "root", "123456", "test")8

9 #使用cursor()方法獲取操作游標(biāo)

10 cursor =db.cursor()11

12 #如果數(shù)據(jù)表已經(jīng)存在使用 execute() 方法刪除表。

13 cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")14

15 #創(chuàng)建數(shù)據(jù)表SQL語句

16 sql = """CREATE TABLE EMPLOYEE (17 FIRST_NAME CHAR(20) NOT NULL,18 LAST_NAME CHAR(20),19 AGE INT,20 SEX CHAR(1),21 INCOME FLOAT )"""

22

23 cursor.execute(sql)24

25 #關(guān)閉數(shù)據(jù)庫連接

26 db.close()

建表結(jié)果 如下:

4.操作數(shù)據(jù)庫表

注意點(diǎn):MySQL中的占位符為%s

operate_table.js

1 #!/usr/bin/python

2 #-*- coding: UTF-8 -*-

3

4 importMySQLdb5

6 #打開數(shù)據(jù)庫連接

7 db = MySQLdb.connect("localhost", "root", "123456", "test")8

9 #使用cursor()方法獲取操作游標(biāo)

10 cursor =db.cursor()11

12 #SQL插入語句

13 ins_sql = """INSERT INTO EMPLOYEE(FIRST_NAME,14 LAST_NAME, AGE, SEX, INCOME)15 VALUES ('yu', 'jie', 20, 'M', 8000)"""

16

17 ins_sql1 = 'insert into employee(first_name, last_name, age, sex, income) values (%s, %s, %s, %s, %s)'

18

19 #SQL查詢語句

20 sel_sql = 'select * from employee where first_name = %s'

21

22 #SQL更新語句

23 upd_sql = 'update employee set age = %s where sex = %s'

24

25 #SQL刪除語句

26 del_sql = 'delete from employee where first_name = %s'

27

28 try:29 #執(zhí)行sql語句

30 #insert

31 cursor.execute(ins_sql)32 cursor.execute(ins_sql1, ('xu', 'f', 20, 'M', 8000))33 #select

34 cursor.execute(sel_sql, ('yu',))35 values =cursor.fetchall()36 printvalues37 #update

38 cursor.execute(upd_sql, (24, 'M',))39 #delete

40 cursor.execute(del_sql, ('xu',))41

42 #提交到數(shù)據(jù)庫執(zhí)行

43 db.commit()44 except:45 #發(fā)生錯(cuò)誤時(shí)回滾

46 db.rollback()47

48 #關(guān)閉數(shù)據(jù)庫連接

49 db.close()

執(zhí)行插入操作

執(zhí)行查詢操作

執(zhí)行更新操作

執(zhí)行刪除操作

查詢語句的知識點(diǎn):

Python查詢Mysql使用 fetchone() 方法獲取單條數(shù)據(jù), 使用fetchall() 方法獲取多條數(shù)據(jù)。

fetchone():?該方法獲取下一個(gè)查詢結(jié)果集。結(jié)果集是一個(gè)對象

fetchall():接收全部的返回結(jié)果行.

例如該例子:

1 sel_sql = 'select * from employee where first_name = %s'

2 cursor.execute(sel_sql, ('yu',))3 results =cursor.fetchall()4 for row inresults:5 fname =row[0]6 lname = row[1]7 age = row[2]8 sex = row[3]9 income = row[4]10 print "fname=%s, lname=%s,age=%d,sex=%s,income=%d" % (fname, lname, age, sex, income)

結(jié)果如下:

總結(jié)

以上是生活随笔為你收集整理的python的mysql模块_python使用MySQLdb模块连接MySQL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。