Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# Connect to local DB (or create it)
|
| 5 |
+
conn = sqlite3.connect('data.db')
|
| 6 |
+
c = conn.cursor()
|
| 7 |
+
|
| 8 |
+
# Create table if not exists
|
| 9 |
+
c.execute('''
|
| 10 |
+
CREATE TABLE IF NOT EXISTS records (
|
| 11 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 12 |
+
name TEXT NOT NULL,
|
| 13 |
+
age INTEGER,
|
| 14 |
+
phone TEXT
|
| 15 |
+
)
|
| 16 |
+
''')
|
| 17 |
+
conn.commit()
|
| 18 |
+
|
| 19 |
+
# Functions
|
| 20 |
+
def add_data():
|
| 21 |
+
name = input("Enter Name: ")
|
| 22 |
+
age = input("Enter Age: ")
|
| 23 |
+
phone = input("Enter Phone Number: ")
|
| 24 |
+
|
| 25 |
+
c.execute("INSERT INTO records (name, age, phone) VALUES (?, ?, ?)", (name, age, phone))
|
| 26 |
+
conn.commit()
|
| 27 |
+
print("[✔] Data added successfully.")
|
| 28 |
+
|
| 29 |
+
def view_data():
|
| 30 |
+
c.execute("SELECT * FROM records")
|
| 31 |
+
rows = c.fetchall()
|
| 32 |
+
if not rows:
|
| 33 |
+
print("No data found.")
|
| 34 |
+
for row in rows:
|
| 35 |
+
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Phone: {row[3]}")
|
| 36 |
+
|
| 37 |
+
def search_data():
|
| 38 |
+
search_term = input("Enter name to search: ")
|
| 39 |
+
c.execute("SELECT * FROM records WHERE name LIKE ?", ('%' + search_term + '%',))
|
| 40 |
+
rows = c.fetchall()
|
| 41 |
+
if rows:
|
| 42 |
+
for row in rows:
|
| 43 |
+
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Phone: {row[3]}")
|
| 44 |
+
else:
|
| 45 |
+
print("No match found.")
|
| 46 |
+
|
| 47 |
+
def help_menu():
|
| 48 |
+
print("\nOffline Data Entry Tool (CMD-based)")
|
| 49 |
+
print("Commands:")
|
| 50 |
+
print(" python main.py add → Add new record")
|
| 51 |
+
print(" python main.py view → View all records")
|
| 52 |
+
print(" python main.py search → Search by name")
|
| 53 |
+
print(" python main.py help → Show this help\n")
|
| 54 |
+
|
| 55 |
+
# Main logic
|
| 56 |
+
if len(sys.argv) < 2:
|
| 57 |
+
help_menu()
|
| 58 |
+
elif sys.argv[1] == "add":
|
| 59 |
+
add_data()
|
| 60 |
+
elif sys.argv[1] == "view":
|
| 61 |
+
view_data()
|
| 62 |
+
elif sys.argv[1] == "search":
|
| 63 |
+
search_data()
|
| 64 |
+
else:
|
| 65 |
+
help_menu()
|
| 66 |
+
|
| 67 |
+
conn.close()
|