Is it possible to change %s to variable when importing csv to mysql?
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
|
show 5 more comments
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
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
|
show 5 more comments
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
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
python mysql csv
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
|
show 5 more comments
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
|
show 5 more comments
1 Answer
1
active
oldest
votes
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:])
finally! thanks for helping me! row[0] = change is what I think the best
– Jeremy Adrian de Vera
Nov 14 '18 at 8:32
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:])
finally! thanks for helping me! row[0] = change is what I think the best
– Jeremy Adrian de Vera
Nov 14 '18 at 8:32
add a comment |
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:])
finally! thanks for helping me! row[0] = change is what I think the best
– Jeremy Adrian de Vera
Nov 14 '18 at 8:32
add a comment |
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:])
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:])
answered Nov 14 '18 at 8:17
deceze♦deceze
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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