- 数据库设置yinanjinying.com
首先,我们需要设置SQLite数据库来存储游戏信息。
sql
CREATE TABLE games (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
price REAL NOT NULL
);
INSERT INTO games (name, description, price) VALUES
(‘Cyberpunk 2077’, ‘An open-world, action-adventure story set in Night City, a megalopolis obsessed with power, glamour and body modification.’, 59.99),
(‘The Witcher 3: Wild Hunt’, ‘A story-driven, next-generation open world RPG set in a visually stunning fantasy universe.’, 39.99),
(‘Minecraft’, ‘A game about placing blocks and going on adventures.’, 26.95);
2. 后端(Python + Flask)
接下来,我们创建Flask应用来处理HTTP请求和数据库交互。
python
from flask import Flask, render_template, jsonify, request
import sqlite3
app = Flask(name)
DATABASE = ‘game_store.db’
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
@app.route(‘/’)
def index():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM games’)
games = cur.fetchall()
conn.close()
return render_template(‘index.html’, games=games)
API 示例(可选)
@app.route(‘/api/games’, methods=[‘GET’])
def get_games():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM games’)
games = [dict(game) for game in cur.fetchall()]
conn.close()
return jsonify(games)
if name == ‘main’:
app.run(debug=True)
3. 前端(HTML + CSS)
最后,创建HTML模板来显示游戏列表。
html
Game Store
{% for game in games %} {% endfor %}ID | Name | Description | Price |
---|---|---|---|
{{ game.id }} | {{ game.name }} | {{ game.description }} | ${{ game.price }} |
- 数据库设置
首先,我们需要设置SQLite数据库来存储游戏信息。
sql
CREATE TABLE games (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
price REAL NOT NULL
);
INSERT INTO games (name, description, price) VALUES
(‘Cyberpunk 2077’, ‘An open-world, action-adventure story set in Night City, a megalopolis obsessed with power, glamour and body modification.’, 59.99),
(‘The Witcher 3: Wild Hunt’, ‘A story-driven, next-generation open world RPG set in a visually stunning fantasy universe.’, 39.99),
(‘Minecraft’, ‘A game about placing blocks and going on adventures.’, 26.95);
2. 后端(Python + Flask)
接下来,我们创建Flask应用来处理HTTP请求和数据库交互。
python
from flask import Flask, render_template, jsonify, request
import sqlite3
app = Flask(name)
DATABASE = ‘game_store.db’
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
@app.route(‘/’)
def index():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM games’)
games = cur.fetchall()
conn.close()
return render_template(‘index.html’, games=games)
API 示例(可选)
@app.route(‘/api/games’, methods=[‘GET’])
def get_games():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM games’)
games = [dict(game) for game in cur.fetchall()]
conn.close()
return jsonify(games)
if name == ‘main’:
app.run(debug=True)
3. 前端(HTML + CSS)
最后,创建HTML模板来显示游戏列表。
html
Game Store
{% for game in games %} {% endfor %}ID | Name | Description | Price |
---|---|---|---|
{{ game.id }} | {{ game.name }} | {{ game.description }} | ${{ game.price }} |