开发者

Linux/Unix Server: Access SQL database that is in different directory than executing script

开发者 https://www.devze.com 2023-04-04 18:56 出处:网络
I have a python script that executes a SQL query & gets information from a SQLite3 database file. The SQLite3 database file resides in a different directory than that of the executing script. All

I have a python script that executes a SQL query & gets information from a SQLite3 database file. The SQLite3 database file resides in a different directory than that of the executing script. All this occurs on my webhost(Unix server cpanel) from within the cgi-bin directory.

My Question/Problem: Is it possible to have a python script access/connect to a SQLite3 database that is in another directory? Right now when I run the script on my webhost it fails to connect to the SQLite3 database.

My python file exists in this directory on the webhost: public_HTML/cgi-bin/dataMiner/. And the SQLite3 database file exists in this directory public_HTML/cgi-bin/.

My code in the python script is:

import DataMiner

def connect_ex( f_name ):
    """ Post: """

    try:
        # When the script runs the file is never found but it actually does exist
        if not os.path.isfile( f_name ): 
            raise Exception("")

        conn = sqlite3.connect( f_name )

        return conn

    except IOError, msg:
        #print str(msg)
        return None

    except OSError, msg:
        #print str(msg)
        return None

    except sqlite3.OperationalError, msg:
        #print str(msg)
        return None

    except Exception, e:
        retu开发者_如何学Gorn None


conn  = connect_ex("../competitionDB.db")
res   = DataMiner.execute_query( conn, "SELECT * FROM MyTable;" )
print res
conn.close()


With the database I use (firebird) you have to specify the full path to the database from the root on the server

So "/var/folder/wwww/db/dbfile.db" for example, with /var being in the root folder of coarse

Just saying where it is FROM the website "db/dbfile.db" wont work (for firebird at least)

This also allows you to put your database file in a folder that isnt even on the website, for security purposes.

0

精彩评论

暂无评论...
验证码 换一张
取 消