標籤:
sudo apt-get install python-mysqldb
#!/usr/bin/python#-*-coding:utf-8-*-‘‘‘This file include all the common routine,that are needed inthe crawler project.Author: Justnzhang @([email protected])Time:2014年7月28日15:03:44‘‘‘import osimport sysimport MySQLdbfrom urllib import quote, unquoteimport uuidreload(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;
ubuntu 安裝python mysqldb