MySQLdb Help

Ben Finney ben+python at benfinney.id.au
Tue Sep 17 03:35:30 EDT 2013


Venkat Addala <venkat.boffin at gmail.com> writes:

>   I'm new to python

Welcome! Congratulations on choosing Python for programming.

> i am connecting mysql database with python. now i want to do
> sanitation on the database, like to remove "\n", extra spaces blah
> blah.. and update it back to mysql database.


> i was trying somthing, here is my code, can you please provide me
> solution for this..

Thank you for providing a small, complete example.

You should also describe what behaviour you expect, and what behaviour
you're getting instead. What happens, and how are you expecting it to be
different?

> #!/usr/bin/python
> import MySQLdb as mdb
>
> con = mdb.connect('localhost', 'root', 'pass at 123', 'workbench');

There's no need to end Python statements with a semicolon; it's only
confusing to do it.

>   rows = cur.fetchall()
>   for row in rows:

You never use ‘rows’ for anything else, so you may as well forget it and
just iterate directly over the return value::

    for row in cur.fetchall():

>     row_new = row[0].split("\n", " ")

Read the documentation for ‘str.split’ to see what is wrong with the
above call.


You might also be interested in the ‘python-tutor’ forum, specially
designed for beginners with basic questions about Python
<URL:http://mail.python.org/mailman/listinfo/tutor>
<URL:http://dir.gmane.org/gmane.comp.python.tutor>.

Good hunting to you!

-- 
 \         “Sunday: A day given over by Americans to wishing that they |
  `\      themselves were dead and in Heaven, and that their neighbors |
_o__)                        were dead and in Hell.” —Henry L. Mencken |
Ben Finney




More information about the Python-list mailing list