概要
在虚拟机安全启动MySQL + Jupyter lab + Conda 服务
在 Jupyter 中进行 PyMySQL 实现SQL语句的开发
重要点:
pandas + pymysql + sqlalchemy
官方文档
Python 字符串 | 菜鸟教程 (runoob.com)
基本使用
pymsql-conn + cursor
import pymysql
import pandas as pd
# 定义连接
connection = pymysql.connect(
host = "localhost",
user = "root",
passwd = "root",
database = "tedu1",
port = 3306
)
# 创建游标cur
cur = connection.cursor()
验证登录成功
cur.execute("SELECT VERSION()")
data = cur.fetchone()
print ("Database version : %s " % data)
cursor + connection
# 执行数据库操作
sql = "SHOW DATABASES"
try:
# 通过游标cur执行sql操作
count = cur.execute(sql)
print(count)
# 逐行输出结果
result = cur.fetchall()
for i in result:
print(i)
# 提交事务 == 执行sql语句,特别是写入或者修改删除操作
connection.commit()
except:
print("Error: unable to execute the sql")
connection.rollback()
# 最终关闭连接, 代码开发的时候一定要有
# finally:
# cur.close()
# connection.close()
execute
# 切换数据库
sql = "use tedu1"
# sql = "CREATE TABLE bm(id int, name varchar(30));"
cur.execute(sql)
# 创建表格
sql = "CREATE TABLE IF NOT EXISTS bm(id int, name varchar(30));"
cur.execute(sql)
# 写入数据并提交
sql = "INSERT INTO tedu1.bm VALUES(%s, %s);"
cur.execute(sql, (1, "OPS_运维部"))
connection.commit() # 必须步骤
executemany
sql = "INSERT INTO tedu1.bm VALUES(%s, %s);"
cur.executemany(sql,
[
(4, "UI"),
(5, "MARKET"),
(6, "TEST")
]
)
connection.commit()
fetch
# fetch 抓取数据的过程会随着游标cur的移动而变化
print(cur.fetchone()) # 1行
# 多行
print(cur.fetchmany(2))
# 全部
print(cur.fetchall())
update
# 单条
sql = "UPDATE tedu1.bm SET name=%s WHERE id=%s;"
cur.execute(sql, ("测试部", 6))
connection.commit()
# 多条
sql = "UPDATE tedu1.bm SET name=%s WHERE id=%s;"
cur.executemany(sql,
[
("Test_测试部", 6), ("Market_市场部", 5)
])
connection.commit()
后台数据查询验证
delete
# 单条
sql = "DELETE FROM tedu1.bm WHERE id = %s;"
cur.execute(sql, 3)
connection.commit()
# 多条
sql = "DELETE FROM tedu1.bm WHERE id = %s;"
cur.executemany(sql, [4,5,6])
connection.commit()
查看数据
pandas + sqlalchemy
优化数据的查看通过使用 Pandas + sqlalchemy
import pandas as pd
import sqlalchemy as sqla
from sqlalchemy import *
db = sqla.create_engine('mysql+pymysql://root:root@localhost/tedu1?charset=utf8')
sql = "SELECT * FROM tedu1.yg"
pd.read_sql(text(sql), con=db.connect(), index_col="ID")
pymysql + prettytable
import pymysql
from prettytable import from_db_cursor
# 创建连接
conn = pymysql.connect(host="localhost",port=3306,user='root',password='root',db='tedu1')
# 创建游标
cur = conn.cursor()
# 执行查询
cur.execute("SELECT * FROM tarena.user LIMIT 5")
# 使用prettytable优化输出
table = from_db_cursor(cur)
print(table)
# 关闭游标 + 连接
cur.close()
conn.close()
pandas + pymysql + IPython
import pymysql
import pandas as pd
from IPython.display import display
# 创建连接
conn = pymysql.connect(host="localhost",port=3306,user='root',password='root',db='tedu1')
# 模式一:
# 执行查询并将结果转化为DF
# pd.read_sql("SELECT * FROM tarena.user LIMIT 5", conn)
# 关闭连接
# 假如关闭连接则没有输出结果
# conn.close()
# 模式二:
# 输出表格
# df = pd.read_sql("SELECT * FROM tarena.user LIMIT 5", conn)
# df
# 关闭连接
# 假如关闭连接则没有输出结果
# conn.close()
# 模式二:
# 输出表格
df = pd.read_sql("SELECT * FROM tarena.user LIMIT 5", conn)
conn.close()
# 关闭连接后任然可以输出
display(df)
cursor.description (不推荐)
import pymysql
# 创建连接
conn = pymysql.connect(host="localhost",port=3306,user='root',password='root',db='tedu1')
# 创建游标
cur = conn.cursor()
# 执行查询
cur.execute("SELECT * FROM tarena.user LIMIT 5")
# 获取查询结果列名
columns = [desc[0] for desc in cur.description]
# 输出列名和数据
print("\t".join(columns))
for row in cur.fetchall():
print("\t".join(str(col) for col in row))
# 关闭游标 + 连接
cur.close()
conn.close()
Example
sql逐步开发汇总代码
# 初始化连接
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='root',
database='tedu1',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# 创建表格
with connection:
with connection.cursor() as cursor:
try:
# Create a new table
sql = """
CREATE TABLE IF NOT EXISTS tedu1.yg (
ID INT,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
AGE INT,
SEX VARCHAR(10),
INCOME FLOAT
);
"""
cursor.execute(sql)
# connection is not autocommit by default. So you must commit to save your changes.
connection.commit()
except:
print("Error: unable to execute the sql scripts about create table")
connection.rollback()
# 写入数据
try:
sql = """
INSERT INTO tedu1.yg (`ID`, `FIRST_NAME`, `LAST_NAME`, `AGE`, `SEX`, `INCOME`)
VALUES (%s, %s, %s, %s, %s, %s)
"""
cursor.executemany(sql, [
(1, 'Mac', 'A', 20, 'M', 20000),
(2, 'Tom', 'B', 20, 'F', 30000),
(3, 'Bob', 'C', 20, 'M', 40000)
])
# connection is not autocommit by default. So you must commit to save your changes.
connection.commit()
except:
print("Error: unable to execute the sql about insert data")
connection.rollback()
# 查询数据
try:
sql = """SELECT * FROM tedu1.yg"""
cursor.execute(sql)
print(cursor.fetchall())
# connection.commit()
except:
print("Error: unable to execute the sql")
connection.rollback()
# 修改数据
try:
sql = """UPDATE tedu1.yg SET INCOME = %s WHERE FIRST_NAME = %s;"""
cursor.execute(sql, (10000, 'Mac'))
connection.commit()
sql = """SELECT * FROM tedu1.yg WHERE FIRST_NAME = %s"""
cursor.execute(sql, 'Mac')
result = cursor.fetchone()
print(result)
except:
print("Error: unable to execute the sql")
connection.rollback()
# 数据删除
try:
sql = """DELETE FROM tedu1.yg WHERE FIRST_NAME = %s;"""
cursor.execute(sql, 'Tom')
connection.commit()
sql = """SELECT * FROM tedu1.yg"""
cursor.execute(sql)
result = cursor.fetchall()
print(result)
except:
print("Error: unable to execute the sql")
connection.rollback()
Juypter lab 落实分步 SQL 语句开发测试
查看数据
connection = pymysql.connect(host='localhost',
user='root',
password='root',
database='tedu1',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM tedu1.yg")
for row in cursor:
print("row = %r" % (row,))
cursor.execute(f"SELECT * FROM tedu1.yg")
row = cursor.fetchone()
while row:
print("%d %s %s %d %s %f" % (row["ID"], row["FIRST_NAME"], row["LAST_NAME"], row["AGE"], row["SEX"], row["INCOME"]))
row = cursor.fetchone()
cursor.close()
connection.close()