python基础学习-python操作sqlite数据库
2017-08-17 21:06
411 查看
'''
python使用sqlite数据库
'''
import sqlite3
con = sqlite3.connect("./test.db")#在磁盘上创建数据库
# con = sqlite3.connect(":memory:")#在内存中创建数据库
cu=con.cursor()#创建一个游标
returnCu = cu.execute("select count(*) from sqlite_master where type='table' and name='user'")
returnTuple = returnCu.fetchone()
#如果表存在则删除表
if returnTuple[0]==1:
cu.execute("drop table user")
#创建表
cu.execute("create table user(id integer primary key,username varchar(20),password varchar(20))")
#插入数据
cu.execute("insert into user values(?,?,?)",(1,"xiaobing","123"))
cu.execute("insert into user values(?,?,?)",(2,"xiaoming","123"))
con.commit()#提交
#查询表数据
returnCu = cu.execute("select * from user")
for (userid,username,password) in returnCu:
print userid,username,password
#修改数据
cu.execute("update user set password=? where id=?",("123456",1))
con.commit()
returnCu = cu.execute("select * from user where id=?",(1,))
getUserCu = returnCu.fetchone()
print getUserCu[2]
#删除数据
cu.execute("delete from user where id=?",(2,))
con.commit()
returnCu = cu.execute("select * from user")
for (userid,username,password) in returnCu:
print userid,username,password
con.close()
python使用sqlite数据库
'''
import sqlite3
con = sqlite3.connect("./test.db")#在磁盘上创建数据库
# con = sqlite3.connect(":memory:")#在内存中创建数据库
cu=con.cursor()#创建一个游标
returnCu = cu.execute("select count(*) from sqlite_master where type='table' and name='user'")
returnTuple = returnCu.fetchone()
#如果表存在则删除表
if returnTuple[0]==1:
cu.execute("drop table user")
#创建表
cu.execute("create table user(id integer primary key,username varchar(20),password varchar(20))")
#插入数据
cu.execute("insert into user values(?,?,?)",(1,"xiaobing","123"))
cu.execute("insert into user values(?,?,?)",(2,"xiaoming","123"))
con.commit()#提交
#查询表数据
returnCu = cu.execute("select * from user")
for (userid,username,password) in returnCu:
print userid,username,password
#修改数据
cu.execute("update user set password=? where id=?",("123456",1))
con.commit()
returnCu = cu.execute("select * from user where id=?",(1,))
getUserCu = returnCu.fetchone()
print getUserCu[2]
#删除数据
cu.execute("delete from user where id=?",(2,))
con.commit()
returnCu = cu.execute("select * from user")
for (userid,username,password) in returnCu:
print userid,username,password
con.close()
相关文章推荐
- python--基础学习(六)sqlite数据库基本操作
- python学习笔记之基础操作
- python基础学习之序列操作
- 文件操作注意事项 分类: python 小练习 python基础学习 open()文件读写 2013-12-03 11:05 532人阅读 评论(0) 收藏
- Python基础学习(一)之切片操作
- python基础学习之文件操作
- python学习笔记.字典基础操作
- Python基础学习----文件操作
- Python基础语法学习--元祖和字典以及相关操作和方法
- Python学习之路-基础知识2-文件操作
- Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级
- 我的Appium学习记录——Python IOS appium& selenium 自动化基础操作
- python基础学习(7)—目录常见操作
- python连接sqlite(mysql)数据库以及sqlite数据库基础操作
- Python基础学习-列表基本操作
- python学习之路-1 python基础操作
- Python学习基础笔记——modules(模块操作)
- Python学习-1天--基础操作
- python基础学习总结——文件操作
- Python基础语法学习----列表的访问,操作以及列表函数