Python练习之操作SQLite数据库
作者:? 发布时间:2024-01-22 03:23:39
标签:Python,操作,SQLite,数据库
前言
文章包括下几点:
考点--操作SQLite数据库:
创建SQLite数据库;
向表中插入记录;
其他数据库操作。
面试题:
1.面试题一:如何创建SQLite数据库?
2.面试题二:如何向SQLite表中插入数据?
3.面试题三:如何查询SQLite表中的数据?
1.创建SQLite数据库
# coding=utf-8
# _author__ = 孤寒者
import sqlite3
import os
dbPath = 'data.sqlite'
if not os.path.exists(dbPath):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
conn.commit()
conn.close()
print('创建数据库成功')
我们通过上述操作已经成功创建了sql数据库,并在里面创建了一张表。
为了查看我们创建的表,我们可以用到SqliteStudio,它是一款 Sqlite数据库可视化工具,是使用Sqlite数据库开发应用的必备软件,软件无需安装,下载后解压即可使用,很小巧但很了用,绿色中文版本。比起其它SQLite管理工具,我喜欢用这个。很方便易用,不用安装的单个可执行文件,支持中文。
2.向SQLite表中插入数据
# coding=utf-8
import sqlite3
dbPath = 'data.sqlite'
conn = sqlite3.connect(dbPath)
c = conn.cursor()
# 首先将表中数据全部删除
c.execute('delete from persons')
# 插入数据
c.execute('''
insert into persons(id,name,age,address,salary)
values(1, '孤寒者', 18, 'China', 9999)
''')
c.execute('''
insert into persons(id,name,age,address,salary)
values(2, '小张', 55, 'China', 9)
''')
conn.commit()
print('insert success')
3.查询SQLite表中的数据
# coding=utf-8
import sqlite3
dbPath = 'data.sqlite'
conn = sqlite3.connect(dbPath)
c = conn.cursor()
persons = c.execute('select name,age,address,salary from persons order by age')
# 打印查询结果发现是个Cursor对象(可迭代对象)
print(type(persons))
result = []
for person in persons:
value = {}
value['name'] = person[0]
value['age'] = person[1]
value['address'] = person[2]
result.append(value)
conn.close()
print(type(result))
print(result)
# 我们也可以使用前面学习的json模块使这个list类型的result转为字符串类型
# 网络传输需要使用字符串类型
import json
resultStr = json.dumps(result, ensure_ascii=False)
print(resultStr)
来源:https://juejin.cn/post/7069638564350263304
0
投稿
猜你喜欢
- 前段时间我编写了一个工业控制的软件,在使用中一直存在一个问题,就是当软件检索设备时,因为这个功能执行的时间比较长,导致GUI界面假死,让用户
- var yData = [];//Y轴数据 var xData = [];//X轴数据 $(data.rows).each(function
- quiver绘制表示梯度变化非常有用,下面是学习过程中给出的两个例子,可以很好理解quiver的用法from pylab import *c
- 在mysql安装目录的bin下, 运行mysql --install MYSQL5查看服务中, 会多一个MYSQL5的服务XAMPP的控制面
- 算法比较暴力,直接用穷举的方式一个一个去试,所以程序运行时间会比较长,运行时间视数独而定。不过从一开始到运行成功,整个过程却是一波三折,设计
- 目录Python的引入变量的引入为什么要有变量定义变量常量引入常量池引入Python解释器Python变量存储机制Python垃圾回收机制引
- Mysql索引索引介绍索引是什么官方介绍索引是帮助MySQL高效获取数据的数据结构。更通俗的说,数据库索引好比是一本书前面的目录,能加快数据
- 禁止鼠标右键:$(document).ready(function(){ $(document).bind("contextmen
- 编程中有时候需要一个初始极大值(或极小值)作为temp,当然可以自定义设置为10000(whatever),不过python中有一个值可以代
- MySQL GUI工具很多,本文就常用的Navicat for MySQL与MySQL-Front的特色功能做一个详细介绍与比较。(一)My
- 一、原理核心思想比较简单。即通过不同旋转角度的模板同时匹配,在多个结果中,找到相似度最大的结果,即认为匹配成功。 在视频的某一帧将这些模板分
- 1.1全部php生成结构1.2html中嵌套php总结如下:html和php混写规则:php代码必须包在<?php ?>html
- 最近做项目碰到的问题.拿出来跟大家分享一下吧. 用servlet赋值给html页面文本框值后,用alert来弹出这个值.结果显示"
- 1.MySQL的安装(1)双击我们去官网下载好的MySQL(2) 一直点下一步,直到遇到以下界面(3)选择Typical,会进行跳转,点击I
- $tar xvf go1.3.3.linux-amd64.tar.gz$mv go /usr/local/$vim /etc/profile
- 一、JSON数据格式介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。JSON建
- 前言本方法基于web2py框架,使用web2py的完整网站数据包创建简单网站。web2py 是一个为Python语言提供的全功能Web应用框
- Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼。装饰器引入初期及问题诞生假如
- 实现效果实现代码from skimage import img_as_floatimport matplotlib.pyplot as pl
- 双击编辑功能如何实现:例如:标题 (鼠标双击“标题”文字 即出现可编辑的输入框形式及提交按钮) <!D