-
Notifications
You must be signed in to change notification settings - Fork 1
/
sat_cmysql.py
110 lines (90 loc) · 2.93 KB
/
sat_cmysql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#coding:utf-8
#sys.setdefaultencoding('utf-8')
debug = False
#debug = True
if __name__ == '__main__':
debug = True
else:
debug = False
import platform
import sys, os
user = 'root'
pwd = '654321'
host = '127.0.0.1'
db = 'sat'
cnx = None
cursor = None
errMysql = None
localplatform = platform.platform()
if localplatform[:5] == 'Linux': #为了windown os 不存在mysql情况
import mysql
import mysql.connector
cnx = mysql.connector.connect(user=user, password=pwd, host=host, database=db, charset='utf8')
cursor = cnx.cursor()
errMysql = mysql.connector.Error
def ConClose():
if cnx != None and cursor != None:
cursor.close()
cnx.close()
def Insert(inHead, ldata):
try:
if cursor != None:
cursor.execute(inHead, ldata)
cursor.execute('commit')
except errMysql:
print(inHead)
print("Error: {}".format(errMysql.msg))
def Select(cmd):
try:
if cursor != None:
cursor.execute(cmd)
except errMysql:
print(cmd)
print("Error: {}".format(errMysql.msg))
if __name__ == '__main__':
print('test mysql connect', cnx, cursor)
ConClose()
"""
in_price = "insert into t_price(code,tradedate,OpeningPrice,Transactionprice,HighestPrice,LowestPrice,Bidprice,Offerprice,Closingprice,Changes,PriceChange,Volumes,Turnover,TotalMarket,Items,EscrowShares,CirculationShares,Indate,F001,F002,F003,F004,F007,F008) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
insert_sql = "INSERT INTO t_stock_code(code, name, indate) VALUES ('000002.SZ', '万科A', '2014-11-30')"
select_sql = "SELECT * FROM t_stock"
try:
cursor.execute(in_price,(value1,value2,value3))
cursor.execute(select_sql)
data = cursor.fetchall()
for i in data:
print(i)
except mysql.connector.Error as err:
print("create table 'mytable' failed.")
print("Error: {}".format(err.msg))
sys.exit()
try:
cursor.execute(insert_sql)
except mysql.connector.Error as err:
print("insert table 't_stock_code' failed.")
print("Error: {}".format(err.msg))
sys.exit()
if os.path.exists(data_file):
myfile = open(data_file)
lines = myfile.readlines()
myfile.close()
for line in lines:
myset = line.split()
sql = "INSERT INTO t_stock_code (id, code, name) VALUES ('{}', {}, {})".format(myset[0], myset[1], myset[2])
try:
cursor.execute(sql)
except mysql.connector.Error as err:
print("insert table 't_stock_code' from file 'mysql-test.dat' -- failed.")
print("Error: {}".format(err.msg))
sys.exit()
try:
cursor.execute(select_sql)
for (id, code, name) in cursor:
print("id:{} code:{} name:{}".format(id, code, name))
except mysql.connector.Error as err:
print("query table 't_stock_code' failed.")
print("Error: {}".format(err.msg))
sys.exit()
"""