[Tutor] Rapidly Importing CSVs with Python into MySQL

Fred G bayespokerguy at gmail.com
Fri Jul 20 22:34:20 CEST 2012


Hi--

This question has to do with MySQL but is fundamentally a python question,
so apologies if it seems tangential initially...

I've written a long python file that creates all the tables in database UN.
 Here is the first few lines:
import MySQLdb as mysql
statement = """CREATE DATABASE UN"""
db = mysql.connect(this stuff is correct)
cursor = db.connect()
cursor.execute(statement)
sql = """CREATE TABLE UN.1(
         id VARCHAR(255),
         name VARCHAR(255),
         age INT
       )"""
cursor.execute(sql)
statement = """LOAD DATA LOCAL INFILE...(this stuff is correct)"""
db.commit()
db.close()

db = mysql.connect(this stuff is correct)
cursor = db.connect()
sql = """CREATE TABLE UN.2(
         id VARCHAR(255),
         fname VARCHAR(255),
         lname VARCHAR(255),
         age INT
       )"""
cursor.execute(sql)
statement = """LOAD DATA LOCAL INFILE...(this stuff is correct)"""
db.commit()
db.close()

UN.3, UN.4, UN.5...etc...

I have a lot of tables here and I have a few questions:
1) When I run this Python file, only the first table is created.  Why does
the interpreter think that the statements are done at this point? I tried
experimenting with taking out all the "db.close()" commands until the last
one, but that didn't work, either.  More importantly, how can I get it such
that I can press run all and all these tables are built?  I'm going to be
re-building these (and other database's tables) pretty frequently, so it's
pretty frustrating to have to copy and paste out of the .py file into the
idle editor every single statement...

2) Is there a shortkey command to run line-by-line in a file using IDLE in
Python 2.7? (Some languages have this capacity, but a Google search of
python was not too promising about this feature...)

Thanks so much
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120720/4980d04e/attachment-0001.html>


More information about the Tutor mailing list