淘先锋技术网

首页 1 2 3 4 5 6 7

Sqlite是Python内置的一个轻量级数据库

使用方法:
import sqlite;
conn = sqlite.conn(“数据库名”); # 得到一个数据库链接(数据库有则连接,无则创建)
cursor = conn.cursor(); # 获得一个操作数据库的游标
# 创建表
c_t_sql = “create table if not exist 表名(字段 类型 其他, 字段 类型…)”;
cursor.execute(c_t_sql);

#  插入数据 
insert_sql = "insert into 表名(要插入的字段名...)values(要插入的值...)";
cursor.execute(insert_sql);

# 删除数据
de_sql = "delete from 表名 where 条件"; 
cursor.execute(de_sql);

# 更改数据
up_sql= "update 表名 set 字段=新值 where 约束条件";
cursor.execute(up_sql);

# 查询数据
view_sql = "select 字段名/* from 表名 where 约束条件";
cursor.execute(view_sql);

# 最后提交所有操作
conn.commit();

# 关闭游标和数据库连接
cursor.close();
conn.close();