当前位置:Gxlcms > 数据库问题 > python操作轻量级数据库

python操作轻量级数据库

时间:2021-07-01 10:21:17 帮助过:22人阅读

In [26]: for item in cu.fetchall():
   ....:     for element in item:
   ....:         print element,
   ....:     print
   ....: 
0 10 鱼 Yu
1 20 cba Xu 技术分享

 

7.Row类型

 

Row提供了基于索引和基于名字大小写敏感的方式来访问列而几乎没有内存开销。 原文如下:

 

sqlite3.Row provides both index-based and case-insensitive name-based access to columns with almost no memory overhead. It will probably be better than your own custom dictionary-based approach or even a db_row based solution.

 

Row对象的详细介绍

class sqlite3.Row

A Row instance serves as a highly optimized row_factory for Connection objects. It tries to mimic a tuple in most of its features.

It supports mapping access by column name and index, iteration, representation, equality testing and len().

If two Row objects have exactly the same columns and their members are equal, they compare equal.

Changed in version 2.6: Added iteration and equality (hashability).

keys()

This method returns a tuple of column names. Immediately after a query, it is the first member of each tuple in Cursor.description.

New in version 2.6.

    下面举例说明

技术分享 In [30]: cx.row_factory = sqlite3.Row

In [31]: c = cx.cursor()

In [32]: c.execute(‘select * from catalog‘)
Out[32]: <sqlite3.Cursor object at 0x05666680>

In [33]: r = c.fetchone()

In [34]: type(r)
Out[34]: <type ‘sqlite3.Row‘>

In [35]: r
Out[35]: <sqlite3.Row object at 0x05348980>

In [36]: print r
(0, 10, u‘\u9c7c‘, u‘Yu‘)

In [37]: len(r)
Out[37]: 4

In [39]: r[2]            #使用索引查询
Out[39]: u‘\u9c7c‘

In [41]: r.keys()
Out[41]: [‘id‘, ‘pid‘, ‘name‘, ‘nickname‘]

In [42]: for e in r:
   ....:     print e,
   ....: 
0 10 鱼 Yu 技术分享

 使用列的关键词查询

In [43]: r[‘id‘]
Out[43]: 0

In [44]: r[‘name‘]
Out[44]: u‘\u9c7c‘

python操作轻量级数据库

标签:secure   eve   provides   主键   游标   字符   href   游标对象   for   

人气教程排行