python - 链接mysql操作mysql代码实例
2023-09-27 14:29:10 时间
import pymysql
from pymysql.cursors import DictCursor
from loguru import logger
# 数据库配置选择
DATABASE_CONFIG = {
"test": {
'user': 'user_name',
'passwd': 'xxxxxx',
'host': '127.0.0.1',
'database': 'test',
'port': 3306
},
"product": {
'user': 'user_name',
'passwd': 'xxxxxx',
'host': '127.0.0.1',
'database': 'product',
'port': 3306
}
}
def inti_mysql(db_conf):
"""
初始化数据库链接
"""
conn = pymysql.connect(host=db_conf['host'],
user=db_conf['user'],
passwd=db_conf['passwd'],
db=db_conf['database'],
port=int(db_conf['port']),
cursorclass=DictCursor)
cursor = conn.cursor()
return conn, cursor
def get_data(db, cursor):
"""
查询
"""
sql = "select id, name, from users;"
cursor.execute(sql)
ret = cursor.fetchall()
return ret
if __name__ == "__main__":
pass
# 链接mysql
data_base_flag = "test"
db_conf = DATABASE_CONFIG[data_base_flag]
db, cursor = inti_mysql(db_conf)
# 操作mysql
res = get_data(db, cursor)
logger.info(f"res = {res}")
相关文章
- Python pprint | 超级好用的Python库,漂亮的打印,让json数据提取体验更好
- python使用插入带有%的字符串到mysql数据库
- python操作db2和mysql ,ibm_db
- python mysql
- python操作MySQL数据库
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- python连接mysql
- python 之 append extend
- Python在mysql中进行操作是十分容易和简洁的
- Python使用MySQL数据库
- MySQLdb in Python: “Can't connect to MySQL server on 'localhost'”
- 【编写环境一】遇到常见python函数处理方式
- 2022.09青少年软件编程(Python)等级考试试卷(二级)
- Python 连接 MySQL 和 Linux 环境下安装 Python3 的操作方法
- mysql-python install error: Cannot open include file 'config-win.h'
- Python 3.7, Failed building wheel for MySql-Python
- python mysql Connect Pool mysql连接池 (201
- How to Access MySQL with Python Version 3.4
- python脚本-导出MySQL数据到excel
- Python +Mysql 安装部署
- Python模块学习遇到的问题
- 为什么Python中sort方法和sorted函数调用废弃使用cmp参数
- 第7.17节 Python类中的静态方法装饰器staticmethod 定义的静态方法深入剖析
- Python中Pandas通过read_sql方法从Mysql或Oracle数据库中读取数据帧(DataFrame)
- python连接mysql
- 一定要看,原来python爬虫这么简单
- 将Python文件发布成DLL并调用
- Python 关于输出
- MySQL-python 安装