0

I am trying to optimize this block of code to use a single query rather than looping over and over.

while not (dataX):
            i += 1
            this_id = '/'.join(this_id.split('/')[0:-i])
            if not this_id:
                break
            else:
                dataX = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref = %s AND _ntype = 'code' LIMIT 1;", data = (this_id,))

I want to use the IN clause with a variable that contains all possible substring but I can't get it to work.

this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"
result = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref IN($this_id_list)")

Any idea what am I doing wrong and how to fix it? I would really appreciate any input! This is a Python script btw.

2
  • what does $this_id_list look like? The IN clause is expecting comma delimited values (and quoted if strings). Commented Sep 21, 2017 at 17:01
  • @KevinBott this_id_list = ['/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'] Commented Sep 21, 2017 at 17:13

1 Answer 1

1
this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"

This should be a string

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried that and it still didn't work. Any other ideas?
What result do you actually get?
When I run it, result ends up being False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.