mySQLdb

Discussion in 'Python' started by m0226065, Aug 26, 2004.

  1. m0226065

    m0226065 Guest

    Ok

    so I have a mySQL server
    and mySQLdb as an interface (do I us this term correct here?) to this server
    from python
    now I have a game:
    I want to read a file into the db
    I want other data to be kept in the db from now on

    problem:
    since I have special things I want to do (like write a dict into a db), do I
    have to write a specialized class, derived from mySQLdb, or would that be
    dumb? I mean, I guess it would be easier to write some functions that take
    care of everything, while I just say "write this dict to that table" or
    "write this dict item to that table" and so on.

    I know there are things like ZOPE, but I just want to get an understanding
    of this...



    --
    ____________________________________________________
    Johan Potums






    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.737 / Virus Database: 491 - Release Date: 12/08/2004
    m0226065, Aug 26, 2004
    #1
    1. Advertising

  2. m0226065 schrieb:
    > so I have a mySQL server
    > and mySQLdb as an interface (do I us this term correct here?) to this server
    > from python
    > now I have a game:
    > I want to read a file into the db
    > I want other data to be kept in the db from now on
    >
    > problem:
    > since I have special things I want to do (like write a dict into a db), do I
    > have to write a specialized class, derived from mySQLdb, or would that be
    > dumb? I mean, I guess it would be easier to write some functions that take
    > care of everything, while I just say "write this dict to that table" or
    > "write this dict item to that table" and so on.
    >

    You can't derive a class from mySQLdb, because that is a module
    containing several classes. If I understand your description correctly,
    you would need functions that
    - convert a data structure, for example a dict or a dict item, to
    parameters suitable for a SQL query,
    - execute (and possibly construct) the right query with those parameters.

    Right? That might quite well be encapsulated into a class, but I don't
    think such a class would have an "is-a" relationship to a MySQLdb class.
    I'd rather give it a MySQLdb.cursor and possibly a MySQL.connection
    instance as attributes.

    Koczian

    --
    Dr. Sibylle Koczian
    Universitaetsbibliothek, Abt. Naturwiss.
    D-86135 Augsburg

    Tel.: (0821) 598-2400, Fax : (0821) 598-2410
    e-mail : -Augsburg.DE
    Sibylle Koczian, Aug 27, 2004
    #2
    1. Advertising

  3. m0226065

    Piet Guest

    Hi Johan,
    I am afraid that there is no simple answer to your (very broad)
    question. However, that doesn´t mean that the idea behind getting data
    from python into a MySql database is inherently difficult. It is just
    not a single step.
    As Sibylle pointed out, subclassing MySQLdb is not possible. From what
    I have understood so far, this module is more or less an interface
    that you can use to type SQL commands. That is, IMHO this module is of
    very limited use when you are not familiar with the mysql syntax. The
    second thing that you have to know (which is a prerequisite for
    profiting from the SQL statements) that you are familiar in general
    with the idea of relational databases.
    When these conditions are fulfilled, everything is quite ease. Lets us
    assume that you have a table named "persons" with the columns
    "firstname" and "lastname" and a dictionary "people" with the content
    {"Johan":"Potums";"Pit":"Grinja"} and you want to get that data in the
    database, then you would make a connection to the database and create
    a cursor as follows:
    server = MySQLdb.connect()
    cursor = server.cursor
    for name in {"Johan":"Potums";"Pit":"Grinja"}.keys():
    cursor.execute("INSERT into persons (name,vorname) "+ name + ","
    +people[name])
    or something like that.
    Depending on where you start, it can take some time to get used to
    these concepts, but believe me, it is worth it!
    Best wishes

    Peter
    Piet, Aug 27, 2004
    #3
    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. Alan Little

    can't import MySQldb on OS X

    Alan Little, Jun 25, 2003, in forum: Python
    Replies:
    0
    Views:
    839
    Alan Little
    Jun 25, 2003
  2. Skip Montanaro

    Re: Problem with MySQLdb on Mac OS X...

    Skip Montanaro, Jul 12, 2003, in forum: Python
    Replies:
    5
    Views:
    386
    Michael S. Jessop
    Jul 16, 2003
  3. Mike M
    Replies:
    1
    Views:
    329
  4. Dave Harrison

    MySQLdb and egenix mxDateTime dumps core

    Dave Harrison, Aug 5, 2003, in forum: Python
    Replies:
    0
    Views:
    336
    Dave Harrison
    Aug 5, 2003
  5. Dave Harrison

    using like and % in MySQLdb

    Dave Harrison, Aug 7, 2003, in forum: Python
    Replies:
    0
    Views:
    312
    Dave Harrison
    Aug 7, 2003
Loading...

Share This Page