avatar

Flask学习教程三

pymysql

pip install pymysql
# 导入pymysql模块
import pymysql

# 连接database
conn = pymysql.connect(
host=“你的数据库地址”,
user=“用户名”,password=“密码”,
database=“数据库名”,
charset=“utf8”)

# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
#cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

# 定义要执行的SQL语句
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8; #注意:charset='utf8' 不能写成utf-8
"""

# 执行SQL语句
cursor.execute(sql)

# 关闭光标对象
cursor.close()

# 关闭数据库连接
conn.close()

bootstrap入门和flask_nav快速导航栏

转载自:https://www.cnblogs.com/wongbingming/p/6813221.html

Author: Michelle19l
Link: https://gitee.com/michelle19l/michelle19l/2020/04/24/Flask教程三/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
    微信
  • 支付寶
    支付寶