python - Using '?' placeholder in sqlite3 statements -


so reason getting errors when using placeholders in select statements.

def get_id(table_name, id_name):     db = sqlite3.connect('test_db')     max_id = db.execute('''select max(?) ?''', (id_name, table_name)).fetchone()[0]     if not max_id:         primary_id = 0     else:         primary_id = max_id + 1 

this functions returns error:

file "test.py", line 77, in get_id max_id = db.execute('''select max(?) ?''', (id_name, table_name)).fetchone()[0] sqlite3.operationalerror: near "?": syntax error 

also in case matters:

id_name = 'pos_id' table_name = 'poscar' 

you aren't able use placeholders column or table names. placeholders values used insert or retrieve data database. library sanitizes them.

to want, try this:

db.execute('''select max({}) {}'''.format(id_name, table_name)).fetchone()[0] 

this use string formatting build query. if need add where condition this, can still using parameters:

db.execute('''select max({}) {} id = ?'''.format(id_name, table_name), id_variable).fetchone()[0] 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -