Flask 第六话之数据库 -- SQLalchemy

一、sqlalchemy原生语句操作

from sqlalchemy import create_engine

HOSTNAME = '127.0.0.1'
PORT = 3306
DATABASE = 'demo'
USERNAME = 'root'
PASSWORD = '123456'

# 链接数据库的字符串,固定格式
DB_URL = "mysql+pymysql://{username}:{password}@{host}:{port}/{db}?charset=utf8".format(
    username = USERNAME,
    password = PASSWORD,
    host = HOSTNAME,
    port = PORT,
    db = DATABASE,
)

engine = create_engine(DB_URL)
# 判断是否链接成功
conn = engine.connect()

result = conn.execute('SELECT * FROM shop_area')

print(result.fetchone())

 

上一篇:安装 CentOS7


下一篇:python 获取本机ip