2017年3月21日 星期二

[python] 連接 mysql 測試

安裝mysql python connector:

pip install pymysql



測試程式:


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 21 14:46:19 2017

@author: ghosty

installation:
  pip install pymysql
 
"""

#!/usr/bin/python
    
# 引入 MySQL 模組
import pymysql as mysql
    
# 連接到 MySQL
db = mysql.connect(host="localhost", user="root", passwd="", db="finance")
cursor = db.cursor()
cursor.execute("SET NAMES 'UTF8'")
cursor.execute("SET CHARACTER_SET_RESULTS=UTF8")

TableName = "StockData"

strSQL = "CREATE TABLE IF NOT EXISTS `" \
        + TableName + "` (" \
        + "`Date` DATE NOT NULL ," \
        + "`StockID` DATE NOT NULL ," \
        + "`Open` FLOAT NOT NULL ,"\
        + "`High` FLOAT NOT NULL ,"\
        + "`Low`  FLOAT NOT NULL ,"\
        + "`Close` FLOAT NOT NULL ,"\
        + "`Volume` FLOAT NOT NULL,"\
        + "INDEX (Date, StockID) " \
        + ") ENGINE = MYISAM ;";

cursor.execute(strSQL)                     

db.close()
 

沒有留言:

張貼留言