0
import sqlite3
conn = sqlite3.connect('/Users/macbook/Desktop/lool.db')
c = conn.cursor()

invalid_input = True

def startinglogin(self):
    loginmessage = input("To register to the Quizzer, type in 'Register'. To login, type in 'Login'.\n")
    while loginmessage != "Login" and loginmessage != "Register":
        loginmessage = input("To register to the Quizzer, type in 'Register'. To login, type in 'Login'.\n")
    self.loginmessage = loginmessage
studentusername = ' '
studentpassword = ' '
def nameandpass():
    global studentusername
    studentusername = input('Enter username: ')
    global studentpassword
    studentpassword = input('Enter password: ')

def start():
    def passlength():
        while len(studentpassword) < 4 or len(studentpassword) > 16:
            print ("ERROR. Your password must be longer than 4 characters and shorter than 16 characters.")
        studentpassword = input('Enter password: ')

    def usernamechecker():
        c.execute("SELECT username FROM studenttest WHERE username=(?)", (studentusername, ))
        if c.fetchone() == None:
            global usernamecheck
            usernamecheck = ' '
        else:
            c.execute("SELECT username FROM studenttest WHERE username=(?)", (studentusername, ))
        usernamecheck = ' '.join(map(str, (c.fetchone())))

    def passwordchecker():
        c.execute("SELECT password FROM studenttest WHERE password=(?)", (studentpassword, ))
        if c.fetchone() == None:
            global passcheck
            passcheck = ' '
        else:
            c.execute("SELECT password FROM studenttest WHERE password=(?)", (studentpassword, ))
            passcheck = ' '.join(map(str, (c.fetchone()))) 
    def create_db():
        c.execute("DROP TABLE IF EXISTS studenttest")
        c.execute("CREATE TABLE studenttest (id INTEGER PRIMARY KEY, username TEXT, password TEXT)")
        conn.commit()
    def loginform():
        while usernamecheck != studentusername or passcheck != studentpassword:
            print ("Your login info is wrong.")
            nameandpass()
            usernamechecker()
            passwordchecker()
    if usernamecheck == studentusername and passcheck == studentpassword:
        print ("Access granted!")
        global invalid_input
        invalid_input = False
    else:
        print("Access denied!")
    def registerform():
        while studentusername == useravailable:
            print ("That username is already taken!")
            global studentusername
            studentusername = input("Enter username: ")
        if studentusername != useravailable:
            c.execute("INSERT INTO studenttest VALUES (NULL,?,?);", (studentusername, studentpassword))
            global invalid_input
            invalid_input = False
            conn.commit()
    def sameusername():
        c.execute("SELECT username FROM studenttest")
        global useravailable
        useravailable = ' '.join(map(str, (c.fetchone())))
    if startinglogin.loginmessage == "Register": 
        sameusername()
        registerform()
    elif startinglogin.loginmessage == "Login":
        usernamechecker()
        passwordchecker()
        loginform()
    conn.commit()

while invalid_input:
    startinglogin(startinglogin)
    nameandpass()
    start()
if invalid_input == False:
    c.close()

I have no idea why my code is locking the database, can someone help out with that? The error message that is displayed in the shell is:

Traceback (most recent call last):
File "/Users/macbook/Documents/hey draft2.py", line 85, in <module>
start()
File "/Users/macbook/Documents/hey draft2.py", line 75, in start
registerform()
File "/Users/macbook/Documents/hey draft2.py", line 65, in registerform
c.execute("INSERT INTO studenttest VALUES (NULL,?,?);",   (studentusername, studentpassword))
sqlite3.OperationalError: database is locked 

If this is a noob mistake, I'm really sorry for wasting time. :/

EDIT: Forgot to call passlength() in the code, don't mind that. Adding or taking it out had no effect.

  • [this](http://stackoverflow.com/questions/27160987/sqlite-python-sqlite3-operationalerror-database-is-locked) might help. – R Nar May 25 '16 at 14:28

1 Answers1

1

Would you happen to be using a visual tool for sqlite (e.g. sqlitebrowser) while running your code? If you have unwritten changes in the visual tool it might throw errors if you try to run your python code. The fix is to write the changes, then try the code again.

EDIT: This question has an answer that recommends this old sqlite documentation, that might help too.

Community
  • 1
  • 1
Andrew Zick
  • 582
  • 7
  • 23
  • Oh my god. Thank you so much, such an easy fix as I thought of. (Noob mistake :/ yes I do use sqlitebrowser) Kind of off topic, but how about the actual code? Anything that catches your eye or could be made more efficient? (PS: It's my first code over 30 lines derp.) – Mert GERDAN May 25 '16 at 14:33
  • @MertGERDAN The first thing that catches my eye is your formatting. I would recommend putting 1-2 empty lines (aka newlines) after then end of every function definition. It will make the code easier to read. Also, if you're not using an IDE, I would recommend [PyCharm](https://www.jetbrains.com/pycharm/download/) as it has syntax and style checking built-in, which will help you write better code. – Andrew Zick May 25 '16 at 14:46
  • Thanks. It actually has spaces, but the code format was messed up when I copied, so I had to do it by hand. Forgot to add spaces on this one. I use IDLE however yes I do have PyCharm's Edu license and will be using that from now on. – Mert GERDAN May 26 '16 at 06:26