sunnyujjawal commited on
Commit
fa2a333
1 Parent(s): 4cb0548

Create dbconnect

Browse files

This program uses the sqlite3 library to connect to an SQLite database called "users.db". If the "users" table does not exist, it will be created with two columns: "username" and "password". The signup function will insert a new row into the table with the entered username and password, and the login function will check if there is a row in the table with the entered username and password.

Note that this example uses placeholder parameters (?) in the SQL queries to avoid SQL injection attacks. You should always use placeholder parameters when constructing SQL queries with user input.

Files changed (1) hide show
  1. dbconnect +43 -0
dbconnect ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+
3
+ # First, we'll create a connection to the database
4
+ conn = sqlite3.connect("users.db")
5
+ cursor = conn.cursor()
6
+
7
+ # Now, we'll create a table to store the users and their corresponding passwords
8
+ cursor.execute("CREATE TABLE IF NOT EXISTS users (username text, password text)")
9
+
10
+ # Now, we'll define a function to handle signup
11
+ def signup():
12
+ # Prompt the user to enter a username and password
13
+ username = input("Enter a username: ")
14
+ password = input("Enter a password: ")
15
+ # Add the username and password to the users table
16
+ cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
17
+ conn.commit()
18
+ print("Signup successful!")
19
+
20
+ # Next, we'll define a function to handle login
21
+ def login():
22
+ # Prompt the user to enter their username and password
23
+ username = input("Enter your username: ")
24
+ password = input("Enter your password: ")
25
+ # Check if the username and password match a set in the users table
26
+ cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
27
+ if cursor.fetchone() is not None:
28
+ print("Login successful!")
29
+ else:
30
+ print("Invalid username or password. Please try again.")
31
+
32
+ # Now, we'll prompt the user to choose between signup and login
33
+ while True:
34
+ choice = input("Would you like to signup or login? (s/l) ")
35
+ if choice == 's':
36
+ signup()
37
+ elif choice == 'l':
38
+ login()
39
+ else:
40
+ print("Invalid choice. Please try again.")
41
+
42
+ # Finally, we'll close the connection to the database
43
+ conn.close()