Is it possible to change %s to variable when importing csv to mysql?










-3















I have this code to import my csv to mysql database:



import csv
import mysql.connector
import re

change = "hello"
mydb = mysql.connector.connect(user='root', password='',
host='localhost',
database='jeremy_db')
file = open('C:\Users\trendMICRO\Desktop\OJT\test.csv', 'rb')
csv_data = csv.reader(file)
mycursor = mydb.cursor()
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO table_test(col1,col2,col3,col4)' 'VALUES(%s, %s, %s, %s)',row)
mydb.commit()
cursor.close()
print("Done")


Can I change the values of %s into my variable change = "hello"?



Please help me I'm new to python, I tried every solution but I can't find how to change the values. I tried this VALUES(hello, %s, %s, %s)',row) but it doesn't work










share|improve this question

















  • 1





    In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

    – deceze
    Nov 14 '18 at 7:00











  • This snippet looks like the right way to use bind variables. What exactly is the problem?

    – Mureinik
    Nov 14 '18 at 7:00











  • I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:01











  • I don't know what's with the downvote?

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:06











  • Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

    – dsgdfg
    Nov 14 '18 at 7:06















-3















I have this code to import my csv to mysql database:



import csv
import mysql.connector
import re

change = "hello"
mydb = mysql.connector.connect(user='root', password='',
host='localhost',
database='jeremy_db')
file = open('C:\Users\trendMICRO\Desktop\OJT\test.csv', 'rb')
csv_data = csv.reader(file)
mycursor = mydb.cursor()
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO table_test(col1,col2,col3,col4)' 'VALUES(%s, %s, %s, %s)',row)
mydb.commit()
cursor.close()
print("Done")


Can I change the values of %s into my variable change = "hello"?



Please help me I'm new to python, I tried every solution but I can't find how to change the values. I tried this VALUES(hello, %s, %s, %s)',row) but it doesn't work










share|improve this question

















  • 1





    In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

    – deceze
    Nov 14 '18 at 7:00











  • This snippet looks like the right way to use bind variables. What exactly is the problem?

    – Mureinik
    Nov 14 '18 at 7:00











  • I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:01











  • I don't know what's with the downvote?

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:06











  • Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

    – dsgdfg
    Nov 14 '18 at 7:06













-3












-3








-3








I have this code to import my csv to mysql database:



import csv
import mysql.connector
import re

change = "hello"
mydb = mysql.connector.connect(user='root', password='',
host='localhost',
database='jeremy_db')
file = open('C:\Users\trendMICRO\Desktop\OJT\test.csv', 'rb')
csv_data = csv.reader(file)
mycursor = mydb.cursor()
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO table_test(col1,col2,col3,col4)' 'VALUES(%s, %s, %s, %s)',row)
mydb.commit()
cursor.close()
print("Done")


Can I change the values of %s into my variable change = "hello"?



Please help me I'm new to python, I tried every solution but I can't find how to change the values. I tried this VALUES(hello, %s, %s, %s)',row) but it doesn't work










share|improve this question














I have this code to import my csv to mysql database:



import csv
import mysql.connector
import re

change = "hello"
mydb = mysql.connector.connect(user='root', password='',
host='localhost',
database='jeremy_db')
file = open('C:\Users\trendMICRO\Desktop\OJT\test.csv', 'rb')
csv_data = csv.reader(file)
mycursor = mydb.cursor()
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO table_test(col1,col2,col3,col4)' 'VALUES(%s, %s, %s, %s)',row)
mydb.commit()
cursor.close()
print("Done")


Can I change the values of %s into my variable change = "hello"?



Please help me I'm new to python, I tried every solution but I can't find how to change the values. I tried this VALUES(hello, %s, %s, %s)',row) but it doesn't work







python mysql csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 6:56









Jeremy Adrian de VeraJeremy Adrian de Vera

837




837







  • 1





    In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

    – deceze
    Nov 14 '18 at 7:00











  • This snippet looks like the right way to use bind variables. What exactly is the problem?

    – Mureinik
    Nov 14 '18 at 7:00











  • I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:01











  • I don't know what's with the downvote?

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:06











  • Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

    – dsgdfg
    Nov 14 '18 at 7:06












  • 1





    In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

    – deceze
    Nov 14 '18 at 7:00











  • This snippet looks like the right way to use bind variables. What exactly is the problem?

    – Mureinik
    Nov 14 '18 at 7:00











  • I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:01











  • I don't know what's with the downvote?

    – Jeremy Adrian de Vera
    Nov 14 '18 at 7:06











  • Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

    – dsgdfg
    Nov 14 '18 at 7:06







