DBS:数据库系统
结构化语言:SQL
数据库管理系统:DBMS
数据库管理员:DBA
SQLite 动态类型
(1)NULL
(2)INTEGER
(3)REAL (decimal, float等,亲源类型)
(4)TEXT
(5)BLOB
SELECT * FROM LinkMan //从LinkMan表中选出所有行所有列字段
INSERT INTO LinkMan (Name,Mobile,BirthDate,IsValid) values ('Tom','15800000000','1990-09-01',1);
UPDATE LinkMan SET BirthDate = '1988-09-01' WHERE rowid = 1
//或
UPDATE LinkMan SET BirthDate = '1988-09-01' WHERE Name = 'Tom'
DELETE FROM LinkMan WHERE OID = 5
SQLite 操作
内置模块 sqlite3
(1)connect('数据文件.db')连接对象
【1】commit() //提交操作
【2】close()
(2)cursor游标 cursor = conn.cursor()
【1】执行语句:execute('SQL语句,[参数]')
import sqlite3
【2】fetchall() 获取所有结果到列表
【3】fetchone() 获取第一条结果到列表
【4】fetchmany(记录数) 获取制定条数结果到列表
(3)参数化查询(不要直接拼接字符串)
【1】目的:避免SQL注入
【2】?:参数传递tuple
【3】:参数名,参数传递字典表
conn = sqlite3.connert(''db\addressbook.db)
c = conn.cursor()
sql = "select * from LinkMan"
result = c.execute(sql)
for row in result:
print(row)
//添加操作
sql = "insert into LinkMan values ('Marry,'18933333333','1985-03-09',1)"
c.excute(sql)
conn.commit()
conn.close()
//修改操作
sql = "UPDATA LinkMan SET Mobile = '18900000000' WHERE Name = 'Peter'"
c.excute(sql)
conn.commit()
conn.close()
//删除操作
sql = "DELETE FROM LinkMan WHERE rowid = 5"
c.excute(sql)
conn.commit()
conn.close()
//fetchall()
sql = "SELECT OID,* FROM LinkMan"
c.execute(sql)
lst = c.fetchall()
for row in lst:
print(row)
//参数化查询(传单个参数)
name = ('Tom',)
sql = "select * from LinkMan where Name = ?"
c.execute(sql,name)
c.fetchall()
//参数化查询(传多个参数)
sql = "insert into LinkMan values (:name,:mobile,:birtthdate,:isvalid)"
c.excute(sql,{"name":John,"mobile":"18900000000","birthdate":"1989-09-03","isvalid":1})