vendredi 26 février 2016

Selecting a specific column from sqllite database returns column name when passed in qmark format in Python

I try to select a random cell from the following sqllite table

  consonants   a    b   c
0          d  ad   bd  cd
1          e  ae  NaN  ce
2          f  af   bf  cf

First I generate a random column c from ['a','b','c'] and a random row r from ['d','e','f']. This works so far.

However, the following select statement always returns the value of c instead of the values of the column specified by c.

x = conn.execute('select ? from syllables WHERE consonants=?',(c,r)).fetchall()
# Returns [('b',)] if c='b' and r='f'

I verified, that the WHERE clause is correct by selecting all columns *.

x = conn.execute('select * from syllables WHERE consonants=?',(r)).fetchall()
# Returns [('f', 'af', 'bf', 'cf')] if r='f'

It also works if I manually retrieve a specific column like c='b'

x = conn.execute('select b from syllables WHERE consonants=?',(r)).fetchall()
# Returns [('bf',)] if r='f'

I do not really get why it always returns the value c when passing it in qmark notation.

Aucun commentaire:

Enregistrer un commentaire