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()
Emily
  • 1
  • 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. – bereal Aug 26 '21 at 16:46
  • 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. – Tim Roberts Aug 26 '21 at 17:40
  • 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: https://stackoverflow.com/questions/2740806/python-sqlite-database-is-locked – Julio Reckin Aug 26 '21 at 16:57

0 Answers0