MySQLdb Help

Venkat Addala venkat.boffin at gmail.com
Tue Sep 17 04:27:40 EDT 2013


hey Ben,

Thanks for your solution, i am done with this before, now i'm having
problem after deleting \n and extra spaces, i want to update it back it
again to mysql db.

For this i tried using regular expression  check this code;

# -*- coding: utf-8 -*-
import re
import MySQLdb

pattern = re.compile('@(.*)@.*$')

conn = MySQLdb.connect(
  host='localhost', user='root',
  passwd='pass123', db='workbench', charset='utf8')

cursor = conn.cursor()

cursor.execute("""
    SELECT * FROM gene where gene_id=13 AND `descrip` regexp "^@.*@.*$"
LIMIT 1""")
rows = cursor.fetchall()
for row in rows:
  rows_new = pattern.match(row[1])
if rows_new.group(1) is not None:
  cursor.execute("""
      update gene set descrip = %s where descrip = %s""",
(rows_new.group(1), row[0]))
      #cursor.execute("""
      #update gene set descrip = %s where descrip = %s""",
(rows_new.group(1), row[0]))

  conn.close()


--
Regards
Boffin





On Tue, Sep 17, 2013 at 1:05 PM, Ben Finney <ben+python at benfinney.id.au>wrote:

> 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
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130917/23692f3a/attachment.html>


More information about the Python-list mailing list