数据库-SQLite

2019-05-09  本文已影响0人  haokeed

sqlite数据库是python自带的数据库

import sqlite3

sql='''
create table test(
    stuid varchar(10),
    stuname varuchar(20),
    gender varchar(2),
    age integer
)
'''

# 使用特定的文件名 memory 在内存中创建一个数据库
con=sqlite3.connect(":memory:")

#创建表
con.execute(sql)
con.commit()

#查询表
cursor=con.execute('select * from test')
cursor.fetchall()

# insert data
data=[('20161001','Zhangsan','男',19),
     ('20161002','Lisi','女',22),
     ('20161003','Wangwu','男',21)]
stmt="insert into test values(?,?,?,?)"
con.executemany(stmt,data) # 封装数据
con.commit() # 提交

# 查询数据
cursor=con.execute("select * from test")
cursor.fetchall()

# 快速查询
import pandas.io.sql as sql

sql.read_sql_query('select * from test',con)
image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读