python MysqlDb模塊安裝及其使用詳解
python調(diào)用mysql數(shù)據(jù)庫通常通過mysqldb模塊,簡(jiǎn)單說下如何調(diào)用
1.安裝驅(qū)動(dòng)
目前有兩個(gè)MySQL的驅(qū)動(dòng),我們可以選擇其中一個(gè)進(jìn)行安裝:
1. MySQL-python:是封裝了MySQL C驅(qū)動(dòng)的Python驅(qū)動(dòng);
2.mysql-connector-python:是MySQL官方的純Python驅(qū)動(dòng)。
這里使用MySQL-python驅(qū)動(dòng),即MySQLdb模塊。
命令行安裝
pip install python-mysql
或者在pycharm包中安裝
源碼安裝方式
訪問: http://www.lfd.uci.edu/~gohlke/pythonlibs/,下載MySQL_python-1.2.5-cp27-none-win_amd64.whl

將其拷貝到Python安裝目錄下的Scripts目錄下,在文件位置打開cmd,執(zhí)行pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
驗(yàn)證,python(command line)輸入import MySQLdb,沒報(bào)錯(cuò),說明安裝成功。

測(cè)試連接:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
# 連接數(shù)據(jù)庫 連接地址 賬號(hào) 密碼 數(shù)據(jù)庫 數(shù)據(jù)庫編碼
db = MySQLdb.connect("localhost", "root", "123456", "test" , charset="utf8")
# 使用cursor()方法獲取操作游標(biāo)
cursor = db.cursor()
# 使用execute方法執(zhí)行SQL語句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法獲取一條數(shù)據(jù)庫。
data = cursor.fetchone()
print "Database version : %s " % data
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
示例1:
#!/usr/bin/python
# coding=utf-8
import MySQLdb
import os, sys
import json
class MysqlDb(object):
def __init__(self):
self.host = "127.0.0.1"
@staticmethod
def get_connect():
db = MySQLdb.connect(self.host , "mail_report", "mail_report", "mailawst", charset="utf8")
return db
def get_mysql_info(self,start_time,end_time):
tmp = []
db = self.get_connect()
sql = '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 = db.cursor()
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()
示例2:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
# 打開數(shù)據(jù)庫連接
db = MySQLdb.connect("localhost", "root", "123456", "test")
# 使用cursor()方法獲取操作游標(biāo)
cursor = db.cursor()
# SQL插入語句
ins_sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('yu', 'jie', 20, 'M', 8000)"""
ins_sql1 = 'insert into employee(first_name, last_name, age, sex, income) values (%s, %s, %s, %s, %s)'
# SQL查詢語句
sel_sql = 'select * from employee where first_name = %s'
# SQL更新語句
upd_sql = 'update employee set age = %s where sex = %s'
# SQL刪除語句
del_sql = 'delete from employee where first_name = %s'
try:
# 執(zhí)行sql語句
# insert
cursor.execute(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
cursor.execute(del_sql, ('xu',))
# 提交到數(shù)據(jù)庫執(zhí)行
db.commit()
except:
# 發(fā)生錯(cuò)誤時(shí)回滾
db.rollback()
# 關(guān)閉數(shù)據(jù)庫連接
db.close()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決pycharm導(dǎo)入本地py文件時(shí),模塊下方出現(xiàn)紅色波浪線的問題
- pycharm中導(dǎo)入模塊錯(cuò)誤時(shí)提示Try to run this command from the system terminal
- Pycharm中出現(xiàn)ImportError:DLL load failed:找不到指定模塊的解決方法
- 解決Pycharm 包已經(jīng)下載,但是運(yùn)行代碼提示找不到模塊的問題
- Pycharm編輯器技巧之自動(dòng)導(dǎo)入模塊詳解
- python中MySQLdb模塊用法實(shí)例
- Python下的Mysql模塊MySQLdb安裝詳解
- 關(guān)于pycharm找不到MySQLdb模塊的解決方法
相關(guān)文章
在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
今天小編就為大家分享一篇在NumPy中創(chuàng)建空數(shù)組/矩陣的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python3.9用pip安裝wordcloud庫失敗的解決過程
一般在命令行輸入pip install wordcloud 總會(huì)顯示安裝失敗,所以下面這篇文章主要給大家介紹了關(guān)于Python3.9用pip安裝wordcloud庫失敗的解決過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python對(duì)小數(shù)進(jìn)行除法運(yùn)算的正確方法示例
這篇文章主要介紹了Python對(duì)小數(shù)進(jìn)行除法運(yùn)算的正確方法示例,正確的方法是需要轉(zhuǎn)換成浮點(diǎn)數(shù),否則永遠(yuǎn)不會(huì)得到正確結(jié)果,需要的朋友可以參考下2014-08-08
Python中執(zhí)行MySQL結(jié)果限制和分頁查詢示例詳解
這篇文章主要為大家介紹了Python中執(zhí)行MySQL結(jié)果限制和分頁查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
全面分析Python的優(yōu)點(diǎn)和缺點(diǎn)
本篇文章給大家詳細(xì)分析了Python的優(yōu)點(diǎn)和缺點(diǎn)以及相關(guān)的優(yōu)勢(shì)劣勢(shì)分析,對(duì)此有興趣的朋友學(xué)習(xí)下。2018-02-02
pycharm配置anaconda環(huán)境時(shí)找不到python.exe解決辦法
今天來說一下python中一個(gè)管理包很好用的工具anaconda,可以輕松實(shí)現(xiàn)python中各種包的管理,這篇文章主要給大家介紹了關(guān)于pycharm配置anaconda環(huán)境時(shí)找不到python.exe的解決辦法,需要的朋友可以參考下2023-10-10
Python使用PDFMiner解析PDF代碼實(shí)例
本篇文章主要介紹了Python使用PDFMiner解析PDF代碼實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

