D
D
Durilka962021-03-26 21:07:37
Python
Durilka96, 2021-03-26 21:07:37

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

then there are 2 functions in one i want to open the database and so that the value of the database name that i opened through the file manager is passed to the second function
first function :
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)

the second function, to which I want to pass the value of the database name (this function is triggered by the onClick event and there are no options here to call the database name every time):
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])


or tell me how you can connect the database so that the connection to it is in any functions that are without a forced connection in them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexa2007, 2021-03-26
@Alexa2007

This is first:

from base import Base as db
def connect(db):
......
def download(db):
........

So you will be connected to one object
. Secondly:
class base:
    def __init__( self ):
        self.main_database_name=None
.............

    @property
    def db_name(self):
        _db_name = self.main_database_name
        return _db_name

Well, then:
def on_select(event, db):
    print(db.db_name)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question