创建数据库以及数据表并添加数据
实现效果
代码实现
import sqlite3
DATABASE = 'D:/soft/SQLite/test.db'
app = Flask(__name__)
# 引入bootstrap
# bootstrap = Bootstrap(app)
# __name__是类,区别于app这个实例,app继承类的所有属性和方法
@app.route('/')
def index():
return ("Hi,Flask!"
)
"""获取数据表数据"""
@app.route("/db/students")
def getUsers():
try:
uListStr = ""
sqliteDB = sqlite3.connect(DATABASE)
cur = sqliteDB.execute("select * from students")
print("数据库连接成功!")
for row in cur.fetchall():
uListStr += str(row[0]) + 'has the id' + str(row[2])
print(row) # 数据库中数据
sqliteDB.close()
return uListStr
except Exception as err:
return err