SQLite 是一款轻型的关系型数据库,它的处理速度比MySQL快。
SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个重要部分。所以主要的通信协议是在编程语言内直接调用API
先是python调用SQLite3的实例:
1、建表
-
#!/bin/env python
-
#coding:utf-8
-
import sqlite3
-
conn = sqlite3.connect('test.db')
-
cur = conn.cursor()
-
sql = 'CREATE TABLE test(id int(11) NOT NULL,name varchar(22) NOT NULL)'
-
cur.execute(sql)
-
conn.commit()
-
conn.close()
2、插入数据
-
#!/bin/env python
-
#coding:utf-8
-
import sqlite3
-
conn = sqlite3.connect('test.db')
-
cur = conn.cursor()
-
#sql = 'CREATE TABLE test(id int(11) NOT NULL,name varchar(22) NOT NULL)'
-
sql1 = "INSERT INTO test(id,name) VALUES (1,'xiaoA')"
-
cur.execute(sql1)
-
conn.commit()
-
conn.close()
3、查询数据
-
#!/bin/env python
-
#coding:utf-8
-
import sqlite3
-
conn = sqlite3.connect('test.db')
-
cur = conn.cursor()
-
sql = 'CREATE TABLE test(id int(11) NOT NULL,name varchar(22) NOT NULL)'
-
sql1 = "INSERT INTO test(id,name) VALUES (1,'xiaoA')"
-
sql2 = "SELECT * FROM test"
-
cur.execute(sql2)
-
for i in cur.fetchall():
-
print i,
-
conn.commit()
-
conn.close()
4、删除数据
-
#!/bin/env python
-
#coding:utf-8
-
import sqlite3
-
conn = sqlite3.connect('test.db')
-
cur = conn.cursor()
-
sql = 'CREATE TABLE test(id int(11) NOT NULL,name varchar(22) NOT NULL)'
-
sql1 = "INSERT INTO test(id,name) VALUES (1,'xiaoA')"
-
sql2 = "SELECT * FROM test"
-
sql3 = "DELETE FROM test"
-
cur.execute(sql3)
-
for i in cur.fetchall():
-
print i,
-
conn.commit()
-
conn.close()
阅读(2324) | 评论(0) | 转发(0) |