An mysql-python tutorial?

Discussion in 'Python' started by Dfenestr8, Jan 28, 2005.

  1. Dfenestr8

    Dfenestr8 Guest

    Hi.

    Been told by the admin of my (free!) server that he'd rather I should
    learn to use mysql if I want to continue writing cgi scripts there.

    Not even sure exactly what mysql is.

    Is there a simple tutorial anywhere on the web about using python + mysql?
    Dfenestr8, Jan 28, 2005
    #1
    1. Advertising

  2. Dfenestr8

    Kartic Guest

    Dfenestr8 said the following on 1/28/2005 5:21 PM:
    > Hi.
    >
    > Been told by the admin of my (free!) server that he'd rather I should
    > learn to use mysql if I want to continue writing cgi scripts there.
    >
    > Not even sure exactly what mysql is.
    >
    > Is there a simple tutorial anywhere on the web about using python + mysql?


    Did you try googling?

    There are two items in the very first page that should be of use to you.
    One is the Python-MySQL module, which you should have installed.

    http://sourceforge.net/projects/mysql-python

    There is an examples directory that has some concrete stuff to help you
    learn.

    Another hit is a basic tutorial, simple to follow and learn :-
    http://images.devshed.com/Server_Side/Python/PythonMySQL/PythonMySQL.pdf

    And here is one more site, good stuff here too:-
    http://www.kitebird.com/articles/pydbapi.html

    You might also want to check the "Charming Python" pages at IBM.com but
    I dont know if they have articles covering mysql.

    HTH!

    Thanks,
    --Kartic
    Kartic, Jan 29, 2005
    #2
    1. Advertising

  3. Dfenestr8

    EuGeNe Guest

    EuGeNe, Jan 29, 2005
    #3
  4. Dfenestr8

    Andy Dustman Guest

    It's a pretty good tutorial, thought I would recommend you forget about
    the fetchone() example. The example below demonstrates three additional
    features that will make your life easier: MySQL option files, tuple
    unpacking, and cursors as iterators (fourth feature: the default host
    is localhost; this is rapidly turning into the Spanish Inquisition
    sketch):

    #!/usr/bin/python
    import MySQLdb
    db = MySQLdb.connect(db="db56a", read_default_file="~/.my.cnf")
    cursor = db.cursor()
    cursor.execute("SELECT name, species FROM animals")
    for name, species in cursor:
    print name, "-->", species

    (I also shy away from doing SELECT *; what if your schema changes?)
    (If the indentation is hosed, blame Google Groups.)
    Andy Dustman, Jan 29, 2005
    #4
  5. Dfenestr8

    Dfenestr8 Guest

    On Sat, 29 Jan 2005 06:41:37 +0000, Kartic wrote:

    [snip]
    > And here is one more site, good stuff here too:-
    > http://www.kitebird.com/articles/pydbapi.html
    >


    Hi.

    I followed the instructions there, tried out the test script they
    recommend.

    Can you tell me why this command, in the python interpreter:

    >>>conn = MySQLdb.connect (host = "localhost", user = "flipper", passwd =

    "[hidden]", db = "mydb")

    produces this error:
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib/python2.3/site-packages/MySQLdb/__init__.py", line 63, in Connect
    return apply(Connection, args, kwargs)
    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 115, in __init__
    self._make_connection(args, kwargs2)
    File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 41, in _make_connection
    apply(super(ConnectionBase, self).__init__, args, kwargs)
    _mysql_exceptions.OperationalError: (1045, "Access denied for user:
    'flipper@localhost' (Using password: YES)")

    I also have MySQL installed, and tried setting up the user flipper with
    the mysql client, as per the instructions here:
    http://vsbabu.org/mt/archives/2003/04/17/mysql_in_mandrake_91.html
    Dfenestr8, Jan 29, 2005
    #5
  6. Dfenestr8

    Andy Dustman Guest

    It definitely looks like an access control problem; recheck your grants.
    Andy Dustman, Jan 30, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rensjuh
    Replies:
    7
    Views:
    949
    Mabden
    Sep 2, 2004
  2. Ravi
    Replies:
    6
    Views:
    1,396
    Suchandra Thapa
    Jul 21, 2003
  3. Replies:
    2
    Views:
    6,173
  4. W. Watson
    Replies:
    13
    Views:
    1,046
    W. Watson
    Sep 20, 2007
  5. Linuxmank
    Replies:
    36
    Views:
    1,473
    Linuxmank
    Aug 8, 2009
Loading...

Share This Page