DB beginner help

Discussion in 'Python' started by Zeljko Vrba, Aug 5, 2004.

  1. Zeljko Vrba

    Zeljko Vrba Guest

    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?)
     
    Zeljko Vrba, Aug 5, 2004
    #1
    1. Advertising

  2. "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!
     
    Frithiof Andreas Jensen, Aug 5, 2004
    #2
    1. Advertising

  3. "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.
     
    Frithiof Andreas Jensen, Aug 5, 2004
    #3
  4. Zeljko Vrba

    David Cook Guest

    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/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
     
    David Cook, Aug 5, 2004
    #4
  5. Zeljko Vrba

    ziaran Guest

    AVI I/O

    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
     
    ziaran, Aug 5, 2004
    #5
    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. tripwater

    Help with Visual Studio (beginner)

    tripwater, Feb 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,463
    Amit Bahree
    Mar 9, 2005
  2. Linas Petras

    Beginner Help

    Linas Petras, Sep 23, 2004, in forum: VHDL
    Replies:
    3
    Views:
    591
    Linas Petras
    Sep 25, 2004
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    597
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    984
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    226
    7stud --
    Apr 12, 2011
Loading...

Share This Page