I can insert hardcoded values into an SQLite table with no problem, but I'm trying to do something like this:
name = input("Name: ")
phone = input("Phone number: ")
email = input("Email: ")
cur.execute("create table contacts (name, phone, email)")
cur.execute("insert into contacts (name, phone, email) values"), (name, phone, email)
I know this is wrong, and I can't find how to make it work. Maybe someone could point me in the right direction.
First, connect to the SQLite database by creating a Connection object. Second, create a Cursor object by calling the cursor method of the Connection object. Third, execute an INSERT statement. If you want to pass arguments to the INSERT statement, you use the question mark (?) as the placeholder for each argument.
def populate_table(table_name, data): #Grab first tuple and find out how many columns are expected to be edited num_columns = len(data[0]) try: cursor. executemany("INSERT INTO {} VALUES({})". format(table_name, ','.
You can use ?
to represent a parameter in an SQL query:
cur.execute("insert into contacts (name, phone, email) values (?, ?, ?)",
(name, phone, email))
cur.executemany("insert into contacts (name, phone, email) values (?, ?, ?)", (name, phone, email))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With