Python Forum
.exe prob - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: .exe prob (/thread-30775.html)



.exe prob - Kundan - Nov-05-2020

import sqlite3

def writeTofile(data, filename):
    # Convert binary data to proper format and write it on Hard Disk
    with open(filename, 'wb') as file:
        file.write(data)
    #print("Stored blob data into: ", filename, "\n")

def readBlobData(empId):
    try:
        sqliteConnection = sqlite3.connect('SQLite_Python.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")

        sql_fetch_blob_query = """SELECT * from new_employee where id = ?"""
        cursor.execute(sql_fetch_blob_query, (empId,))
        record = cursor.fetchall()
        for row in record:
            #print("Id = ", row[0], "Name = ", row[1])
            name  = row[1]
            photo = row[2]
            

            print("Storing employee image and resume on disk \n")
            photoPath = r"C:\AdwCleaner\\" + name + ".txt"
            
            writeTofile(photo, photoPath)
            

        cursor.close()

    except sqlite3.Error as error:
        print("Failed to read blob data from sqlite table", error)
    finally:
        if (sqliteConnection):
            sqliteConnection.close()
            print("sqlite connection is closed")

# 1.G  2. M

readBlobData(1)
The above code works well in the folder in which it was created. But when I transfer it to another folder for making and run it I get the following output:
Connected to SQLite
Failed to read blob data from sqlite table no such table: new_employee
sqlite connection is closed


RE: .exe prob - ndc85430 - Nov-05-2020

You're only ever looking for the database in the current directory. If it doesn't exist there, I assume it's being created and so of course doesn't have your schema (because there's nothing in the code to do that). Why don't you pass the path to where the database lives?


RE: .exe prob - Kundan - Nov-06-2020

Please let me know how to pass the path in the code. I am unable to create an .exe with the help of pyinstaller. The created .exe does not give the reqired output.


RE: .exe prob - Kundan - Nov-06-2020

Also let me know how to find the path where the database lives. I am trying this for the first time.


RE: .exe prob - ndc85430 - Nov-06-2020

Pass it as an argument to your program too. Look at argv in the sys module to get access to the arguments passed on the command line. Then, you'll be able to pass it to the function in which you need it.

Surely you know where the file is?!


RE: .exe prob - Kundan - Nov-07-2020

Instead of passing arguments canit be ingrained in the code itself please tell me how?


RE: .exe prob - ndc85430 - Nov-07-2020

I guess I'm not really understanding what you want to do. I thought you wanted to be able to run the program from a different place to where the database ia stored. You then either need to hardcode the path to it (meaning you'd have to change the program if you moved it) or pass it in to the program somehow (command line argument, config file, environment variable, ...). Of course, your program could scan your file system(s) to try and discover where the file is, but that may be slow and do you really want to write code to do that anyway?


RE: .exe prob - Kundan - Nov-09-2020

My prob is that when I give this file to pyinstaller the .exe created in the dist folder cannot access the table. So I want to have the table path in the program so that it can be known to the .exe .


RE: .exe prob - ndc85430 - Nov-09-2020

So either hardcode the full path, or pass it in, as I said above.


RE: .exe prob - Kundan - Nov-12-2020

OK Thanks! GOD BLESS YOU!!!!