Re: Querying MariaDB from python

2018-10-02 Thread Thomas Jollans
On 2018-10-02 18:07, Tony van der Hoff wrote:
> On 02/10/18 16:47, Ervin Hegedüs wrote:
>> hi,
>>
>> now rows will looks like this:
>> ({'id':...,...},{'id':...,}...)
> 
> Thanks Ervin, but:
> 
>cursor = cnx.cursor(pymysql.cursors.DictCursor)
> NameError: name 'pymysql' is not defined
> 
> I have been using the mysql.connector module, which seems to be the
> "official" python interface. I hadn't spotted the pymysql module. Is the
> consensus here that pymysql is the preferred connector?

I don't know, but it appears to be (the?) one that solves your problem.

-- 
https://mail.python.org/mailman/listinfo/python-list


[SOLVED] Re: Querying MariaDB from python

2018-10-02 Thread Tony van der Hoff
On 02/10/18 17:13, Larry Martell wrote:
> On Tue, Oct 2, 2018 at 12:09 PM Tony van der Hoff  
> wrote:
>>
>> On 02/10/18 16:47, Ervin Hegedüs wrote:
>>> hi,
>>>
>>> now rows will looks like this:
>>> ({'id':...,...},{'id':...,}...)
>>
>> Thanks Ervin, but:
>>
>>cursor = cnx.cursor(pymysql.cursors.DictCursor)
>> NameError: name 'pymysql' is not defined
>>
>> I have been using the mysql.connector module, which seems to be the
>> "official" python interface.
> 
> That also supports the cursordict:
> 
> https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html
> 

Great, thanks Larry, that sorts it.

-- 
Tony van der Hoff| mailto:t...@vanderhoff.org
Buckinghamshire, England |
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Ervin Hegedüs
Hi Tony,

On Tue, Oct 02, 2018 at 05:07:38PM +0100, Tony van der Hoff wrote:
> On 02/10/18 16:47, Ervin Hegedüs wrote:
> > hi,
> > 
> > now rows will looks like this:
> > ({'id':...,...},{'id':...,}...)
> 
> Thanks Ervin, but:
> 
>cursor = cnx.cursor(pymysql.cursors.DictCursor)
> NameError: name 'pymysql' is not defined
> 
> I have been using the mysql.connector module, which seems to be the
> "official" python interface. I hadn't spotted the pymysql module. Is the
> consensus here that pymysql is the preferred connector?

well, since I'm using Python3, I didn't use "old" MySQLdb python
module, I switched to pymysql - but as I know they are
compatible, so you can use:

cnx.cursor(MySQLdb.cursors.DictCursor)

I don't know about "mysql.connector" module yet - but it doesn't
mean that it doesn't existst :)

hth,


a.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Larry Martell
On Tue, Oct 2, 2018 at 12:09 PM Tony van der Hoff  wrote:
>
> On 02/10/18 16:47, Ervin Hegedüs wrote:
> > hi,
> >
> > now rows will looks like this:
> > ({'id':...,...},{'id':...,}...)
>
> Thanks Ervin, but:
>
>cursor = cnx.cursor(pymysql.cursors.DictCursor)
> NameError: name 'pymysql' is not defined
>
> I have been using the mysql.connector module, which seems to be the
> "official" python interface.

That also supports the cursordict:

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursordict.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Tony van der Hoff
On 02/10/18 16:47, Ervin Hegedüs wrote:
> hi,
> 
> now rows will looks like this:
> ({'id':...,...},{'id':...,}...)

Thanks Ervin, but:

   cursor = cnx.cursor(pymysql.cursors.DictCursor)
NameError: name 'pymysql' is not defined

I have been using the mysql.connector module, which seems to be the
"official" python interface. I hadn't spotted the pymysql module. Is the
consensus here that pymysql is the preferred connector?

Cheers,

-- 
Tony van der Hoff| mailto:t...@vanderhoff.org
Buckinghamshire, England |
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Tony van der Hoff
On 02/10/18 16:37, Larry Martell wrote:
> On Tue, Oct 2, 2018 at 11:34 AM Tony van der Hoff  
> wrote:
>>I would have expected the connector to be able to return a
>> dictionary.
>>
>> Can anyone suggest a better way of doing this?
> 
> https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.DictCursor
> 
Well, thanks, Larry, for taking the trouble to reply, but I can't see
how that solves the issue.

-- 
Tony van der Hoff| mailto:t...@vanderhoff.org
Buckinghamshire, England |
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Ervin Hegedüs
hi,

On Tue, Oct 02, 2018 at 04:14:45PM +0100, Tony van der Hoff wrote:
> I'm writing a database application, in python 3,5 under Debian9.
> 
> My code:
> 
>     def get_albums(self, parent_id = 0 ):
>     cursor = self.cnx.cursor()

  cursor = self.cnx.cursor(pymysql.cursors.DictCursor)

>     sql =(  "select"
>     "    id"
>     ",   parent_id"
>     ",   title"
>     ",   ifnull( description, '' )"
>     ",   path"
>     ",   date( from_unixtime( date_created ) ) as date"
>     " from album"
>     " where parent_id = %(parent_id)s"
>     " order by date_created"
>  )

  sql = ("""SELECT
  id,
  parent_id,
  ...
 """)

>     cursor.execute( sql, {'parent_id': parent_id } )   
>     rows = cursor.fetchall()

now rows will looks like this:
({'id':...,...},{'id':...,}...)


a.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Querying MariaDB from python

2018-10-02 Thread Larry Martell
On Tue, Oct 2, 2018 at 11:34 AM Tony van der Hoff  wrote:
>
> I'm writing a database application, in python 3,5 under Debian9.
>
> My code:
>
> def get_albums(self, parent_id = 0 ):
> cursor = self.cnx.cursor()
> sql =(  "select"
> "id"
> ",   parent_id"
> ",   title"
> ",   ifnull( description, '' )"
> ",   path"
> ",   date( from_unixtime( date_created ) ) as date"
> " from album"
> " where parent_id = %(parent_id)s"
> " order by date_created"
>  )
> cursor.execute( sql, {'parent_id': parent_id } )
> rows = cursor.fetchall()
>
> # return result as a list of dicts
> result = []
>
> for row in rows:
> result.append({ 'id':row[0],
> 'parent_id':row[1],
> 'title':row[2],
> 'description':row[3],
> 'path':row[4],
> 'date':row[5],
> }
> )
> return result
>
> This works OK, but looks inelegant. Having to iterate through the
> returned data to get it into a dictionary is error-prone if the query
> changes. I would have expected the connector to be able to return a
> dictionary.
>
> Can anyone suggest a better way of doing this?

https://pymysql.readthedocs.io/en/latest/modules/cursors.html#pymysql.cursors.DictCursor
-- 
https://mail.python.org/mailman/listinfo/python-list