简体   繁体   中英

How do I execute an insert command on a table in a different root?

I am extracting image chips of blobs detected from a bunch of images and storing this data to an sqlite3 database for later use.

I am successfully able to filter the images, find the blobs, extract an image chip of the blobs, and store the filename, blob center coordinates, and blob chip data to a database that is in the same directory as my code.

def addEntry(filename, cp_y, cp_x, sigma, intensity, chip):
    sqlite3.register_adapter(np.ndarray, adapt_array)
    sqlite3.register_converter('ARRAY', convert_array)
    con = sqlite3.connect('sources.db', detect_types = sqlite3.PARSE_DECLTYPES, timeout = 10)
    cur = con.sursor()
    cur.execute('''INSERT INTO sources (filename, cp_y, cp_x, sigma, intensity, chip) VALUES(?, ?, ?, ?, ?, ?), (filename, cp_y, cp_x, sigma, intensity, chip))
    con.commit()
    con.close()

Now I want to implement this over a couple hundred images and create a different table for each images extracted data. I would like to store these tables in a folder separate from my other code so I changed my code to:

con = sqlite3.connect('./data/sources.db', detect_types = sqlite3.PARSE_DECLTYPES, timeout = 10)
cur.execute('''INSERT INTO ./data/sources (filename, ...

But this returned the error OperationalError: near ".": syntax error . But if I remove the ./data/ portion in the execute command, I receive the error OperationalError: no such table: sources . How do I define the sources table in the /data/ folder to the INSERT command ??

The documentation here does not show an example of executing a command on a table in a different directory and I am yet to come across any other examples of this.

You expect sqlite to replace the . by the current folder path. But it doesn't provide this feature. You can instead provide the full path

import os
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
DB_PATH = SCRIPT_DIR +  '/data/sources.db'
...
con = sqlite3.connect(DB_PATH, detect_types = sqlite3.PARSE_DECLTYPES, timeout = 10)

If the file/table did not exist before, you should simply perfom a kind of database initilization to create the table. But this would be nothing more than the CREATE TABLE statement

CREATE TABLE sources (...) VALUES ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM