python-mysql:设置数据类型初探
时间:2021-07-01 10:21:17
帮助过:27人阅读
import pymysql
2 import pandas
as pd
3
4 dbcon = pymysql.connect(host=
‘localhost‘,
5 port=
3306,
6 user=
‘root‘,
7 passwd=
‘1234‘,
8 db=
‘model_database‘
9 )
10 cursor =
dbcon.cursor()
11 data = pd.read_csv(r
‘C:\Users\zhangjingyu\PycharmProjects\MyModels\knnCluster\data.csv‘)
12
13 sql1 =
‘DROP TABLE IF EXISTS cluster_data‘
14 cursor.execute(sql1)
15 sql2 =
‘CREATE TABLE cluster_data(x FLOAT (8,6),y VARCHAR (20))‘
16 cursor.execute(sql2)
17
18 data_mysql =
[]
19
20 data_mysql = [(
float(data.iloc[i,
0]),
float(data.iloc[i,
1]))
for i
in range(data.shape[
0])]
21 cursor.executemany(
‘INSERT INTO cluster_data VALUES(%s,%s)‘, data_mysql)
22 try:
23 dbcon.commit()
24 print(
‘success written!‘)
25 except:
26 dbcon.rollback()
27 print(
‘failed written!‘)
28 finally:
29 cursor.close()
30 dbcon.close()
View Code
结果如下:
实在有必要执行其它操作,倒建议直接在SQL命令里写,自带SQL语句的补全功能。
python-mysql:设置数据类型初探
标签:into arch pymysql 自带 open arm das cursor zhang