Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon-nova-experimental-chat-05-14· AmazonHugging Face LMArena Human Preference 140K

please code a python script that connects to local db and adds a name and a phone no. in…

  1. User

    please code a python script that connects to local db and adds a name and a phone no. in contacts table

  2. Amazon Nova Assistant

    Certainly! Below is a Python script that connects to a local SQLite database, creates a contacts table if it doesn't exist, and adds a name and phone number to the table.

    Requirements:

    1. SQLite: This script uses SQLite, which is included with Python by default.
    2. 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:

    1. 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.
    2. create_contacts_table(conn: sqlite3.Connection):

      • This function creates the contacts table if it does not already exist.
      • The table has three columns: id, name, and phone_no.
      • id is the primary key and auto-increments with each new entry
      • The with conn: statement ensures that the transaction is committed automatically.
    3. add_contact(conn: sqlite3.Connection, name: str, phone_no: str):

      • This function inserts a new contact into the contacts table.
      • 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.
    4. 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:

    1. Save the script as add_contact.py.
    2. Run the script: python add_contact.py.
    3. Enter the contact's name and phone number when prompted.

    The script will create a local SQLite database file named local_db.sqlite, create the contacts table if it doesn't exist, and add the provided contact to the table.