Shrey0405 commited on
Commit
e71e0a6
·
verified ·
1 Parent(s): e5a93fe

Create database.py

Browse files
Files changed (1) hide show
  1. database.py +32 -0
database.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ # connect to database (creates file automatically)
4
+ conn = sqlite3.connect("users.db", check_same_thread=False)
5
+ cursor = conn.cursor()
6
+
7
+ # create table
8
+ cursor.execute("""
9
+ CREATE TABLE IF NOT EXISTS users (
10
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
11
+ email TEXT UNIQUE,
12
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
13
+ )
14
+ """)
15
+
16
+ conn.commit()
17
+
18
+
19
+ # insert new user
20
+ def add_user(email):
21
+ try:
22
+ cursor.execute("INSERT INTO users (email) VALUES (?)", (email,))
23
+ conn.commit()
24
+ return True
25
+ except:
26
+ return False
27
+
28
+
29
+ # check if user exists
30
+ def user_exists(email):
31
+ cursor.execute("SELECT * FROM users WHERE email = ?", (email,))
32
+ return cursor.fetchone()