0

I have a long list (or a table of 3 to 4 columns of data) which simultaneously needs to be satisfied in a query, like below:

A    B   C
x1   y1  z1
x2   y2   z2
x3   y3   z3
.
.
.
xn    yn   zn

Is there a way I can create an expression/function etc or separate list etc which I can have which can be called into the SQL Query (instead of explicitly writing it):

So assuming, I have this list as a matrix/list in a table X (local) and I can use

and I can use a sample SQL query something like

SELECT *
FROM TABLE B
WHERE CONDITION  IN  ROWS OF TABLE X 

Just to add here, that I am using python driver to get data via database and the table X (I have it locally on my drive) which I have is which I created locally.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stan
  • 786
  • 1
  • 9
  • 25
  • . . Don't call something a table unless it is in the database. Your question is unclear. – Gordon Linoff Oct 12 '18 at 16:09
  • What I mean was that I have the table X as a file locally on my PC. Sorry for the confusion. – Stan Oct 12 '18 at 16:26
  • To clarify the question as pointed out by Gordan: No I was not asking for the python code. I asked how can I create a compact query filter with the WHERE clause where the condition is being from a local table. Say based on the table I provided if each of the conditions in each row are met like ( x1 & y1 & z1 or x2 & y2 & z2 or x3 & y3 & z3 ..) – Stan Oct 12 '18 at 21:05

1 Answers1

0

I am assuming you are asking for python code.

Using a simple in-memory SQLite db you can achieve this. See how use Sqlite db with python here. How to work with sqlite3 and Python.

Considering your example where we have list of data.

import sqlite3
connection = sqlite3.connect("company.db")

cursor = connection.cursor()

staff_data = [ ("William", "Shakespeare", "m", "1961-10-25"),
               ("Frank", "Schiller", "m", "1955-08-17"),
               ("Jane", "Wall", "f", "1989-03-14") ]

for p in staff_data:
    format_str = """INSERT INTO employee (staff_number, fname, lname, gender, birth_date)
    VALUES (NULL, "{first}", "{last}", "{gender}", "{birthdate}");"""

    sql_command = format_str.format(first=p[0], last=p[1], gender=p[2], birthdate = p[3])
    cursor.execute(sql_command)

See more here.

.

Dave
  • 299
  • 1
  • 7
  • No I was not asking for the python code. I asked how can I create a compact query filter with the WHERE clause where the condition is being from a local table. Say based on the table I provided if each of the conditions in each row are met like ( x1 & y1 & z1 or x2 & y2 & z2 or x3 & y3 & z3 ..) – Stan Oct 12 '18 at 21:05