Reputation: 11
import sqlite3
database_connection = sqlite3.connect("accounts.db")
database_cursor = database_connection.cursor()
def create_table():
database_cursor.execute(
"""
CREATE TABLE IF NOT EXISTS accounts (
website VARCHAR(50) NOT NULL,
username VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL);
"""
)
def create_account():
website = input("\nEnter Website: ")
username = input("Enter Username: ")
email = input("Enter Email: ")
password = input("Enter Password: ")
database_cursor.execute(
"""
INSERT INTO accounts ('website', 'username', 'email', 'password')
VALUES (website, username, email, password)
"""
)
def remove_account():
pass
def lookup_account():
pass
def all_accounts():
database_cursor.execute(
"""
SELECT * FROM accounts;
""")
return database_cursor.fetchall()
create_table()
create_account()
print(all_accounts())
database_connection.commit()
database_connection.close()
The code is untidy temp code (sorry) because i wanted to get the database to work, cant figure out why it's giving me this error. I'm very new to sqlite3 and sql so it's probably a dumb and elementary mistake but i cant get it to work. help appreciated :)
Upvotes: 0
Views: 672
Reputation: 11
For anyone with this problem i troubleshooted a solution hope it helps :)
I Changed:
database_cursor.execute(
"""
INSERT INTO accounts ('website', 'username', 'email', 'password')
VALUES (website, username, email, password)
"""
)
To:
database_cursor.execute(
f"""
INSERT INTO accounts (website, username, email, password)
VALUES ('{user_website}', '{user_username}', '{user_email}', '{user_password}');
"""
)
Upvotes: 1
Reputation: 373
You need to connect to the db before you query it. You are doing this and passing it a table name as your db filename.
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
here is the link for more info: https://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/
Upvotes: 0