1




1





In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

– deceze
Nov 14 '18 at 7:00





In this particular case, you wouldn't use a variable in the first place, you'd just hardcode the value in the query: '... VALUES("hello", %s, %s, ...)'.

– deceze
Nov 14 '18 at 7:00













This snippet looks like the right way to use bind variables. What exactly is the problem?

– Mureinik
Nov 14 '18 at 7:00





This snippet looks like the right way to use bind variables. What exactly is the problem?

– Mureinik
Nov 14 '18 at 7:00













I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

– Jeremy Adrian de Vera
Nov 14 '18 at 7:01





I tried that using hardcode but gives me ProgrammingError: Not all parameters were used in the SQL statement

– Jeremy Adrian de Vera
Nov 14 '18 at 7:01













I don't know what's with the downvote?

– Jeremy Adrian de Vera
Nov 14 '18 at 7:06





I don't know what's with the downvote?

– Jeremy Adrian de Vera
Nov 14 '18 at 7:06













Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

– dsgdfg
Nov 14 '18 at 7:06





Create variable on outside, like: var1= "blah blah" ; var2="heyooo"; cursor.execute(var1,var2)

– dsgdfg
Nov 14 '18 at 7:06












1 Answer
1






active

oldest

votes


















0














For this very particular case, you'd simply hardcode the value in the query:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row)


Now you have only three placeholders in your query though, so you need to ensure that row is a list with three items. E.g.:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row[1:])


If you really wanted to use a variable, you could do something like:



row[0] = change
cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', row)


Or:



cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', [change] + row[1:])





share|improve this answer























  • finally! thanks for helping me! row[0] = change is what I think the best

    – Jeremy Adrian de Vera
    Nov 14 '18 at 8:32











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294654%2fis-it-possible-to-change-s-to-variable-when-importing-csv-to-mysql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














For this very particular case, you'd simply hardcode the value in the query:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row)


Now you have only three placeholders in your query though, so you need to ensure that row is a list with three items. E.g.:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row[1:])


If you really wanted to use a variable, you could do something like:



row[0] = change
cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', row)


Or:



cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', [change] + row[1:])





share|improve this answer























  • finally! thanks for helping me! row[0] = change is what I think the best

    – Jeremy Adrian de Vera
    Nov 14 '18 at 8:32
















0














For this very particular case, you'd simply hardcode the value in the query:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row)


Now you have only three placeholders in your query though, so you need to ensure that row is a list with three items. E.g.:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row[1:])


If you really wanted to use a variable, you could do something like:



row[0] = change
cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', row)


Or:



cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', [change] + row[1:])





share|improve this answer























  • finally! thanks for helping me! row[0] = change is what I think the best

    – Jeremy Adrian de Vera
    Nov 14 '18 at 8:32














0












0








0







For this very particular case, you'd simply hardcode the value in the query:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row)


Now you have only three placeholders in your query though, so you need to ensure that row is a list with three items. E.g.:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row[1:])


If you really wanted to use a variable, you could do something like:



row[0] = change
cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', row)


Or:



cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', [change] + row[1:])





share|improve this answer













For this very particular case, you'd simply hardcode the value in the query:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row)


Now you have only three placeholders in your query though, so you need to ensure that row is a list with three items. E.g.:



cursor.execute('INSERT INTO ... VALUES ("hello", %s, %s, %s)', row[1:])


If you really wanted to use a variable, you could do something like:



row[0] = change
cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', row)


Or:



cursor.execute('INSERT INTO ... VALUES (%s, %s, %s, %s)', [change] + row[1:])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 8:17









decezedeceze

397k62540696




397k62540696












  • finally! thanks for helping me! row[0] = change is what I think the best

    – Jeremy Adrian de Vera
    Nov 14 '18 at 8:32


















  • finally! thanks for helping me! row[0] = change is what I think the best

    – Jeremy Adrian de Vera
    Nov 14 '18 at 8:32

















finally! thanks for helping me! row[0] = change is what I think the best

– Jeremy Adrian de Vera
Nov 14 '18 at 8:32






finally! thanks for helping me! row[0] = change is what I think the best

– Jeremy Adrian de Vera
Nov 14 '18 at 8:32




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294654%2fis-it-possible-to-change-s-to-variable-when-importing-csv-to-mysql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo