Python MySQL 两种方式连接数据库pymysql/sqlalchemy连接mysql数据库

pymysql

import pymysql

host = "127.0.0.1:3306"
user = "root"
password = "root"
database = "test"
mysql_db = pymysql.connect(host=host,
                                user=user,
                                password=password,
                                database=database,
                                charset="utf8")
mysql_cursor = mysql_db.cursor()
mysql_cursor.execute(sql)

sqlalchemy

from sqlalchemy import create_engine

host = "127.0.0.1:3306"
user = "root"
password = "root"
database = "test"

mysql_engine = create_engine(f"mysql+pymysql://{user}:{password}@{host}/{database}")
mysql_engine.execute(sql)

你可能感兴趣的:(python,数据库,mysql,sql)