yusufgundogdu commited on
Commit
517c2ce
·
verified ·
1 Parent(s): 163e93b

Update shared_data.py

Browse files
Files changed (1) hide show
  1. shared_data.py +28 -7
shared_data.py CHANGED
@@ -1,7 +1,28 @@
1
- shared_data = {
2
- "users": [
3
- {"id": 1, "name": "Ahmet", "score": 85},
4
- {"id": 2, "name": "Mehmet", "score": 92}
5
- ],
6
- "models": ["model1", "model2", "model3"]
7
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ from flask import g
3
+
4
+ DATABASE = 'users.db'
5
+
6
+ def get_db():
7
+ db = getattr(g, '_database', None)
8
+ if db is None:
9
+ db = g._database = sqlite3.connect(DATABASE)
10
+ return db
11
+
12
+ def init_db():
13
+ with app.app_context():
14
+ db = get_db()
15
+ cursor = db.cursor()
16
+ cursor.execute('''
17
+ CREATE TABLE IF NOT EXISTS users (
18
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
19
+ name TEXT NOT NULL,
20
+ score INTEGER DEFAULT 0
21
+ )
22
+ ''')
23
+ # Insert initial data if empty
24
+ cursor.execute('SELECT COUNT(*) FROM users')
25
+ if cursor.fetchone()[0] == 0:
26
+ cursor.executemany('INSERT INTO users (name, score) VALUES (?, ?)',
27
+ [('Ahmet', 85), ('Mehmet', 92)])
28
+ db.commit()