Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:25:39+00:00 2026-06-06T04:25:39+00:00

I am working on a script to parse a csv file and generate input

  • 0

I am working on a script to parse a csv file and generate input for a MySQL table.

I import the data via csv.reader, so every row is a list of strings.
I want to iterate over the rows and put different entries into the database.

I can get the following test to work:

sql = "INSERT INTO `testSmall` (`idtestSmall`, `column1`, `column2`) VALUES (1, 'entry1', 'entry2');"
cursor.execute (sql)

So my SQL connection works and the principle SQL syntax is ok.
I can also access the entries I want to put in there, and they are correct and of the data type I expect.
However, I don’t seem to be able to use the same SQL syntax with variables within the iterations:

allData = csv.reader(open('TestTable.csv', 'rb'), delimiter=',', quotechar='|')
for row in allData:
    sql = "INSERT INTO `testSmall` (`idtestSmall`, `column1`, `column2`) VALUES (row[0], row[1], row[2]);"
    cursor.execute (sql)

This generates a Syntax Error:

Error 1064: You have an error in your SQL syntax; check the manual that corresponds to our MySQL server version for the right syntax to use near '[0], row[1], row[2])' at line 1

But the data types are correct and the SQL syntax is the same as in the working example…

Can anyone tell me what I’m doing wrong and how to make it work?

(In the end, I want to not only insert the pure csv entries but also derived values, which is why I’m not just using mysql bulk import.)

Thanks in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:25:41+00:00Added an answer on June 6, 2026 at 4:25 am

    use:

    sql = "INSERT INTO `testSmall` (`idtestSmall`, `column1`, `column2`) VALUES (?, ?, ?);"
    cursor.execute (sql, (row[0], row[1], row[2]))
    

    The questionmark is a placeholder. An extra advantage of using placeholders, is that they automatically make your input ‘safe’, by escaping qoutes etc.
    Right now, you are using the row[0], row[1], row[2] as a string with the text “row[0], row[1], row[2]”, instead of telling python to use the values of these variables.


    Also, if you want to use rows of multiple lengths, or if you want to be able to easily change the size of your input list, you can dynamically create the placeholders:

    sql = "INSERT INTO testSmall VALUES (%s);" % ', '.join('?' for _ in row)
    cursor.execute (sql, row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am working on script which creates events in Google Calendar from data in
So, i'm working on a progress bar for a CSV processing script. dnc_scrubber.php goes
I have a python script which should parse a file and produce some output
At the moment I'm working on a quite tricky transferring from a .csv file
I am working on a script to parse a certain fault code out of
I'm working on a script which involves continuously analyzing data and outputting results in
Still working on this part of my script to parse my JSON jsonArray =
I'm currently writing a simple .sh script to parse an Exim log file for
I'm working on a Python script to parse Squid(http://www.squid-cache.org/) log files. While the logs
im working on a script that sends a few data stored using GET to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.