Python Forum
something is wrong with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
something is wrong with my code
#1
i made a ui , and converted it into code and add two lines , it worked untill i want to use a variable , see the code below :
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication,QFileDialog
 
class Ui_MainWindow(object): 
####################################################   my code begins
    def get_file_name(self):        
        fname = QFileDialog.getOpenFileName()         
        fname2 = fname[0]
        fname3 = '"' + fname2 + '"'        
        return fname3    
    def sqlite_connect(self):   
        
        conn = sqlite3.connect(get_file_name())####  this variable dosent work for some reason 
        c = conn.cursor()
        c.execute("select * from verses where book_number = 10 and chapter = 1 and verse = 1")
        text = c.fetchmany(1)
        print(text)
 ##########################################################       my code ends
          
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(532, 563)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 532, 21))
        self.menuBar.setObjectName("menuBar")
        self.menuopen = QtWidgets.QMenu(self.menuBar)
        self.menuopen.setObjectName("menuopen")
        MainWindow.setMenuBar(self.menuBar)
        self.actionopen_a_sqlite_file = QtWidgets.QAction(MainWindow)
        self.actionopen_a_sqlite_file.setObjectName("actionopen_a_sqlite_file") 
        self.actionopen_a_sqlite_file.triggered.connect(self.get_file_name)####################     my code
        self.menuopen.addAction(self.actionopen_a_sqlite_file)
        self.menuBar.addAction(self.menuopen.menuAction()) 
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow) 
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menuopen.setTitle(_translate("MainWindow", "open"))
        self.actionopen_a_sqlite_file.setText(_translate("MainWindow", "open a sqlite file"))
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
ui will quit itself
Reply
#2
Provide a better description of the problem. Is there an error codes or trace? When does it crash?
Reply
#3
(Apr-27-2022, 05:07 AM)deanhystad Wrote: Provide a better description of the problem. Is there an error codes or trace? When does it crash?

accutally it doesnt crash, it is just that the return value generated from the first fuction【get_file_name(self):】 can not be used by the second function 【sqlite_connect(self):】 somehow
    def get_file_name(self):        
        fname = QFileDialog.getOpenFileName()         
        fname2 = fname[0]
        fname3 = '"' + fname2 + '"'        
        return fname3    
    def sqlite_connect(self):   
         
        conn = sqlite3.connect(get_file_name())####  this variable dosent work for some reason 
        c = conn.cursor()
        c.execute("select * from verses where book_number = 10 and chapter = 1 and verse = 1")
        text = c.fetchmany(1)
        print(text)
Reply
#4
What is the problem exactly? What is happening? Saying something "doesn't work" isn't helpful. What does "doesn't work" actually mean for this problem? Tell us what happens and what you expect to happen.
Reply
#5
(Apr-27-2022, 05:50 AM)ndc85430 Wrote: What is the problem exactly? What is happening? Saying something "doesn't work" isn't helpful. What does "doesn't work" actually mean for this problem? Tell us what happens and what you expect to happen.

my question is this
shoud i use this
conn = sqlite3.connect(get_file_name())
or this
conn = sqlite3.connect(self.get_file_name)
or this
def get_file_name(self):        
    fname = QFileDialog.getOpenFileName()         
    fname2 = fname[0]
   self.fname3 = '"' + fname2 + '"'             
def sqlite_connect(self):            
    conn = sqlite3.connect(self.fname3)
or semething else?
Reply
#6
Obviously you need parens when you call the function, otherwise you're passing the function itself rather than its return value (which has its uses, but not here).

You've missed the point though: what is wrong with the code you posted? How is it not working? Do you get an error or something else? We can't read your mind, or see your screen.
Reply
#7
(Apr-27-2022, 05:55 AM)53535 Wrote:
self.fname3 = '"' + fname2 + '"'
Why are you adding the quotes?
When you provide the name of the database file in the program, you need quotes:
(Apr-27-2022, 05:55 AM)53535 Wrote: conn = sqlite3.connect("my_db_file.db")
This is to let Python know you are providing a string literal. But when you provide this name as a string variable you must not add the quotes to the name of the database file, or else Python thinks the quotes are part of the name. So:
def get_file_name(self):        
    fname = QFileDialog.getOpenFileName()         
    return fname[0]

def sqlite_connect(self):   
    conn = sqlite3.connect(self.get_file_name())
    c = conn.cursor()
    c.execute("select * from verses where book_number = 10 and chapter = 1 and verse = 1")
    text = c.fetchmany(1)
    print(text)
I did not test it, so please let us know if this works.
Reply
#8
(Apr-27-2022, 07:52 AM)ibreeden Wrote:
(Apr-27-2022, 05:55 AM)53535 Wrote:
self.fname3 = '"' + fname2 + '"'
Why are you adding the quotes?
When you provide the name of the database file in the program, you need quotes:
(Apr-27-2022, 05:55 AM)53535 Wrote: conn = sqlite3.connect("my_db_file.db")
This is to let Python know you are providing a string literal. But when you provide this name as a string variable you must not add the quotes to the name of the database file, or else Python thinks the quotes are part of the name. So:
def get_file_name(self):        
    fname = QFileDialog.getOpenFileName()         
    return fname[0]

def sqlite_connect(self):   
    conn = sqlite3.connect(self.get_file_name())
    c = conn.cursor()
    c.execute("select * from verses where book_number = 10 and chapter = 1 and verse = 1")
    text = c.fetchmany(1)
    print(text)
I did not test it, so please let us know if this works.

no it didnt work, cuz the QFileDialog window was being opened again, i only want the value

but i used this method
def get_file_name(self):        
    fname = QFileDialog.getOpenFileName()         
    fname2 = fname[0]
   self.fname3 = '"' + fname2 + '"'             
def sqlite_connect(self):     
      value1 = self.fname3
     print(value1) #########the value did get into the second function and is printable 
    conn = sqlite3.connect(value1)####but when i wanna use this value my app closed by itself  , 
and the console display nothing but this message

================================ RESTART: Shell ================================
>>>
Reply
#9
It is a pity the console output does not give more information.
But what happens if you change:
self.fname3 = '"' + fname2 + '"'
... to:
self.fname3 = fname2
Reply
#10
(Apr-27-2022, 09:42 AM)ibreeden Wrote: It is a pity the console output does not give more information.
But what happens if you change:
self.fname3 = '"' + fname2 + '"'
... to:
self.fname3 = fname2


this time it worked , i used this code according to your instruction , i changed the fname3 to fname 2
def get_file_name(self):        
    fname = QFileDialog.getOpenFileName()         
   self.fname2 = fname[0]             
def sqlite_connect(self):     
      value1 = self.fname2
     print(value1)  
    conn = sqlite3.connect(value1) 
thanks man Dance
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what is wrong with my code? 53535 11 3,434 Apr-28-2022, 01:52 AM
Last Post: 53535

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020