mercredi 29 juillet 2015

I've been messing about a little bit with SQLite3 with Python. Everything is working out beautifully and the convenience of SQLite is nice in small datbases to work with. However, I have been attempting to insert a timestamp field and keep getting peculiar results. Consider the code below.

timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
print "Timestamp - " + timestamp
insert_result(timestamp, 'New Scan Wizard', 'PASS')

Which will successfully print 20150729152909 (some timestamp), however inserts <function timestamp at 0x0262A3F0> which ofcourse defeats my purpose. I have been trying playing about seeing if I could intuitively (I thought maybe some use of raw_string would've come in handy, which did not turn out to be the case) come up with a solution but my lack of experience with Python as a whole makes me think that this is an internal cause beyond my understanding.

Below is the relevant SQLite code:

def insert_result(curr_timestamp, test_case_name, result):
    database_path = 'C:\Users\Public\Documents\Test Results\Acunetix ATDD Results.db'
    database_path.encode('string-escape')
    database_path.replace(r'\\', chr(92))
    print "[DEBUG] Database path: %s" % database_path
    connector = sqlite3.connect(database_path)

    cursor = connector.cursor()
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS TestCases 
        (test_id INTEGER PRIMARY KEY AUTOINCREMENT,
            timestamp varchar(255), 
            test_case_name varchar(255), 
            result char(4),
            notes varchar(255), 
            bug_id INTEGER)
        ''')
    cursor.execute('INSERT INTO TestCases VALUES (NULL,\'%s\', \'%s\', \'%s\', 
                    NULL, NULL)' % (timestamp, test_case_name, result))

for row in cursor.execute('SELECT * FROM TestCases'):
    print row

connector.commit()
connector.close()

Essentially, does this issue lie within sqlite3 or within Python?

I apologise if I don't abide by the proper conventions yet

Aucun commentaire:

Enregistrer un commentaire