Answer the question
In order to leave comments, you need to log in
How to pass database name to another function?
I already wrote a class, but I'm still struggling with the problem of visibility of the database name in another function.
there is a class
class base:
def __init__( self ):
self.main_database_name=None
self.sqlite_connection=None
self.cursor=None
self.sqlite_create_table_query=None
self.query=None
self.parametr=None
self.one_result=None
def connect(self,main_database_name):
self.main_database_name=main_database_name
self.sqlite_connection = sqlite3.connect(main_database_name)
self.cursor = self.sqlite_connection.cursor()
return main_database_name
def close(self):
if (self.sqlite_connection):
self.sqlite_connection.close()
print("закрыто")
def create_table(self,sqlite_create_table_query):
self.sqlite_create_table_query =sqlite_create_table_query
self.cursor.execute(sqlite_create_table_query)
self.sqlite_connection.commit()
return print("сохранено")
def insert(self,query,parametr):
self.query=query
self.parametr=parametr
self.cursor.execute(query,parametr)
self.sqlite_connection.commit()
print("записано")
def select_many(self,query,id):
self.cursor.execute(query)
self.one_result=self.cursor.fetchmany(size=id)
return print(self.one_result)
def select_singl(self,query):
self.cursor.execute(query)
self.one_result=self.cursor.fetchone()
return self.one_result
def connect():
name_DB = filedialog.askopenfilename(filetypes=(("Database", ".db"),))
mdb=base()
mdb.connect(name_DB)
return name_DB
def download():
# подключение к бд
name_DB=connect()
db=base()
db.connect(name_DB)
sqlite_create_table_query='''CREATE TABLE session (id INTEGER PRIMARY KEY ,ip_src TEXT ,ip_dst TEXT ,sport TEXT , dport TEXT ,request TEXT ,response TEXT );'''
db.create_table(sqlite_create_table_query)
def on_select(event):
print(#имя базы данных) #<- как мне здесь вывести имя этой базы данных
# Если привязывались не к событию <<TreeviewSelect>>,
# то тут нужно проверить, что вообще что-то выбрано:
if not tree.selection():
return
# Получаем id первого выделенного элемента
selected_item = tree.selection()[0]
# Получаем значения в выделенной строке
values = tree.item(selected_item, option="values")
print(values[0])
Answer the question
In order to leave comments, you need to log in
This is first:
from base import Base as db
def connect(db):
......
def download(db):
........
class base:
def __init__( self ):
self.main_database_name=None
.............
@property
def db_name(self):
_db_name = self.main_database_name
return _db_name
def on_select(event, db):
print(db.db_name)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question