0

I am writing a python script to search if an element pre-exists in my sql database.

 def create_connection(db_file):
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
    except Error as e:
        print(e)
    main(conn)    #function with inserts creates table and inserts values and calls find_values() function

def find_values(conn):
    sql = ''' SELECT link_ID from links_table where link_ID="l_1234"  '''
    conn.execute(sql)
    conn.commit()

Here I am comparing to something which I have already entered in my code like "l_1234". How will I compare it to something which is dynamic like a user entered variable? Also how will I find if the element was found?

1 Answers1

0

Have a look to this post :

How do I use SQL parameters with python?

or this one :

How to use variables in SQL statement in Python?

From one of the last post:

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()
Thomas
  • 1,231
  • 14
  • 25