MySQL安装及配置、+python代码实战
1、MySQL安装和配置
(1)、 下载(https://dev.mysql.com/downloads/windows/installer/5.7.html):
(在网上找安装教程https://blog.csdn.net/weixin_43189346/article/details/88595292)
ps:如果在安装的过程中发现已存在的多个mysql的服务器,再命令提示符里面输入sc delete 服务器名回车,就可以删除没有用的服务器
(2)、配置环境:此电脑鼠标右键属性——>高级系统设置——>环境变量——>
2、使用MySQL
安装插件:pip3 install pymysql
从cmd进入mysql命令:mysql -h localhost -u root -p
基础的MySQL语句
创建一个testDev的表:create database testDev;
进入:use testDev ;
查看表:show tables;
查看表数据:select * from user;
删除出表id为1的数据:delete from user where id=1;
修改表中id为1的first_name为Liu的数据:update user set first_name=Liu where id=1
3、实战:
插入单条语句:
import pymysql def insertOne(): '''插入单条语句(数据库插入单条数据)''' try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: #创建游标 cur=conn.cursor() sql='insert into user values(%s,%s,%s,%s,%s,%s)' params=(1,'Liu','her name',18,'girl',2000) cur.execute(sql,params) conn.commit() finally: cur.close() conn.close() insertOne()
数据库表:
插入多条语句:
import ptmysql def insertMany():
'''插入多条语句''' try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: #创建游标 cur=conn.cursor() #创建数据 sql='insert into user values(%s,%s,%s,%s,%s,%s)' params=[ (2,'hi','her name',18,'girl',2000), (3, 'hi','her name', 18,'girls', 2000) ] cur.executemany(sql,params) #写入数据的时候需要加,读取数据的时候不需要写 conn.commit() finally: cur.close() conn.close()
insertMany()
数据库表:
查询多条语句:
import pymysql def queryMany():
'''查询多条语句''' try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: # 创建游标 cur = conn.cursor() sql='select * from user;' cur.execute(sql) data=cur.fetchall() for item in data: print(item) finally: cur.close() conn.close() # queryMany()
查询单条语句:
import pymysql def queryOne():
'''查询单条语句''' try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: # 创建游标 cur = conn.cursor() sql='select * from user where id=%s;' params=(2,) cur.execute(sql,params) data=cur.fetchall() for item in data: print(item) finally: cur.close() conn.close() queryOne()
修改语句:
improt pymysql def updateOne(): try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: # 创建游标 cur = conn.cursor() sql='update user set first_name=%s where id=%s;' params=('Liu',1) cur.execute(sql,params) conn.commit() finally: cur.close() conn.close() ##执行 updateOne()
删除语句:
import pymysql def deleteOne(): try: conn = pymysql.connect(host='127.0.0.1', user='root', password='root', db='testDev') except Exception as e: raise e.args else: # 创建游标 cur = conn.cursor() sql='delete from user where id=%s;' params=(2,) cur.execute(sql,params) conn.commit() finally: cur.close() conn.close() deleteOne()
(1)、将地址、用户名、密码、表名都分离成Json文件:
db.json文件:
{ "database": { "host": "127.0.0.1", "user": "root", "password": "root", "db": "testDev" } }
pathutils.py文件
import os '''获取当前的工程路径''' def base_dir(): #os.path.dirname()去掉脚本的文件名,返回目录 return os.path.dirname(os.path.dirname(__file__)) def filePath(directory='datas',fileName=None): '''找到具体的文件路径''' return os.path.join(base_dir(),directory,fileName)
jsonutils.py文件
import json from utils.pathUtils import base_dir,filePath import os def readJson(): return json.load(open(filePath(fileName='db.json'), encoding='utf-8')) #调试,输出readJson() # print(readJson())
db操作.py
improt pymysql from utils.jsonUtils import readJson def insertMany(): try: conn = pymysql.connect( host=readJson()['database']['host'], user=readJson()['database']['user'], password=readJson()['database']['password'], db=readJson()['database']['db']) except Exception as e: raise e.args else: #创建游标 cur=conn.cursor() #创建数据 sql='insert into user values(%s,%s,%s,%s,%s,%s)' params=[ (4,'hi','her name',18,'girl',2000), (5, 'hi','her name', 18,'girls', 2000) ] cur.executemany(sql,params) #写入数据的时候需要加,读取数据的时候不需要写 conn.commit() finally: cur.close() conn.close() insertMany()
(2)、将地址、用户名、密码、表名都分离成Yaml文件:
db.yaml文件
database: host: 127.0.0.1 user: root password: root #如果密码是int类型需要加引号,假如密码是123,这时候password: "123" db: testDev
pathutils.py文件
import os '''获取当前的工程路径''' def base_dir(): #os.path.dirname()去掉脚本的文件名,返回目录 return os.path.dirname(os.path.dirname(__file__)) def filePath(directory='datas',fileName=None): '''找到具体的文件路径''' return os.path.join(base_dir(),directory,fileName)
yamlutils.py文件
import yaml
from utils.pathUtils import base_dir,filePath
import os
def readYaml():
'''读取yaml文件里面的内容'''
return yaml.load(open(filePath(fileName='db.yaml'),encoding='utf-8'))
# print(readYaml())
db.py
def insertMany(): try: conn = pymysql.connect( host=readYaml()['database']['host'], user=readYaml()['database']['user'], password=readYaml()['database']['password'], db=readYaml()['database']['db']) except Exception as e: raise e.args else: #创建游标 cur=conn.cursor() #创建数据 sql='insert into user values(%s,%s,%s,%s,%s,%s)' params=[ (6,'hi','her name',18,'girl',2000), (7, 'hi','her name', 18,'girls', 2000) ] cur.executemany(sql,params) #写入数据的时候需要加,读取数据的时候不需要写 conn.commit() finally: cur.close() conn.close() insertMany()