python usually call MySQL database through the mysqldb module, simply say how to call the

1. to install the driver

currently has two MySQL drive, we can choose one of them to install:


1. is a package of MySQL C driver Python driver;

2.mysql-connector-python: is the official MySQL pure Python driver.

uses the MySQL-python driver here, that is, the MySQLdb module.

 PIP install installation command line python-mysql

or in the pycharm package to install the


access source code:" pythonlibs/


will be copied to Python the installation directory under the Scripts directory, open the CMD file in the PIP install MySQL_python-1.2.5-cp27-none-win_amd64.whl

executive position,

verification, python (command line) import MySQLdb input, no error, indicating successful installation.


 # connection: 

! /usr/bin/python coding: UTF-8 import MySQLdb # -*- -*- # connection database connection address account password database encoding DB = MySQLdb.connect ("localhost", "root", "123456", "test charset=", "utf8") # using cursor () method to obtain the operation cursor = db.cursor (# cursor) use the execute method to execute the SQL cursor.execute (SELECT) VERSION (fetchone) (# ") using a database access method. Data = cursor.fetchone (print) Database version:%s% data # db.close (

) close the database connection

example 1:

 /usr/bin/python coding=utf-8 import MySQLdb # #! Import OS, Sys import JSON class MysqlDb (object): def __init__ (self): = "" @staticmethod (get_connect): DB = def MySQLdb.connect (, "mail_report", "mail_report", "mailawst", "charset= utf8") return DB def get_mysql_info (self, start_time, end_time): TMP = dB (SQL) [] = self.get_connect'select = send_time, mail_id, mail_addr, server_domain, server_ip, mail_status from real_mail_log where send_time > "%s"; and send_time <%s limit 10; '% (start_time, end_time) cursor = (rsor) cursor.execute (SQL) values = cursor.fetchall (for) I in values: data data["send_time"] = {} = str (i[0]) data["mail_id"] = str (i[1]) data["mail_addr"]= str (i[2]) data["server_domain"] = str (i[3]) data[= "server_ip"] str (i[4]) data["mail_status"]= str (i[5].encode ('utf8')) tmp.append (data) data = json.dumps (TMP, ensure_ascii=False) db.close (return) data def (main): u = MysqlDb (print) u.get_mysql_info ('2017-05-01 00:00:02','2017-05-01 00:50:03') if __name__'__main__' (

): Main =

example 2:

 #! /usr/bin/python # -*- coding: UTF-8 import MySQLdb -*- # open database connection ("localhost DB = MySQLdb.connect"," Root "," 123456 "," test ") # using cursor () method to obtain the cursor (db.cursor) = cursor operation # SQL insert statement ins_sql =" INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('yu','jie', 20,'M', 8000) "and" ins_sql1 "'Insert into = employee (first_name, last_name, age, sex, income) values (%s,%s,%s,%s,%s) #' SQL query sel_sql ='select * from employee where first_name =%s'SQL ='update # upd_sql update employee set age =%s where sex =%s' SQL del_sql = # delete statement'delete from employee where first_name =%s'try: insert cursor.execute # # execute SQL statements (ins_sql) cursor.execute (ins_sql1 ('xu','f', 20,'M', 8000) # select cursor.execute (sel_sql), ('yu')) Values = cursor.fetchall (print) values update cursor.execute (upd_sql, # (24,'M') delete (del_sql, cursor.execute) # ('xu') #) submitted to the database (db.commit) implementation of except: error occurs when # (db.rollback) # rollback (db.close) closes the database connection all the content above is the 

. I hope to help everyone to learn, I hope you will support a script.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Python MysqlDb module installation and detailed solution | Script Home

You may also be interested in these articles!