python - Python 和 MySQL 连接
问题描述
我知道如何连接 python 脚本和 MySQL 数据库,但是当数据库不运行时我不能这样做,所以我需要首先运行我的数据库。主要问题是如何从 python 脚本运行我的数据库?通常我从 MySQL Workbench 运行它,但我希望它使用我的 GUI 类的init方法运行。我正在使用 mysql.connector 模块进行连接。
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "root",
passwd = "12121212",
database = 'mydb',
auth_plugin = "mysql_native_password"
)
cursor = mydb.cursor()
cursor.execute("select * from tab")
results = cursor.fetchall()
print(results)
解决方案
看看这个,我希望它是你需要的!
https://codetheory.in/how-to-write-a-python-script-which-communicates-with-the-mysql-server/
推荐阅读
- scala - 删除播放缓存中的单个项目
- qt - qtcreator设计模式是什么类型的qWidget?
- astronomy - 从天空中的(方位角,仰角)查找(ra,dec)
- c# - typeof 泛型和强制类型
- python - Python。我无法使用 int 将数字从 txt 文件转换为验证器
- android - 在颤动中关闭设备后退按钮上的应用程序
- spring-integration - Spring 集成 InboundAdapter/OutboundAdapter 流程
- vue.js - 带有 vuetify 的登录页面
- odoo - 加载合作伙伴、父母和所有孩子的发票
- gcc - objcopy 正在删除一个部分,除非我在该部分中声明一个静态 volatile 变量(使用属性)