pyspider爬取数据存入mysql--2.测试数据库能否连通

做一个简单的测试,看数据能否存入mysql

 1 #!/usr/bin/env python
 2 # -*- encoding: utf-8 -*-
 3 # Created on 2017-10-26 16:03:21
 4 # Project: mysql_test
 5 
 6 from pyspider.libs.base_handler import *
 7 import MySQLdb
 8 
 9 class Handler(BaseHandler):
10     crawl_config = {
11     }
12 
13     def __init__(self):
14         self.db = MySQLdb.connect(ip, name, password, tableName, charset='utf8')
15 
16     @every(minutes=24 * 60)
17     def on_start(self):
18         try:
19             cursor = self.db.cursor()
20             sql = "insert into user(userName, userAge, userAddress) values ('pyspider',1,'园子')"
21             cursor.execute(sql)
22             cursor.close()
23             self.db.commit()
24             self.db.close()
25         except Exception, e:
26             print e
27             self.db.rollback()

 user表中已经插入了记录:

posted @ 2017-10-26 16:43  大明湖畔的守望者  阅读(1393)  评论(1编辑  收藏  举报