sudo apt-get install python-mysqldb
#!/usr/bin/python #-*-coding:utf-8-*- ''' This file include all the common routine,that are needed in the crawler project. Author: Justnzhang @(uestczhangchao@qq.com) Time:2014年7月28日15:03:44 ''' import os import sys import MySQLdb from urllib import quote, unquote import uuid reload(sys) sys.setdefaultencoding('utf-8') def insertDB(dictData): print "insertDB" print dictData id = uuid.uuid1() try: conn_local = MySQLdb.connect(host='192.168.30.7',user='xxx',passwd='xxx',db='xxx',port=3306) conn_local.set_character_set('utf8') cur_local = conn_local.cursor() cur_local.execute('SET NAMES utf8;') cur_local.execute('SET CHARACTER SET utf8;') cur_local.execute('SET character_set_connection=utf8;') values = [] # print values values.append("2") values.append("3") values.append("2014-04-11 00:00:00") values.append("2014-04-11 00:00:00") values.append("6") values.append("7") cur_local.execute("insert into health_policy values(NULL,%s,%s,%s,%s,%s,%s)",values) #print "invinsible seperator line-----------------------------------" conn_local.commit() cur_local.close() conn_local.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1]) if __name__ == '__main__': values = [1,2,4] insertDB(values)
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for health_policy -- ---------------------------- DROP TABLE IF EXISTS `health_policy`; CREATE TABLE `health_policy` ( `hid` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(1000) DEFAULT NULL COMMENT '政策标题', `url` varchar(1000) NOT NULL COMMENT '经过MD5加密后的URL', `pub_time` datetime DEFAULT NULL COMMENT '发布时间', `inser_time` datetime NOT NULL COMMENT '插入时间', `website` varchar(1000) DEFAULT NULL COMMENT '来源网站', `content` longtext COMMENT '政策内容', PRIMARY KEY (`hid`) ) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8;