- 数据库设计(SQL)yuhaosujiaofirst.com
首先,我们设计一个基本的数据库来存储游戏信息。
sql
CREATE TABLE Games (
game_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL
);
INSERT INTO Games (title, description, price, stock) VALUES
(‘Game 1’, ‘Description of Game 1’, 9.99, 100),
(‘Game 2’, ‘Description of Game 2’, 14.99, 50);
2. 后端服务(Python with Flask)
使用Flask框架创建一个简单的RESTful API。
python
from flask import Flask, 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(‘/games’, methods=[‘GET’])
def get_games():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM Games’)
games = cur.fetchall()
conn.close()
return jsonify([dict(ix) for ix in games])
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM Games WHERE game_id = ?’, (game_id,))
game = cur.fetchone()
conn.close()
if game:
return jsonify(dict(game))
else:
return jsonify({‘error’: ‘Game not found’}), 404
启动Flask应用
if name == ‘main’:
app.run(debug=True)
3. 前端界面(HTML + JavaScript with Fetch API)
这里仅展示一个HTML页面和JavaScript代码片段,用于从API获取游戏列表。
html
Game Store
<script>
fetch('/games')
.then(response => response.json())
.then(data => {
const gamesList = document.getElementById('games-list');
data.forEach(game => {
const listItem = document.createElement('li');
listItem.textContent = `${game.title} -
$$
{game.price}`;
gamesList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error:’, error));
- 数据库设计(SQL)
首先,我们设计一个基本的数据库来存储游戏信息。
sql
CREATE TABLE Games (
game_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL
);
INSERT INTO Games (title, description, price, stock) VALUES
(‘Game 1’, ‘Description of Game 1’, 9.99, 100),
(‘Game 2’, ‘Description of Game 2’, 14.99, 50);
2. 后端服务(Python with Flask)
使用Flask框架创建一个简单的RESTful API。
python
from flask import Flask, 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(‘/games’, methods=[‘GET’])
def get_games():
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM Games’)
games = cur.fetchall()
conn.close()
return jsonify([dict(ix) for ix in games])
@app.route(‘/games/int:game_id’, methods=[‘GET’])
def get_game(game_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute(‘SELECT * FROM Games WHERE game_id = ?’, (game_id,))
game = cur.fetchone()
conn.close()
if game:
return jsonify(dict(game))
else:
return jsonify({‘error’: ‘Game not found’}), 404
启动Flask应用
if name == ‘main’:
app.run(debug=True)
3. 前端界面(HTML + JavaScript with Fetch API)
这里仅展示一个HTML页面和JavaScript代码片段,用于从API获取游戏列表。
html
Game Store
<script>
fetch('/games')
.then(response => response.json())
.then(data => {
const gamesList = document.getElementById('games-list');
data.forEach(game => {
const listItem = document.createElement('li');
listItem.textContent = `${game.title} -
$$
{game.price}`;
gamesList.appendChild(listItem);
});
})
.catch(error => console.error(‘Error:’, error));