scrapy怎么连接mysql

scrapy怎么连接mysql,第1张

1. 首先获取相关配置信息,这个在初始化时,从配置文件中解析而来

2. 新建了一个handler

3. 做了执行statement之前的准备工作。看看准备了些什么,跟踪代码,最后进入了DataSource类的doGetConnection方法,该方法做如下 *** 作:

private Connection doGetConnection(Properties properties) throws SQLException {

initializeDriver()

Connection connection = DriverManager.getConnection(url, properties)

configureConnection(connection)

return connection

}

# -*- coding: utf-8 -*-

#__author__:Mr丶zhang

#引入pymysql库,没有的话,请安装  pip install pymysql

import pymysql

dbObject = pymysql.connect(host='192.168.XX.XX', user='数据库用户名', passwd='数据库密码', charset='utf8',use_unicode=False)

       cursor = dbObject.cursor()

       sql = "SELECT * FROM ch_weather.cityCode"     #sql语句

       try:

           cursor.execute(sql)

           results = cursor.fetchall() #读取数据库信息,结果是一个tuple元祖

           print type(results)

           for i in range(len(results)):

               print results[i] #遍历元祖,得到数据

       except:

           print "Error: unable to fecth data"

结果如图所示:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7478226.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存