Python操作Mysql详解

MySQL基础知识

(一)基本数据库操作语句、三大列类型

(二)where / having / group by / order by / limit 简单查询

(三)子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)

(四)函数、视图

(五)数据库优化理论篇

(六)查询优化技术

(七)查询性能优化 深入理解MySql如何执行查询

 

我们使用pymysql模块操作MySQL数据库

一、pymysql安装与使用

(一)pymysql安装

pip install pymysql

(二)pymysql方法介绍

1.Python中数据库操作流程:

 

2.pymysql常用方法如下表所示:

 

3.小结:

准备工作:

#创建公司数据库,编码格式utf-8, COLLATE utf8_general_ci:数据库校对规则。
##ci是case insensitive的缩写,意思是大小写不敏感;相对的是cs,即case sensitive,大小写敏感;
##还有一种是utf8_bin,是将字符串中的每一个字符用二进制数据存储,区分大小写。
create database company_db default character set utf8 collate utf8_general_ci;

#选择数据库:
use  company_db;

#创建一张员工信息表:姓名,工号, 职位,薪资,等级
create table employee(
id int primary key auto_increment,
name char(32),
jobnum int,
position char(16),
salary int,
level int
)default charset=utf8;

#查看当前库中表
show tables;

#员工表中插入一条信息:
#插入一条信息:
insert into employee(name, jobnum, position, salary, level) VALUES('sun', 9000, 'software_eg', 20000, 12);

#查询所有员工信息:
select * from employee;

(一)插入数据

1)插入一条数据:

#-*-coding:utf8 -*-
import pymysql

#1.参数依次为:数据库地址,用户名,密码,库名
db = pymysql.connect("localhost", "root", "root", "company_db")

#2.获取游标
cursor = db.cursor()
#3.编写sql语句 sql = "insert into employee(name,jobnum,position,salary,level) values('li',9001,'tester',18000,13)" #4.执行sql语句 cursor.execute(sql) #5.提交数据 db.commit() #6.断开连接 cursor.close() db.close()

2)多次插入数据,可以使用executemany:

#-*-coding:utf8 -*-
import pymysql

#1.参数依次为:数据库地址,用户名,密码,库名
db = pymysql.connect(host="127.0.0.1", user='root', password='root', database='company_db')

#2.获取游标
cursor = db.cursor()

#3.准备数据并编写sql语句
values=[['gao', 9003, 'hardware_eg', 15000, 10],['qian', 9004, 'software_eg', 2000, 12]]
sql = "insert into employee(name,jobnum,position,salary,level) values(%s,%s,%s,%s,%s)"

#4.执行sql语句
cursor.executemany(sql, values)

#5.提交数据
db.commit()

#6.断开连接
cursor.close()
db.close()

3)一次插入多条数据:

#-*-coding:utf8 -*-
import pymysql

#1.参数依次为:数据库地址,用户名,密码,库名
db = pymysql.connect(host="127.0.0.1", user='root', password='root', database='company_db')

#2.获取游标
cursor = db.cursor()

#3.准备数据并编写sql语句
values=[['test1', 9005, 'hardware_eg', 15000, 10],['test2', 9006, 'software_eg', 2000, 12]]
sql = "insert into employee(name,jobnum,position,salary,level) values"

for item in values:
    if values.index(item) == len(values)-1:
        sql = sql + "(%s)"%str(item)[1:-1]
    else:
        sql = sql + "(%s)"%str(item)[1:-1] + ','
print(sql)

#4.执行sql语句
cursor.execute(sql)

#5.提交数据
db.commit()

#6.断开连接
cursor.close()
db.close()

(二)查询数据

 1)读取所有员工信息:

import pymysql
db = pymysql.connect("localhost", "root", "root", "company_db")
cursor = db.cursor()
sql = "select * from employee"
cursor.execute(sql)
#返回当前游标之后一条记录
data = cursor.fetchone()
print(data)
#返回当前游标之后多条记录
data = cursor.fetchmany(2)
print(data)
#返回当前游标之后的全部记录
data = cursor.fetchall()
for record in data:
    print(record)
cursor.close()
db.close()

2)条件查询:

import pymysql
db = pymysql.connect("localhost", "root", "root", "company_db")
cursor = db.cursor()
sql = "select * from employee where level >11"
cursor.execute(sql)
data = cursor.fetchall()
for record in data:
    print(record)
cursor.close()
db.close()

二、练习

(一)将csv文件导入MySQL数据库

(二)在sql中查询数据

 

posted @ 2019-04-24 11:05  n0page404  阅读(150)  评论(0)    收藏  举报