Omar Jarkas
Omar Jarkas

Reputation: 110

Generating insert statement for a csv file

I'm trying to implement a Python application based on certain specifications.

One of the specifications is to generate a SQL insert statement for all rows in a CSV file.

I'm not sure what this specification means exactly. Should I iterate over the row and put them in a SQL insert format only or should I do something else? I didn't find an interesting resource online that could have helped me.

Does anyone know what the specification means exactly?

Thank you

Upvotes: 1

Views: 316

Answers (1)

mackorone
mackorone

Reputation: 1066

iterate over the row and put them in an SQL insert format only

Yep, this sounds right. First parse your CVS files into a list of tuples (data) and then use MySQLdb to actually insert the rows via the executemany method. Here's an example from the MySQLdb documentation:

data = [
    ('Jane', date(2005, 2, 12)),
    ('Joe', date(2006, 5, 23)),
    ('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)

Upvotes: 1

Related Questions