[Tutor] Rapidly Importing CSVs with Python into MySQL

Walter Prins wprins at gmail.com
Sat Jul 21 00:27:41 CEST 2012


Hi Fred,

On 20 July 2012 21:34, Fred G <bayespokerguy at gmail.com> wrote:
> Here is the first few lines:

Rather than paraphrase and omit details that might be relevant to the
problem you're having, please try to whittle down your code to a bare
test program that is runnable by us as-is that demonstrates your
problem.

<snip pseudocode>

There's nothing there to suggest that Python or MySQL should thing
that the statements are done, so I'm supposing some of the stuff you
think is correct may not in fact be.  (But this is all guessing also)

> 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.

Have you tried moving some of the create statements to a prior section
and creating them with the same connection/cursor you use for the
first table that is created correctly?

> 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...

Of course.  There's a bug or bugs and you/we need to fix it. :)

> 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...)

On my Python 2.7 installation (Activestate distribution, in case it
matters), you can run a program line by line in the debugger from Idle
as follows:
1) Open Idle
2) Open the file you want to debug. (File->Open, click filename etc.)
3) On the main interpreter window, click "Debug" then click
"Debugger".  The debugger window appears.
4) Click back to your program window, and press F5 to run it.  The
program is started on the first line in the debugger window and
displays the first line of the program about to be executed.
5) You can now click the buttons at the top to control the program
flow, e.g. "Go", "Step", "Over", "Out" and "Quit", which respectively
(I guess) runs the program without further interruption, steps to the
next line (into functions/methods if needed), steps to the next line
(over function calls if needed), steps to the next line (returning
from the current call, e.g. "out"), and quits the program.

As an aside, I normally use Eclipse with PyDev which has very good
Python debugger integration.  If you'd like to give Eclipse a whirl
sometime and need a hand getting Eclipse set up for Python development
I'd be happy to post a little howto. (It does mean you'll have to
download Java if you don't yet have it, as well as an Eclipse bundle
etc, all of which may perhaps make it less attractive for you, but if
you happen to have a reasonably fast internet connection it's not
*that* much of a hardship...)

Walter


More information about the Tutor mailing list