DB beginner help

Z

Zeljko Vrba

1. Is there any HOW-TO documentation about DB API except PEP 249?

2. How are you supposed to write DB-driver independent code when each
driver has its own, possibly different from any other, paramstyle
('qmark', 'numeric', etc..)?

In Perl I always use question marks and have no trouble switching the
application between different databases. How do you guys do it in Python
(of course, without rewriting all queries?)
 
F

Frithiof Andreas Jensen

In Perl I always use question marks and have no trouble switching the
application between different databases. How do you guys do it in Python
(of course, without rewriting all queries?)

Do your database work from a "connection" object and use a database-specific
function to create that object. Most of the time that will work with several
SQL databases.

I use a class wrapper for the specific database that is given a connection
object when the class is initialised. The wrapper hides the lameness of SQL
in some obvious situations - f.ex calculating a running balance like a bank
statement is easiest done by the application, but procedures will work also.

I also think that a dicts are a good way to "paste" information into SQL
query strings

It goes sort of like:

# open the stock database and insert the information.
try:
cursor = self.conn.cursor()
cursor.execute(
"""
INSERT INTO stocks (ticker, exchange, stockName,
stockDescription)
VALUES ('%(Ticker)s', '%(Exchange)s', '%(stockName)s,
'%(stockDescription)s)
""" % colkey)
except:
log.error(
"""
Failed to create new Entry for %(Ticker)s, on %(Exchange)s'
""" % colkey, exc_info=True)
raise
else:
# account opened.
self.commit(True)
log.info('Entry Created for %(Ticker)s, on %(Exchange)s'
% colkey)
# done
return None

...... Funny how every amateur programmer seem to dabble in stocks :)

The "connection" is the standardised way of representing a database as an
object in Python. *How* to get the connection is database-dependent. You
might also be bitten by the various interpretations of SQL in the underlying
databases; Time/Date fields are always dubious, some databases only work
with few data types, even Strings Only(!), and some corners of SQL like
CHECK constraints (PySQLite) and even FOREIGN KEY constraints (Seen it on
something, I discarded for that reason - GadFly?) may not be enforced!
 
F

Frithiof Andreas Jensen

"Frithiof Andreas Jensen" <frithiof.jensen@die_spammer_die.ericsson.com>
wrote in message
BTW, the 's in the query string are wrong.
 
D

David Cook

2. How are you supposed to write DB-driver independent code when each
driver has its own, possibly different from any other, paramstyle
('qmark', 'numeric', etc..)?

This recipe might help:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278612

I haven't tried it.
In Perl I always use question marks and have no trouble switching the
application between different databases. How do you guys do it in Python
(of course, without rewriting all queries?)

pyformat is the most useful IMO, e.g.

cursor.execute(
"insert into foo (baz, quux) values (%(baz)s, %(quux)s)",
{'parrot' : 'deceased', 'quux' : "O'Reilly", 'baz' : 1})

The values for 'baz' and 'quux' will be interpolated and quoted correctly,
and 'parrot' will be ignored.

A few adapters have a dictfetch method, but for those that don't, getting
your data out in dict form is a two liner:

fieldnames = [tup[0] for tup in cursor.description]
dictrows = [dict(zip(fieldnames, row)) for row in cursor.fetchall()]

Dave Cook
 
Z

ziaran

Hello,

I want to use Python for Image proccessing.
I need the following capabilities:

1. To be able to read and WRITE an AVI file frame by frame.
2. To be able to access each fram as a simple matrix.

I tried to search for that in google but could not find something
intuitive and simple.

The matrices part is important since iamge processing is done through
matrices manipulation.

MATLAB is an example of a tool with scripting capabilities in which you
can do that in a simple way.

Is Python adequate for that?

Thanks,
Nir
 

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,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top