# 1. create database and table'''
sqlite3
内置模块,不需要安装
'''import sqlite3
import os
dbPath ='data.sqlite'ifnot os.path.exists(dbPath):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
conn.commit()
conn.close()print('创建数据库成功')
# 3. select
persons = c.execute('select name, age, address, salary from persons order by age')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)import json
resultStr = json.dumps(result)print(resultStr)