2

I'm trying to create a simple login program that saves the recorded username and password from variables into an SQLite3 database. Running the program using hardcoded strings works as expected, but when I try to use variables, a str-based TypeError occurs. I tried using str(variable), but that didn't work and I'm unsure what else could be problem. Any help would be appreciated.

import sqlite3
from sqlite3 import Error
import sys

def execute_query(connection, query):
    cursor = connection.cursor()
    try:
        cursor.execute(query)
        connection.commit()
        print("Query executed successfully")
    except Error as e:
        print(f"The error '{e}' occurred")

def create_new_user(new1, new2):

    create_users =  ("""INSERT INTO users (username, password)
                       VALUES (?, ?)
                        ;""", str(new1), str(new2))

    execute_query(connection, create_users)   

create_users_table =  """CREATE TABLE IF NOT EXISTS users (
                                        id INTEGER PRIMARY KEY AUTOINCREMENT,
                                        username TEXT NOT NULL,
                                        password TEXT NOT NULL
                                    ); """

execute_query(connection, create_users_table)

user = input("Would you like to create an account? ")
if "yes" in user:

    new1 = input("\nNew username: ")
    new2 = input("New password: ")
    create_new_user(new1, new2)

else:
    sys.exit(0)
Traceback (most recent call last):
  File "/Users/scnewmark/Documents/Database/database.py", line 62, in <module>
    create_new_user(new1, new2)
  File "/Users/scnewmark/Documents/Database/database.py", line 40, in create_new_user
    execute_query(connection, create_users)
  File "/Users/scnewmark/Documents/Database/database.py", line 18, in execute_query
    cursor.execute(query)
ValueError: operation parameter must be str
1
  • 1
    Edit the question to show the full traceback as properly formatted text. Commented Feb 24, 2020 at 23:02

1 Answer 1

1

The execute method expects a SQL query string as the first argument and a tuple of parameters as the second argument, and yet with your:

create_users =  ("""INSERT INTO users (username, password)
                   VALUES (?, ?)
                    ;""", str(new1), str(new2))

and passing create_users as the query argument to do:

cursor.execute(query)

you are passing a tuple as the first argument to the execute method, resulting in the TypeError.

Instead, you can pass the query string and the parameters separately:

def execute_query(connection, query, credentials):
    cursor = connection.cursor()
    try:
        cursor.execute(query, credentials)
        connection.commit()
        print("Query executed successfully")
    except Error as e:
        print(f"The error '{e}' occurred")

def create_new_user(new1, new2):

    query = "INSERT INTO users (username, password) VALUES (?, ?);"
    credentials = str(new1), str(new2))

    execute_query(connection, query, credentials)
Sign up to request clarification or add additional context in comments.

2 Comments

That worked; thank you! Now one follow up question, is there a way to search the database using a something along the lines of if username in database: <run code>?
You're welcome. You can refer to stackoverflow.com/questions/2440147/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.