Having self.con = sqlite3.connect (db) and self.cur = self.con.cursor() in the db.py file, how can I call them in the main.py file to use cursor.execute? I wrote db.self.cur.execute('SELECT Name FROM TableExample'), but obviously I was wrong, there is an error of course
P.S: In db.py do I have to insert the path of the database inside db of the sqlite3.connect(db)?
I had tried like this:
DB.PY
import sqlite3
class Database:
def __init__(self, db):
self.con = sqlite3.connect(db)
self.cur = self.con.cursor()
sql = """
CREATE TABLE IF NOT EXISTS employees(
ID Integer Primary Key,
example1 integer,
example2 integer
)
"""
self.cur.execute(sql)
self.con.commit()
MAIN.PY
from db import Database
db = Database('/home/xxxx/database.db')
def example():
db.self.cur.execute('SELECT Name FROM TableExample') #error
result=[row[0] for row in cursor]
return result
UPDATE: New example in main.py
def example():
db.cur.execute('SELECT Name FROM TableExample')
result=[row[0] for row in db.cur]
return result