How to create a database file in python?
I’m not too sure how to link python to database using SQLite as the updateDB function isnt working. This code should work but the update isnt working correctly.
What am i missing?
import sqlite3
class Database: def init(self): self.DBName ='BUYHIRELtd.db'
def connect(self):
conn = None
try:
conn = sqlite3.connect(self.DBName)
except Exception as e:
print(e)
return conn
def queryDB(self, command, params=[]):
conn = self.connect()
cur = conn.cursor()
cur.execute(command, params)
result = cur.fetchall()
self.disconnect(conn)
return result
def updateDB(self, command, params=[]):
conn = self.connect()
cur = conn.cursor()
cur.execute(command, params)
result = cur.fetchall()
self.disconnect(conn)
return result
def disconnect(self, conn):
conn.close()
CodePudding user response:
You are missing conn.commit
to save changes to the database.
import sqlite3
class Connector:
dbName = "Database.db"
def connect(self):
conn = None
try:
conn = sqlite3.connect(self.dbName)
except Exception as e:
print(e)
return conn
def disconnect(self, conn):
conn.close()
def queryDB(self, command, params=[]):
conn = self.connect()
cur = conn.cursor()
cur.execute(command, params)
result = cur.fetchall()
self.disconnect(conn)
return result
def updateDB(self, command, params=[]):
conn = self.connect()
cur = conn.cursor()
cur.execute(command, params)
conn.commit()
result = cur.fetchall()
self.disconnect(conn)
return result
CodePudding user response:
make sure that you're importing sqlite3 in the top of the database.py file or the commands wont be able to run.