bwangel23
10/23/2016 - 6:56 AM

Python sqlite3 示例代码

Python sqlite3 示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from sqlite3 import dbapi2 as sqlite3

conn = sqlite3.connect('flaskr.db')
c = conn.cursor()
c.execute('select * from entries;')

for idx, col in enumerate(c.description):
    """ cursor.description

    这是一个只读字段,用来返回上一次查询结果的列的名字和索引值

    为了和 Python DB API保持兼容,每个列的名字都是一个七元组,而且最后六个元素都是None
    """
    print(idx, col)

conn.close()

""" 执行结果
➜ /tmp/sqlite $ python3 test.py
0 ('id', None, None, None, None, None, None)
1 ('title', None, None, None, None, None, None)
2 ('text', None, None, None, None, None, None)
"""