fetchone()和fetchone()用法
cs1.fetchone()
cs1.fetchall()
cs1.execute(sql)
爬虫项目代码
注意{}外也有‘’,注意categoryMovieTable无引号,注意value('{}', '{}', '{}', '{}')
fetchone()
fetchall()
class CategoryPipeline(object):
def process_item(self, item, spider):
if spider.name == 'category':
sql = "select count(*) from categoryMovieTable where title='{}'".format(item.get('title'))
sql1 = "insert into categoryMovieTable(category, url, title, source) value('{}', '{}', '{}', '{}')".format(item.get('category'), item.get('url'), item.get('title'), item.get('source'))
put_to_mysql(sql, sql1)
return item
python链接mysql代码
from pymysql import *
def main():
conn = connect(host='localhost',port=3306,database='jing_dong',user='root',password='mysql',charset='utf8')
cs1 = conn.cursor()
count = cs1.execute('insert into goods_cates(name) values("硬盘")')
print(count)
count = cs1.execute('insert into goods_cates(name) values("光盘")')
print(count)
count = cs1.execute('update goods_cates set name="机械硬盘" where name="硬盘"')
count = cs1.execute('delete from goods_cates where id=6')
conn.commit()
count = cs1.execute('select id,name from goods where id>=4')
cs1.close()
conn.close()
if __name__ == '__main__':
main()