SQlite3基本用法,使用sublime编辑器

来源:互联网 发布:软件运营部门 编辑:程序博客网 时间:2024/06/03 13:53
# coding:utf-8
import sqlite3
# 1.连接数据库,文件不存在自动创建新的数据库文件
connect=sqlite3.connect('database.db')
# 2.连接游标
cursor=connect.cursor()
# 3.向数据库database.db添加一张表,以及四个字段id,name,age,score
# create_student_table="CREATE TABLE Student(id INTEGER PRIMARY KEY,name TEXT,age INTEGER,score FLOAT)"
# cursor.execute(create_student_table)
# 4.往表中插入数据
# insert_sql="INSERT INTO Student(name,age,score)VALUES('李四','30',50)"
# cursor.execute(insert_sql)
# 5.更改表中的数据
update_sql="UPDATE Student SET name='%s',age='%d' WHERE id=1 "%('王五',100)
cursor.execute(update_sql)
# 6.查询表中所有的数据
select_sql="SELECT*FROM Student"
res=cursor.execute(select_sql)


for x in res:
    print x
# 7.删除表中的数据
delete_sql="DELETE FROM Student WHERE id=2"
cursor.execute(delete_sql)
# 执行提交操作
connect.commit()
cursor.close()
connect.close()

原创粉丝点击