0

I am relatively new to SQL and sqlite3 in general, and have a question about using question mark or named style in executions.

I have several databases with a table called structures, and I have made a method in a class in Python that extracts the n'th value of some column using

def get_nth(self, col_name, n):
    cursor = self.con.cursor()
    if n == -1:
        cursor.execute(f"SELECT {col_name} FROM structures ORDER BY id DESC LIMIT 1")
    else:
        cursor.execute(f"SELECT {col_name} FROM structures WHERE id == {n}")

    try:
        val = cursor.fetchall()[0][0]
    except IndexError as e:
        print("Faulty database - no 'structures' table found")
        raise e
    return val

Having googled about other sqlite3-related problems, I found that executing with f-strings or str.format will be vulnerable to injection attacks, and therefore I tried altering the execution, according to Cursor Objects, to:

if n == -1:
    cursor.execute("SELECT ? FROM structures ORDER BY id DESC LIMIT 1", (col_name, ))
else:
    cursor.execute("SELECT ? FROM structures WHERE id == ?", (col_name, n))

print(cursor.fetchall()[0][0])

This outputs the column name itself instead of the actual value, since .execute uses 'id' and not id if col_name='id'.

Question:

Is there a way to use question mark or named style in this way?

1
  • Use a hybrid: cursor.execute("SELECT {col_name} FROM structures WHERE id == ?", (col_name, n)). The bit to protect against injection is the value, not nessecarily the column name. Protect the value, and the column name is no longer an issue. Commented Apr 3, 2020 at 11:08

2 Answers 2

1

In agreement with the answer from @SergeBallesta, you can do something like:

sql = 'SELECT {col_name} FROM structures WHERE id = ?'.format(col_name=colname)
cursor.execute(sql, [n])

Which uses a hybrid of the two conventions.

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

Comments

1

No. You can only pass data in parameterized queries, but neither SQL words like INSERT, SELECTor UPDATE, nor names like table or field names.

Comments

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.