Reputation: 15
I am trying to make a virtual machine, but in a CLI. I am using SQLite for the login data and potentially for other data in the future. Sadly, I keep getting this error and I don't know how to fix it. Could you guys help me? I plan on releasing the first version of this on GitHub when I fixed this error.
from os import system, name
import sqlite3
from sqlite3 import Error
db_file = "userdata"
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(db_file)
print(sqlite3.version)
except Error as e:
print(e)
finally:
if conn:
conn.close()
def clear():
if name == 'nt':
_ = system('cls')
else: _ = system('clear')
def todolist():
print(" Backlog | To-do | In progress | Finished (coming in next update)")
print(" --------|---------------------------|-----------------|---------------------------------")
print(" - | Register function | SQLite database | Basis build")
print(" | Forgot password function | |")
print(" | Login function | |")
print("Made by The Celt")
print("Welcome back!")
print("Login (l)")
print("Register (r)")
print("Forgot password (fp)")
starterAction = input("What do you want to do? ")
create_connection(db_file)
if starterAction == "l":
s.sleep(0.8)
clear()
insertUsername = input("Username:")
for name in ('bar','foo'):
cursor.execute("SELECT rowid FROM components WHERE name = %s"%insertUsername, (name,))
data=cursor.fetchone()
if data is None:
print("Could not find this username in the database!")
else:
insertPassword = input("Password: ")
for name in ('bar','foo'):
cursor.execute("SELECT rowid FROM components WHERE name = %s"%insertPassword, (name,))
data=cursor.fetchone()
if data is None:
print("Could not find this password in the database!")
else:
print("Welcome back, %s"%insertUsername)
elif starterAction == "r":
s.sleep(0.8)
clear()
print("Still in progress!")
elif starterAction == "todolist":
todolist()
Upvotes: 0
Views: 3088
Reputation: 4129
It is not a sqlite error, it is a python error: You did not defined "cursor"
You have to define conn and the cursor within the global scope, near to db_file
db_file = "userdata"
conn = None
cursor = None
and then, after connection has been established (within create_connection routine), you need to add cursor creation
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
see here for details
And the way you're selecting from db is also wrong:
for security's sake use bind variables where possible, like that (from documentaion)
cur.execute('SELECT * FROM stocks WHERE symbol=?', t)
if you still insist on concatenating the query string (which I NOT recommend) you need to place value you're searching for within two apostrophes (I've updated %s to '%s')
... WHERE name = '%s'"%insertPassword, (name,) ...
Upvotes: 1