Is it possible to use variables in SQL statement in Python?

0 votes
Suppose there is a statement in SQL "INSERT INTO table VALUES var1, var2, var3," where var1 and var2 are strings and var3 is an integer. So now my concern is How can I write the variable names without python including them as part of the query text?
Aug 21, 2019 in Python by Arvind
• 2,980 points
1,962 views

1 answer to this question.

0 votes
cursor.execute("INSERT INTO table VALUES (%s, %s, %s)", (var1, var2, var3))

I think this should solve your query. Here the parameters are passed as a tuple.

answered Aug 21, 2019 by Neel
• 3,010 points

Related Questions In Python

0 votes
1 answer

Is it possible in Python requests to print entire HTTP request?

A better idea is to use the ...READ MORE

answered Nov 26, 2018 in Python by Nymeria
• 3,520 points
5,756 views
0 votes
1 answer
0 votes
4 answers

What is a Tuple in Python and how to use it?

Tuples  are a  Unchanging sequence of values, ...READ MORE

answered Jun 21 in Python by sahil
• 540 points
454 views
0 votes
1 answer

is it possible to do method overloading in python?

You cannot have two methods with the ...READ MORE

answered Mar 25, 2019 in Python by Mohammad
• 3,210 points
91 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,451 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,085 views
0 votes
1 answer

Is it possible to print a string and a variable in a single print statement in Python?

This can be done using simple string ...READ MORE

answered Jul 15, 2019 in Python by Neel
• 3,010 points
149 views
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26, 2019 in Python by Neel
• 3,010 points
162 views