您好,登錄后才能下訂單哦!
參考
:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001391435131816c6a377e100ec4d43b3fc9145f3bb8056000
http://www.cnblogs.com/fnng/p/3565912.html
一 模塊
mysql-connector-python:是MySQL官方的純Python驅動;
MySQL-python:是封裝了MySQL C驅動的Python驅動。
可以把兩個都裝上,使用的時候再決定用哪個:
$ pip install mysql-connector-python pip install MySQL-python
或者 使用yum安裝(包名我忘記了 可以是使用yum search mysql去找)
yum install MySQL-python
測試是否按照好的驅動
#!/usr/bin/env python #coding:utf-8 import sys try: import MySQLdb print "MySQL python drivier is ok!" except Exception, e: print e sys.exit(1) finally: sys.exit(1)
二、數據庫中的表
show create table net_data; 可以查看
CREATE TABLE `net_data` ( `_id` int(11) NOT NULL AUTO_INCREMENT, `create_date` datetime NOT NULL, `province` varchar(64) NOT NULL, `city` varchar(64) NOT NULL, `net_type` enum('CTC','CNC','CMC','JK') NOT NULL, `med` float NOT NULL, `loss` float NOT NULL, PRIMARY KEY (`_id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
三 先看插入的實例吧
作用,連接數據庫插入一條數據
數據庫插入一條數據
import MySQLdb .... .... def insert_into_mysql(info): conn= MySQLdb.Connect( host='localhost', port = 3306, user='root', passwd='12qwaszx', db ='netmap', charset='utf8', ) cur = conn.cursor() sqli = "insert into net_data (create_date,province,city,net_type,med,loss) values(%s,%s,%s,%s,%s,%s)" cur.execute(sqli,(datetime.now(),'上海','上海','CTC','23','0')) conn.commit() cur.close() conn.close()
同時插入多條數據
#info type is list 列表 def insert_into_mysql(info): conn= MySQLdb.Connect( host='localhost', port = 3306, user='root', passwd='12qwaszx', db ='netmap', charset='utf8', ) cur = conn.cursor() sqli = "insert into net_data (create_date,province,city,net_type,med,loss) values(%s,%s,%s,%s,%s,%s)" cur.executemany(sqli,(info)) conn.commit() cur.close() conn.close()
四、查詢實例(完成程序的一部分,完成的是一個統計報表發送的郵件的程序)
#!/usr/bin/env python #encoding=utf8 #by cuizhiliang at 2016-04-29 from datetime import datetime, timedelta import time from jinja2 import Template from sendmail import SendHtmlEmail import MySQLdb import os,sys reload(sys) sys.setdefaultencoding("utf-8") g= { 'host': '127.0.0.1', 'port': 3306, 'user': 'zabbix', 'password': 'zabbix', 'db':'zabbix', } now= datetime.now() yesterday = now - timedelta(1) yest_clock = (datetime.now() -timedelta(1)).strftime('%s') data = {} def select_mysql(cmd): conn= MySQLdb.Connect( host= g['host'], port= g['port'], user= g['user'], passwd= g['password'], db =g['db'], charset='utf8', ) #print cmd cur = conn.cursor() cur.execute(cmd) res =cur.fetchall() return res #zabbix 上用戶列表 def get_user_info(): cmd="select userid, alias, name, type from users;" user_list = [] user_list= list(select_mysql(cmd)) print user_list return user_list if __name__ == "__main__": data["user"]= get_user_info()
五、注意:
1 插入數據庫中文亂碼 連接數據的字符集 一定要設定
charset='utf8'
2 不慣數據庫中的字段是datetime類型還是int類型,在插入的時候都傳為%s 字符串類型,類型對我們是透明了,模塊會自動處理類型!
3 模塊在mac中可能會出錯 MySQLdb
報錯:
Traceback (most recent call last):
File "ping_info.py", line 12, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: p_w_picpath not found
解決方法,因為是C語言開發驅動,所有依賴mysql客戶端模塊
sudo find / -name "libmysqlclient.18.dylib"
/usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib
sudo cp /usr/local/mysql-5.6.24-osx10.8-x86_64/lib/libmysqlclient.18.dylib /usr/lib
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。