当前位置:首页 > 数据库 > SQlite

Python处理Sqlite3数据库

sqlite3比较小众 本章主要通过Python Code表述如何增、查、改、删 sqlite3 DB

一.直接上代码

Python处理Sqlite3数据库 - 文章图片Python处理Sqlite3数据库 - 文章图片
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-09-29 12:04:51
# @Author  : call_me
# @Version : V1.0

import sqlite3
connect = sqlite3.connect('student.db')
cursor = connect.cursor()
try:
    sql = '''create table student(
        [id]        integer PRIMARY KEY,
        [name]        varchar(50),
        [sex]        varchar(2),
        [age]        int
        );
        '''
    cursor.execute(sql)
except Exception as e:
    print("表已创建")

# 插入数据
sql = '''insert into student (id,name,sex,age) VALUES (?,?,?,?)'''
data = (1,"张1","男","9")
cursor.execute(sql,data)
data = (2,"张2","女","10")
cursor.execute(sql,data)
data = (3,"张3","男","11")
cursor.execute(sql,data)
data = (4,"张4","女","12")
cursor.execute(sql,data)

# 查数据
for select in cursor.execute("select * from student"):
    print("名称:%s ,t 年龄:%s " %(select[1],select[3]))

# 删除数据
cursor.execute("delete from student")

# 删除表
cursor.execute("drop table student")

connect.commit()
connect.close()
Code

 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!