pip 安装 mysql-connector-python
# 导入API
import mysql.connector
# 创建数据库
def create_db_sampledb():
# config为数据库的配置信息
config_root = {
"host":"localhost",
"user":"root",
"password":"1234"
} # 创建root用户的连接配置
sql = "Create Database IF Not Exists sampledb CHARSET=utf8 COLLATE=utf8_bin"
try:
conn = mysql.connector.connect(**config_root) #创建连接
cursor = conn.cursor() #设置游标
cursor.execute(sql) #执行sql语言
conn.commit() # 上传
finally:
conn.close() # 关闭连接
# 设置配置信息,模仿用户配置的信息
mysql_config_sampledb = {
"host":"127.0.0.1",
"database":"sampledb",
"user":"root",
"password":"1234"
}
# 创建表
def create_table_sampletb():
# 如果该表不存在,创建该表
sql = (
"Create Table If Not Exists sampletb("
"DateTime DateTime Primary key,"
"open float(32),"
"high float(32),"
"low float(32),"
"close float(32))"
)
try:
conn = mysql.connector.connect(**mysql_config_sampledb)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
finally:
conn.close()
# 如果存在删除该表
def drop_table_sampletb():
sql = "DROP TABLE IF EXISTS sampletb"
try:
conn = mysql.connector.connect(**mysql_config_sampledb)
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
finally:
conn.close()
#插入数据
import pandas as pd
def insert_into_sampletb():
df = pd.read_csv("523-628IF00.csv")
df = df.where(~pd.isnull(df),other=None) # ~表示 非,即保存不是缺失值的数
sql = (
"INSERT INTO sampletb"
"(DateTime,open,high,low,close)"
"VALUES (%s,%s,%s,%s,%s)"
)
try:
conn = mysql.connector.connect(**mysql_config_sampledb)
cursor = conn.cursor()
# cursor.executemany(sql,multiple_data) # 插入多条记录
# cursor.execut(sql,data) # 插入单条纪录,其中data记录值
for row in df.iterrows():
li = row[1].values
cursor.execute(sql,tuple(li)) # 此处为插入单条数据
conn.commit()
finally:
conn.close()
# 查询数据
select_sampletb = "SELECT * FROM sampletb LIMIT 100" # 查询表列名
select_sampletb_columns = "SHOW columns FROM sampletb" #查看数据行数
def select_from_sample(config,sql):
'''
查询语句,输入数据库的连接信息(字典)以及查询语句,
打印查询条数,并返回查询的结果
'''
try:
conn = mysql.connector.connect(**config)
cursor = conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
finally:
conn.close()
return result
result = select_from_sample(mysql_config_sampledb,select_sampletb) # result为关于查询结果的一个list
# 通过查询函数查询列名
select_from_sample(mysql_config_sampledb,select_sampletb_columns)
pip install pymysql
# 导入API
import pymysql
# 创建数据库
def create_datebase_sampledb2():
config_root = {
"host":"localhost",
"user":"root",
"password":"1234"
}
sql = "Create Database If Not Exists sampleddb2 CHARSET=utf8MB4 COLLATE=utf8mb4_bin"
conn = pymysql.connect(**config_root) # 打开数据库连接
try:
with conn.cursor() as cursor: # 使用cursor()方法获取语法
cursor.execute(sql) # 执行SQL
conn.commit() #提交
finally:
conn.close()
# 设置配置信息
pymysql_config_sampledb2={
"host":"127.0.0.1",
"database":"sampleddb2",
"user":"root",
"password":"1234"
}
# 创建表
def create_table_sampletb2():
'''
如果该表不存在,创建该表
'''
sql = (
"Create Table If Not Exists sampletb2("
"DateTime DateTime Primary key,"
"open float(32),"
"high float(32),"
"low float(32),"
"close float(32))"
)
conn = pymysql.connect(**pymysql_config_sampledb2)# 打开连接
try:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
finally:
conn.close()
# 删除表
def drop_table_sampletb2():
'''
如果该表存在,删除该表
'''
sql = "DROP TABLE IF EXISTS sampletb2"
conn = pymysql.connect(**pymysql_config_sampledb2)
try:
with conn.cursor() as cursor:
cursor.execute(sql)
conn.commit()
finally:
conn.close()
# 插入数据
import pandas as pd
def insert_into_sampletb2():
df = pd.read_csv("523-628IF00.csv")
df = df.where(~pd.isnull(df),other=None) # ~表示 非,即保存不是缺失值的数
sql = (
"INSERT INTO sampletb"
"(DateTime,open,high,low,close)"
"VALUES (%s,%s,%s,%s,%s)"
)
conn = pymysql.connect(**pymysql_config_sampledb2)
try:
with conn.cursor() as cursor:
# cursor.executemany(sql,multiple_data) # 插入多条记录
# cursor.execut(sql,data) # 插入单条纪录,其中data记录值
for row in df.iterrows():
li = row[1].values
cursor.execute(sql,tuple(li)) # 此处为插入单条数据
conn.commit()
finally:
conn.close()
其他相关方法和mysql-connector-python中的一样,不再一一重复。
SQLite3 和上面的两种方法不一样,上面的两种方法是通过API连接mysql数据库,但是SQLite3是python自带的一个便捷式数据库,而不是去连接,它本身就是一个小型的mysql。
执行创建语句后,会在本地生成一个.db 文件
#导入入模块
import sqlite3
# 设置一个路径作为数据库的保存路径
sqlite3_path="sqlite_sample.db"
# 创建一个数据库并创建一个表
def create_sqlite_sampletb(db_path):
sql = (
"Create Table If Not Exists sampletb("
"DateTime DateTime Primary key,"
"open float(32),"
"high float(32),"
"low float(32),"
"close float(32))"
)
with sqlite3.connect(db_path) as conn:
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
create_sqlite_sampletb(sqlite3_path)
其他实现数据库的方法,即变换sql语句即可。