參考:
https://www.cnblogs.com/woider/p/5926744.html
https://www.cnblogs.com/mooba/p/6484357.html
http://www.runoob.com/python3/python3-mysql.html
PyMySQL 是在 Python3.x 版本中用於連接 MySQL 服務器的一個庫,Python2中則使用mysqldb。
通常使用的方法包括:連接、建表、增刪查改、事務處理。
# -*- coding: utf-8 -*-
import pymysql
import pymysql.cursors
connect = pymysql.Connect(host='', # 遠程主機的IP地址
user='', # MySQL用戶名
db='', # 數據庫名字
passwd='', # 數據庫密碼
port=3306, # 數據庫監聽端口,默認3306
charset='utf8',) # 指定編碼
# 創建遊標對象
cursor = connect.cursor()
# 使用SQL語句
sql = ""
try:
cursor.execute(sql) # 執行SQL語句
# db.commit() # 若修改數據需要執行此操作
data = cursor.fetchall() # 獲取cursor得到的數據
# for row in data:
except:
connect.rollback() # 若發生錯誤則回滾
# 關閉連接
cursor.close()
connect.close()
sql語句備註:SELECT message FROM fcomment;
ALTER TABLE fcomment ADD column sentiment VARCHAR(10) AFTER message;
UPDATE fcomment SET sentiment = '%s' WHERE id='" + i + "';
將Dataftame寫入MySQL數據庫