please code a python script that connects to local db and adds a name and a phone no. in…
- User
please code a python script that connects to local db and adds a name and a phone no. in contacts table
Assistant
Certainly! Below is a Python script that connects to a local SQLite database, creates a
contactstable if it doesn't exist, and adds a name and phone number to the table.Requirements:
- SQLite: This script uses SQLite, which is included with Python by default.
- SQLite3 module: This is part of the Python standard library, so no additional installations are required.
Script:
import sqlite3 def connect_to_db(db_name: str) -> sqlite3.Connection: """Connect to the SQLite database.""" return sqlite3.connect(db_name) def create_contacts_table(conn: sqlite3.Connection): """Create the contacts table if it does not exist.""" with conn: conn.execute( """ CREATE TABLE IF NOT EXISTS contacts ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, phone_no TEXT NOT NULL ) """ ) def add_contact(conn: sqlite3.Connection, name: str, phone_no: str): """Add a contact to the contacts table.""" with conn: conn.execute( "INSERT INTO contacts (name, phone_no) VALUES (?, ?)", (name, phone_no), ) print(f"Contact {name} with phone number {phone_no} added successfully.") def main(): db_name = "local_db.sqlite" # Name of the SQLite database file # Connect to the database conn = connect_to_db(db_name) # Create the contacts table create_contacts_table(conn) # Add a contact name = input("Enter the contact's name: ") phone_no = input("Enter the contact's phone number: ") add_contact(conn, name, phone_no) # Close the connection conn.close() if __name__ == "__main__": main()Explanation:
-
connect_to_db(db_name: str) -> sqlite3.Connection:
- This function connects to the SQLite database specified by
db_name. sqlite3.connect(db_name)returns a connection object.
- This function connects to the SQLite database specified by
-
create_contacts_table(conn: sqlite3.Connection):
- This function creates the
contactstable if it does not already exist. - The table has three columns:
id,name, andphone_no. idis the primary key and auto-increments with each new entry- The
with conn:statement ensures that the transaction is committed automatically.
- This function creates the
-
add_contact(conn: sqlite3.Connection, name: str, phone_no: str):
- This function inserts a new contact into the
contactstable. - It uses a parameterized query to prevent SQL injection.
- It also prints a success message once the contact is added.
- The
with conn:statement ensures that the transaction is committed automatically.
- This function inserts a new contact into the
-
main():
- This is the main function that orchestrates the database connection, table creation, and contact addition.
- It prompts the user to enter a name and phone number.
- After adding the contact it closes the connection using
conn.close()
Usage:
- Save the script as
add_contact.py. - Run the script:
python add_contact.py. - Enter the contact's name and phone number when prompted.
The script will create a local SQLite database file named
local_db.sqlite, create thecontactstable if it doesn't exist, and add the provided contact to the table.