File size: 1,754 Bytes
72e766e |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import sqlite3
import sys
# Connect to local DB (or create it)
conn = sqlite3.connect('data.db')
c = conn.cursor()
# Create table if not exists
c.execute('''
CREATE TABLE IF NOT EXISTS records (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
phone TEXT
)
''')
conn.commit()
# Functions
def add_data():
name = input("Enter Name: ")
age = input("Enter Age: ")
phone = input("Enter Phone Number: ")
c.execute("INSERT INTO records (name, age, phone) VALUES (?, ?, ?)", (name, age, phone))
conn.commit()
print("[β] Data added successfully.")
def view_data():
c.execute("SELECT * FROM records")
rows = c.fetchall()
if not rows:
print("No data found.")
for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Phone: {row[3]}")
def search_data():
search_term = input("Enter name to search: ")
c.execute("SELECT * FROM records WHERE name LIKE ?", ('%' + search_term + '%',))
rows = c.fetchall()
if rows:
for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Phone: {row[3]}")
else:
print("No match found.")
def help_menu():
print("\nOffline Data Entry Tool (CMD-based)")
print("Commands:")
print(" python main.py add β Add new record")
print(" python main.py view β View all records")
print(" python main.py search β Search by name")
print(" python main.py help β Show this help\n")
# Main logic
if len(sys.argv) < 2:
help_menu()
elif sys.argv[1] == "add":
add_data()
elif sys.argv[1] == "view":
view_data()
elif sys.argv[1] == "search":
search_data()
else:
help_menu()
conn.close()
|