3

I'm working with and Sqlite3 database trying to access its data from a different directory than it was originally created.

The python script(test case) that I have ran through our Squish GUI Automation IDE is located in a directory

C:\Squish_Automation_Functional_nVP2\suite_Production_Checklist\tst_dashboard_functional_setup

There I create a database with the following table inside of that same script:

def get_var_value_pass():
    conn = sqlite3.connect('test_result.db')
    c = conn.cursor()
    c.execute('CREATE TABLE IF NOT EXISTS test_result_pass (pass TEXT,result INTEGER)')    
    c.execute("INSERT INTO test_result_pass VALUES('Pass', 0)")
    conn.commit()  
    c.execute("SELECT * FROM test_result_pass WHERE pass='Pass'", )       
    pass_result = c.fetchone() 
    test.log(str(pass_result[1])) 
    test_result = pass_result[1] 
    c.close()
    conn.close()       
    return test_result

Now I'd like to access the same database which I've created formerly by "conn = sqlite3.connect('test_result.db')" inside of another test case which is located in a different directory

C:\Squish_Automation_Functional_nVP2\suite_Production_Checklist\tst_Calling_In-Call-Options_Text_Share-Text_Update-Real-Time

The problem is, when I try a select statement for the same database-- inside of a different script(test case) like so:

def get_var_value_pass(pass_value):
    conn = sqlite3.connect('test_result.db')
    c = conn.cursor()
    c.execute("SELECT * FROM test_result_pass WHERE pass='Pass'", )  

My test fails as soon as I try the c.execute("") statement because the table can't be found. Instead, the most recent "conn = sqlite3.connect('test_result.db')" has just created a new empty database, instead of referring to my original created database. Therefore, I've come to the conclusion that I'll want to try and store my original database where both test cases can use them as a test_suite_resource--basically inside of another directory where the other test cases will have reference. Ideally here:

C:\Squish_Automation_Functional_nVP2\suite_Production_Checklist

Is there a sqlite3 function that lets you declare the place where you'd like to Connect / Store your database? Similar to sqlite3.connect('test_result.db') except you define its path?

BTW-- I have tried the second snippet of code inside of the same test script and it runs perfectly. I have also tried an in-memory approach by sqlite3.connect(':memory:') -- still no luck. Please help! Thanks.

Austin Duran
  • 83
  • 1
  • 1
  • 9
  • Just specify either the full absolute path to the file (Or a path relative to the current working directory of your program). – Shawn Oct 19 '18 at 22:40
  • Shawn, thanks for your response. By still using the sqlite function conn = sqlite3.connect('test_result.db') ? Then specifying the path from which it was originally created? conn = sqlite3.connect('C:\Squish_Automation_Functional_nVP2\suite_Production_Checklist\tst_dashboard_functional_setup\test_result.db') ? – Austin Duran Oct 19 '18 at 23:14
  • 1
    you may have to escape your backslashes in the quoted string. – bruceg Oct 20 '18 at 00:34
  • 2
    You can pass an absolute full path into `connect()`. – Parfait Oct 20 '18 at 01:41

1 Answers1

5

It's not clear to me that you received an answer. You have a number of options. I happen to have a sqlite database stored in one directory which I can open in that or another directory by specifying it in one of the following way.

import sqlite3

conn = sqlite3.connect(r'C:\Television\programs.sqlite')
conn.close()
print('Hello')

conn = sqlite3.connect('C:\\Television\\programs.sqlite')
conn.close()
print('Hello')

conn = sqlite3.connect('C:/Television/programs.sqlite')
conn.close()
print('Hello')

All three connection attempts succeed. I see three Hellos as output.

  1. Stick an 'r' ahead of the string. See the documents about string for the reason.
  2. Replace each backward stroke with a pair of backward strokes.
  3. Replace each backward stroke with a forward stroke.
Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • `See the documents about string for the reason.` Why you do this. Just post the reason please. – NoName Dec 09 '20 at 07:51
  • @NoName: Why do I do what? Are you asking me to post the reason why the person asking the question experienced that, or why I offered this suggestion? – Bill Bell Dec 09 '20 at 16:59