mySQLdb

M

m0226065

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

Sibylle Koczian

m0226065 said:
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 : (e-mail address removed)-Augsburg.DE
 
P

Piet

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top