學習python-mysql:連線資料的步驟(一)
阿新 • • 發佈:2018-12-14
1.匯入
import MySQLdb
2.定義實用類和插入語句並測試
class MysqlPipeline(object): def __init__(self): # self.conn = MySQLdb.connect('host', 'user', 'password', 'dbname', charset='utf8', user_unicode=True) # 下面連線的密碼啥的要換成自己電腦上的登入名和密碼 self.conn = MySQLdb.connect('host', 'user', 'password', 'dbname', charset='utf8', use_unicode=True) self.cursor = self.conn.cursor() def process_item(self, item, spider): # title, url, create_date, url_object_id, front_image_url, # front_image_path, praise_nums, comment_nums, fav_nums, tags, content insert_sql = """ insert into jobbole_article(title, url, create_date, fav_nums) VALUES (%s, %s, %s, %s) """ self.cursor.execute(insert_sql, (item['title'], item['url'], item['create_date'], item['fav_nums'])) self.conn.commit()
中間犯得錯
①.use_unicode=True中的use_unicode寫成了user_unicode