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

一.直接上代码

#!/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

相关文章:

  • 2021-05-28
  • 2021-12-10
  • 2021-09-19
  • 2022-12-23
  • 2021-07-22
  • 2021-08-06
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2021-11-27
  • 2021-08-06
  • 2021-07-22
  • 2021-08-07
  • 2022-12-23
  • 2021-12-14
相关资源
相似解决方案