Attach database before performing SQL query in tensorflow

I found that tensorflow has a way to handle sqlite queries [see here]. However, how would one proceed if I need to attached several databases to the main database before I can perform the query? Is there still a way to use this particular sqlite queries support in tensorflow?

1 Like

Hi @edamondo. Can you please tell what you mean about “attach[ing] several databases”? I’m not sure I understand what you mean,

1 Like

Hello @tagoma, yes sure. For example in this stack overflow post,

c.execute('ATTACH DATABASE "db_1.sqlite" AS db_1')

attaches a database to the one that was opened with

conn = sqlite3.connect('db_master.sqlite')

So I could join tables from the two different databases and perform queries on the joined tables.

Is it more clear now?

1 Like

Hello @edamondo
After trying out a few things on my side, it looks like on can combine different SQL execute
statements under the unique argument query of tf.data.experimental.SqlDataset. You just have to separate them by the usual semi-column.
Something along this line:

dataset = tf.data.experimental.SqlDataset(driver_name='sqlite',
                                          data_source_name= '/foo/bar.sqlite3',
                                          query='ATTACH DATABASE "foo/otherDB.sqlite3" AS AttachedDB;\
                                          SELECT name, age FROM AttachedDB.tableInAttachedDB',
                                          (tf.string, tf.int32))
1 Like

@tagoma, thank you this helped me. I still couldn’t query my databasis since the records contains tensors arrays (like tf.constant([10, 20,30])) or numpy arrays (like np.array([50,60,70])) and it seems that tf.Dtype in output_type [see here] doesn’t support such array types. Or am I making a mistake?

Hey @edamondo.
I’m not sure I understand you set up.
What do you mean “records contains tensors arrays”, please?