0

I'm trying to input variables into a SQL table these variables are set above in the code. This is what's causing me the issue:

c.execute("INSERT INTO weather VALUES (temp, hum, pres)")
3
  • what library are you using? Commented Jan 5, 2020 at 10:08
  • im using sqlite3 Commented Jan 5, 2020 at 10:08
  • Can you provide the code where you set your variables? Commented Jan 5, 2020 at 10:34

2 Answers 2

2

Taken from the documentation over at sqlite documentation

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

NEVER DO THIS! IT WILL MAKE YOUR CODE VUNERABLE TO SQL INJECTION ATTACKS

symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

or

c.execute(f"SELECT * FROM stocks WHERE symbol = '{symbol}'") 

NEVER DO THIS! IT WILL MAKE YOUR CODE VUNERABLE TO SQL INJECTION ATTACKS


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

Comments

1

You can use

c.execute("INSERT INTO weather VALUES (?, ?, ?)", temp, hum, pres)

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.