0

I am very new to SQL and trying to create a table using SQL with python. my first trial got an error as "data base is locked"

here are my quote, please kindly help .

import sqlite3
with sqlite3.connect("PhoneBook.db")as db:
    cursor=db.cursor()
    

    
cursor.execute("""CREATE TABLE IF NOT EXISTS Names(
        ID integer PRIMARY KEY, firstname text NOT NULL, surname text NOT NULL,
        phonenumber text);""")

        
cursor.execute("""INSERT INTO Names (ID,firstname,surname,phonenumber)VALUES 
               ("6","Simon","Howels","01223349752")""")
db.commit()

cursor.execute("""INSERT INTO Names (ID,firstname,surname,phonenumber)VALUES 
               ("7","Karen","Philip","0954295773")""")
db.commit()
             
cursor.execute("""INSERT INTO Names (ID,firstname,surname,phonenumber)VALUES 
               ("8","Darren","Smith","01583749012")""")

db.commit()
cursor.execute("""INSERT INTO Names (ID,firstname,surname,phonenumber)VALUES 
               ("9","Anne","Jones","01323567322")""")

db.commit()

cursor.execute("""INSERT INTO Names (ID,firstname,surname,phonenumber)VALUES 
               ("10","Mark","Smith","01223855534")""")

db.commit()

db.close()
3
  • I cannot reproduce it locally, but I think the reason could be that you're running most of the code outside of the with clause, e.g. after the connection is already closed. But normally this error happens when more than one process tries accessing the same DB. Commented Aug 26, 2021 at 16:46
  • This could have many causes, a common error is for example that you have opened the ide of sqlite and are actively using it, then you already have a process running, which is why the database is locked for more. The problem has been discussed here before: stackoverflow.com/questions/2740806/… Commented Aug 26, 2021 at 16:57
  • I was going to say @bereal couldn't be right and that was just an indentation problem in the cut-and-paste, but after checking, I think he has nailed it. Instead of using "with", just say db = sqlite3.connect(...) / cursor = db.cursor() and see if that doesn't clear it up. Commented Aug 26, 2021 at 17:40

0

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.