-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
31 lines (24 loc) · 790 Bytes
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sqlite3
# Set up a connection to the database file
conn = sqlite3.connect('database.db')
# Create a cursor object to execute queries
c = conn.cursor()
# Loop to accept user input and execute queries
while True:
# Get input from the user
command = input("Enter a SQL command (or 'quit' to exit): ")
# Check if the user wants to quit
if command.lower() == 'quit':
break
# Execute the user's command
try:
c.execute(command)
# If the command is a SELECT statement, print the results
if command.upper().startswith('SELECT'):
results = c.fetchall()
for row in results:
print(row)
except Exception as e:
print(f"Error: {e}")
# Close the connection when finished
conn.close()