Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > DB beginner help

Reply
Thread Tools

DB beginner help

 
 
Zeljko Vrba
Guest
Posts: n/a
 
      08-05-2004
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?)


 
Reply With Quote
 
 
 
 
Frithiof Andreas Jensen
Guest
Posts: n/a
 
      08-05-2004

"Zeljko Vrba" <> wrote in message
news:...

> 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!




 
Reply With Quote
 
 
 
 
Frithiof Andreas Jensen
Guest
Posts: n/a
 
      08-05-2004

"Frithiof Andreas Jensen" <frithiof.jensen@die_spammer_die.ericsson.com>
wrote in message news:cesp5u$sha$...

BTW, the 's in the query string are wrong.


 
Reply With Quote
 
David Cook
Guest
Posts: n/a
 
      08-05-2004
On 2004-08-05, Zeljko Vrba <> wrote:

> 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/Coo.../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
 
Reply With Quote
 
ziaran
Guest
Posts: n/a
 
      08-05-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
Help a beginner!!! =?Utf-8?B?U2Ft?= Wireless Networking 4 04-21-2005 06:50 AM
Help with Visual Studio (beginner) tripwater ASP .Net 3 03-09-2005 06:00 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments