python MysqlDb模塊安裝及其使用詳解
python調(diào)用mysql數(shù)據(jù)庫(kù)通常通過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ù)庫(kù) 連接地址 賬號(hào) 密碼 數(shù)據(jù)庫(kù) 數(shù)據(jù)庫(kù)編碼 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ù)庫(kù)。 data = cursor.fetchone() print "Database version : %s " % data # 關(guān)閉數(shù)據(jù)庫(kù)連接 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ù)庫(kù)連接 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ù)庫(kù)執(zhí)行 db.commit() except: # 發(fā)生錯(cuò)誤時(shí)回滾 db.rollback() # 關(guān)閉數(shù)據(jù)庫(kù)連接 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)文章
使用Python實(shí)現(xiàn)ELT統(tǒng)計(jì)多個(gè)服務(wù)器下所有數(shù)據(jù)表信息
這篇文章主要介紹了使用Python實(shí)現(xiàn)ELT統(tǒng)計(jì)多個(gè)服務(wù)器下所有數(shù)據(jù)表信息,ETL,是英文Extract-Transform-Load的縮寫,用來描述將數(shù)據(jù)從來源端經(jīng)過抽取(extract)、轉(zhuǎn)換(transform)、加載(load)至目的端的過程,需要的朋友可以參考下2023-07-07python next()和iter()函數(shù)原理解析
這篇文章主要介紹了python next()和iter()函數(shù)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02python連接mysql調(diào)用存儲(chǔ)過程示例
這篇文章主要介紹了python連接mysql調(diào)用存儲(chǔ)過程示例,需要的朋友可以參考下2014-03-03Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具
其實(shí)使用pangu做文本格式標(biāo)準(zhǔn)化的業(yè)務(wù)代碼在之前就實(shí)現(xiàn)了,主要能夠?qū)⒅形奈谋疚臋n中的文字、標(biāo)點(diǎn)符號(hào)等進(jìn)行標(biāo)準(zhǔn)化。但是為了方便起來我們這里使用了Qt5將其做成了一個(gè)可以操作的頁面應(yīng)用,需要的可以了解一下2022-10-10python 計(jì)算兩個(gè)列表的相關(guān)系數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了python 計(jì)算兩個(gè)列表的相關(guān)系數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06