python操作sqlite3

连接sqlite3数据库

import sqlite3

path = '/static/sqlite3/demo.sqlite3'
// 如果路径存在这个sqlite3文件则连接,如果不存在就创建并连接
conn = sqlite3.connect(path)

创建表

conn.execute('''
      CREATE TABLE IF NOT EXISTS People
         (ID INTEGER  PRIMARY KEY NOT NULL,
          Name TEXT NOT NULL,
          Age INT,
          Sex TEXT   
         );
         ''')

增删改查

add = "insert into People(Name, Age, Sex) values ('陈', 22, '男') "
conn.execute(add)

select = "select * from People"
conn.execute(select)

update = "update People set Name = 'chen' "
conn.execute(update)

deleteData = "delete from People where Name = 'chen' "
conn.execute(deleteData)

dropTable = "drop table People"
conn.execute(dropTable)

所以总的来说需要执行sql语句的时候使用 execute方法执行。

最后保存关闭数据库

conn.commit()
conn.close()

你可能感兴趣的:(python操作sqlite3)