huylaughmad commited on
Commit
90a3e4d
·
verified ·
1 Parent(s): 4730338

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py CHANGED
@@ -225,6 +225,30 @@ def init_db():
225
  {"url": "https://images.unsplash.com/photo-1588776814546-1ffcf47267a5?ixlib=rb-4.0.3&auto=format&fit=crop&w=1470&q=80", "caption": "Phim X-quang sau"},
226
  {"url": "https://images.unsplash.com/photo-1621841957884-1210fe19daca?ixlib=rb-4.0.3&auto=format&fit=crop&w=1470&q=80", "caption": "Chị Anh hạnh phúc"}
227
  ]),))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  # Gọi hàm init_db khi ứng dụng khởi động
229
  init_db()
230
 
 
225
  {"url": "https://images.unsplash.com/photo-1588776814546-1ffcf47267a5?ixlib=rb-4.0.3&auto=format&fit=crop&w=1470&q=80", "caption": "Phim X-quang sau"},
226
  {"url": "https://images.unsplash.com/photo-1621841957884-1210fe19daca?ixlib=rb-4.0.3&auto=format&fit=crop&w=1470&q=80", "caption": "Chị Anh hạnh phúc"}
227
  ]),))
228
+
229
+ # Tạo bảng comments
230
+ cursor.execute('''
231
+ CREATE TABLE IF NOT EXISTS comments (
232
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
233
+ post_id INTEGER NOT NULL,
234
+ author_name TEXT NOT NULL,
235
+ author_email TEXT NOT NULL,
236
+ content TEXT NOT NULL,
237
+ created_at TEXT DEFAULT (datetime('now')),
238
+ FOREIGN KEY (post_id) REFERENCES posts(id)
239
+ )
240
+ ''')
241
+
242
+ # Chèn dữ liệu mẫu cho comments (nếu bảng trống)
243
+ cursor.execute('SELECT COUNT(*) FROM comments')
244
+ if cursor.fetchone()[0] == 0:
245
+ cursor.execute('''
246
+ INSERT INTO comments (post_id, author_name, author_email, content, created_at) VALUES
247
+ (1, 'Nguyễn Thị Mai', 'mai.nguyen@example.com', 'Bài viết rất hữu ích, tôi đã áp dụng và thấy hiệu quả rõ rệt. Cảm ơn bác sĩ!', '2023-06-13 10:00:00'),
248
+ (1, 'Trần Văn Nam', 'nam.tran@example.com', 'Tôi đang tìm hiểu về dịch vụ này, bài viết giải đáp được nhiều thắc mắc của tôi.', '2023-06-08 14:30:00'),
249
+ (2, 'Lê Thị Hồng', 'hong.le@example.com', 'Cảm ơn bài viết, rất chi tiết và dễ hiểu!', '2023-06-09 09:15:00')
250
+ ''')
251
+
252
  # Gọi hàm init_db khi ứng dụng khởi động
253
  init_db()
254