mardi 3 mars 2015

Using SQLite in Python: database locked

I am trying to use an SQLite3 database with Python. My database functions are defined within a class and look like this:



def insertStation(self, stationID, nameNL, nameFR, nameDE, nameEN, latitude, longitude):
c = self.conn.cursor()
station = (stationID,
nameNL,
nameFR,
nameDE,
nameEN,
latitude,
longitude)
c.execute('INSERT INTO station VALUES (?, ?, ?, ?, ?, ?, ?)', station)
return

def insertTrain(self, trainNumber, commercialType, origin, destination):
c = self.conn.cursor()
train = (trainNumber,
commercialType,
origin,
destination)
c.execute('INSERT INTO train VALUES (?, ?, ?, ?)', train)
return

def trainExists(self, trainNumber):
c = self.conn.cursor()
rows = c.execute('SELECT EXISTS (SELECT * FROM train WHERE train_id = ?)', (trainNumber,))
if (rows.fetchone()[0] is 1):
return True
else:
return False

def commit(self):
self.conn.commit()


In another file, I am calling these functions in the following manner:



trainID = #some ID
if not(database.trainExists(trainID)):
database.insertTrain(trainID,
schedule['CommercialTypes'][0],
schedule['Origins'][0],
schedule['Destinations'][0])
routeID = database.insertRoute(toUnix(schedule['DepartureDate']),
trainID)
database.commit()


When I run this code, it gives me an error saying the database is locked. How can I get this to work?


Aucun commentaire:

Enregistrer un commentaire