Python处理Sqlite3数据库
2024-08-26 05:11:39
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","男","")
cursor.execute(sql,data)
data = (2,"张2","女","")
cursor.execute(sql,data)
data = (3,"张3","男","")
cursor.execute(sql,data)
data = (4,"张4","女","")
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
最新文章
- 原生JS编写的照片墙效果实例演示特效
- 【BZOJ-2879】美食节 最小费用最大流 + 动态建图
- pure css兼容IE
- python 装饰器(decorator)
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- sql 将某一列的值拼接成字符串
- LeetCode解题报告:Binary Tree Postorder Traversal
- Performance of Transport Control Protocol over Dynamic Spectrum Access Links
- 2014百度之星第四题Labyrinth(DP)
- shell查找指定时间段内的文件
- tcp/ip 卷一 读书笔记(3)为什么既要有IP地址又要有MAC地址
- windows pe
- Exp2 后门原理与实践
- Java EE 导图
- xls文件导入数据库
- Linq 操作DataTable
- MyBatis-Configuration
- 微信获取地理位置转城市demo
- BZOJ3537 : [Usaco2014 Open]Code Breaking
- CentOS静默安装Oracle 11gR2(x64)