1.mysql下载与安装
https://blog.csdn.net/zhouzezhou/article/details/52446608
上篇博文写的很清楚,就不重复写了
安装过程中遇到的问题:打开cmd,输入“mysql -u root -p”回车,返回'no modual mysql'
原因:环境变量没有配置好
解决方案:配置环境变量时”用户变量”和”系统变量”下的path都添加mysql路径C:\Program Files\MySQL\MySQL Server 5.6
2.数据库基本操作
http://www.runoob.com/python/python-mysql.html
http://www.runoob.com/python3/python3-mysql.html
https://www.ctolib.com/TracyMcgrady6-pymsql_Operation.html
注意:插入一条用cursor.execute(sql),批量插入用cursor.executemany(sql,T)
3.数据批量导入数据库
https://blog.csdn.net/colourless/article/details/41444069
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 18 15:49:27 2019
@author: admin
"""
import pymysql
# 打开数据库连接
#db = pymysql.connect("193.112.61.11","root","sha155","ceshi" )
db = pymysql.connect("localhost","root","lcl123","test" )
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
# 使用预处理语句创建表
sql = """CREATE TABLE EMPLOYEE (
ID INT auto_increment primary key not null,
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
# SQL插入语句的两种方式
# =============================================================================
# sql1 = """INSERT INTO EMPLOYEE(FIRST_NAME,
# LAST_NAME, AGE, SEX, INCOME)
# VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
# =============================================================================
# =============================================================================
# sql1 = "INSERT INTO EMPLOYEE(FIRST_NAME, \
# LAST_NAME, AGE, SEX, INCOME) \
# VALUES (%s, %s, %s, %s, %s)" % \
# ('Mac', 'Mohan', 20, 'M', 2000)
# =============================================================================
#数据库批量插入
sql1 = "INSERT INTO EMPLOYEE(FIRST_NAME, \
LAST_NAME, AGE, SEX, INCOME) \
VALUES (%s, %s, %s, %s, %s)"
T = [['li','xiaoming',31,'M',3000],['li','xiaomin',32,'F',4000],['lin','xiaoli',22,'F',5000]]
try:
# 执行sql语句
cursor.executemany(sql1,T)
#cursor.execute(sql1)
# 提交到数据库执行
db.commit()
except Exception as e:
# 如果发生错误则回滚
print(e)
db.rollback()
# 关闭数据库连接
db.close()
4.设置表主键
https://jingyan.baidu.com/article/0eb457e50dc8ce03f1a905b0.html
ID INT auto_increment primary key not null
5.遇到的问题及解决方案
(1)'numpy.float64' object has no attribute 'translate'
原因:‘numpy.float64’与mysql 的float类型不匹配
解决方案:将插入mysql的数据转化为float类型,或者创建表类型为char,插入数据转化为string类型
(2)(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'precision FLOAT,\n f1score FLOAT)' at line 7")
原因:可能是precision ,f1score与系统命名冲突
解决方案:修改表头名字
(3)插入中文报错
添加红色框中句子即可
6.从数据库获取数据
import pymysql
def read_from_mysql(EnterpriseID):
host = "***"
#port = 3306
user = "***"
password = "***"
dbname='co' + EnterpriseID
db = pymysql.connect(host,user, password, dbname, charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
cursor.execute("ALTER DATABASE `%s` CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'" % dbname)
try:
sql = "select Standard,Similarity from Synonym_table"
cursor.execute(sql)
date = cursor.fetchall()
finally:
db.close()
return date
7.解决数据库Can't connect to MySQL server on 'localhost' (10061)的问题
https://blog.csdn.net/qq_34369025/article/details/77351709
打开任务管理器->点击服务->找到MySQL***,***是你的版本号,比如我是56.然后右击启动服务,稍等片刻重新打开数据库就可以了