3.查询SQLite表中的数据
# coding=utf-8 import sqlite3 dbPath = 'data.sqlite' conn = sqlite3.connect(dbPath) c = conn.cursor() persons = c.execute('select name,age,address,salary from persons order by age') # 打印查询结果发现是个Cursor对象(可迭代对象) print(type(persons)) result = [] for person in persons: value = {} value['name'] = person[0] value['age'] = person[1] value['address'] = person[2] result.append(value) conn.close() print(type(result)) print(result) # 我们也可以使用前面学习的json模块使这个list类型的result转为字符串类型 # 网络传输需要使用字符串类型 import json resultStr = json.dumps(result, ensure_ascii=False) print(resultStr)
总结
使用sqlite3模块中的API可以操作SQLite数据库,该模块是Python内置的模块,不需要单独安装。
到此这篇关于Python练习之操作SQLite数据库的文章就介绍到这了,更多相关Python操作SQLite 内容请搜索
标签:SQLite
相关阅读 >>
php登录验证功能示例【用户名、密码、验证码、数据库、已登陆验证、自动登录和注销登录等】
mssql和Sqlite中关于if not exists 的写法
更多相关阅读请进入《Sqlite》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。