SQlite3基本用法,使用sublime编辑器
2017-12-20 20:55
211 查看
# 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()
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()
相关文章推荐
- sublime编辑器【快捷键】基本用法,sublime小技巧
- displaytag使用指南之基本用法(翻译)
- Vi编辑器的基本使用方法
- lVi编辑器的基本使用方法!
- 【VC++ 中使用ADO操作数据库学习笔记】_ConnectionPtr指针的基本用法
- VI编辑器的基本使用方法
- Vi编辑器的基本使用方法
- Vi编辑器的基本使用方法!
- Vi编辑器的基本使用方法
- Vi编辑器的基本使用方法
- CxImage的使用及基本用法
- Android ListView 最基本的用法,使用SimpleCursorAdapter 。 附例子。
- vim基本编辑器的使用
- Vi编辑器的基本使用方法- -
- Vi 编辑器的基本使用方法
- 开始使用 Zend_View 占位符:占位符基本用法
- 【Android】在Android上使用OrmLite数据库框架 之 基本用法
- FCK编辑器在JSP中使用基本说明(来自http://blog.360quan.com/u/1281c9/show/506089)
- VI编辑器的基本使用
- Vi编辑器的基本使用方法