how to insert string variable into sqlite database 1

how to insert string variable into sqlite database

def create_db():
    with con:
        cur = con.cursor()
        cur.execute("DROP TABLE IF EXISTS Contacts")
        cur.execute("CREATE TABLE Contacts (First Name TEXT, Last Name TEXT, Phone TEXT, Email TEXT);")
        cur.execute("INSERT INTO Contacts VALUES (?, ?, ?, ?);", (firstname, lastname, phone, email))

        ## call commit on the connection...
        con.commit()

Here is what the above code is Doing:
1. We’re creating a connection to the database.
2. We’re creating a cursor object.
3. We’re dropping the table if it already exists.
4. We’re creating a new table.
5. We’re inserting a new row into the table.
6. We’re committing the changes to the database.

Similar Posts