Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

small improvements for having dictionaries and unicode

CUBRID Official Forum » Programming Languages » Python API » small improvements for having dictionaries and unicode

author
message
[Level:0]dragosh_r0ck

Post subject: small improvements for having dictionaries and unicode

registered: 09/08/2012

IP: *.96.215.239

views: 4

Hi there,

In a old version of the python driver there was the possibility to have a record in the result set as a tuple or as a dictionary.

1. in the cursors.py add:

def tlzToUnicodeEx2(s):
    #convert to unicode
    if isinstance(s, unicode): return s
    else:
        try:
            return unicode(s, 'utf-8')
        except:
            try:
                return unicode(s, 'latin-1')
            except:
                return s

class DictCursor(Cursor):
    def fetchone(self):
        r = {}
        data = Cursor.fetchone(self)
        if not data:
            return r;

        desc = self._cs.description

        for i in range(len(desc)):
            name = desc[i][0]
            r[name] = tlzToUnicodeEx2(data[i])

        return r


2. in the connections.py replace the method cursor with:

    def cursor(self, dictCursor=0):
        if dictCursor:
            cursorClass = DictCursor
        else:
            cursorClass = Cursor
        return cursorClass(self._db.cursor())


How to use it: if you pass 1 to the cursor method then the result set will contain dictionaries and the values can be extracted by name and not by index number in the set. This is very useful if you add more field in the select clause and the order is changed.

Also, because in my web application I work with unicodes I really need that values from the database to be unicodes - this is what tlzToUnicodeEx2 does.


I attached the 2 py files. Thank you very much Ovidiu for your support on this issue.


Hope this will help,
Dragos


Attachment:

Quote
[Level:0]dragosh_r0ck

# Post subject:Re: small improvements for having dictionaries and unicode

profile

registered: 09/08/2012

IP: *.96.215.239

PS:

actually those try...except blocks are not needed if you are sure that all the data inserted is utf8 encoded; it will be faster to remove them.

Quote
[Level:8]CUBRID

# Post subject:Re: small improvements for having dictionaries and unicode

profile

registered: 03/29/2010

IP: *.181.249.254

The development team has been working on the matter and as far as it is currently known, the python driver for CUBRID Apricot will have the tuple feature as well (among other bug fixes and improvements).

Thank you for noticing and mentioning the issue!

Best regards,

The CUBRID Team

Quote




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: