python调用sqlite
参考资料:https://www.liaoxuefeng.com/wiki/1016959663602400/1017801751919456 https://www.cnblogs.com/liaocheng/p/5198574.html
SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其他数据库一样,您不需要在系统中配置。
就像其他数据库,SQLite 引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite 直接访问其存储文件,它有以下优势
不需要一个单独的服务器进程或操作的系统(无服务器的)。
SQLite 不需要配置,这意味着不需要安装或管理。
一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。
SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。
SQLite 是自给自足的,这意味着不需要任何外部的依赖。
SQLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问。
SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。
SQLite 使用 ANSI-C 编写的,并提供了简单和易于使用的 API。
SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中运行。
python自带sqlite3这个库,无需导入,只要在前面import一下就好了
然后就是对数据库的操作,首先就是创建数据库
cx= sqlite3.connect("E:/test.db") 这行代码表示打开目标路径里的test.db文件,如果没有该文件,就会自动创建一个。当然也可以创建在内存中,cx = sqlite3.connect(":memory:")
sqlite有四种操作,commit(),rollback(),close()和cursor()。
commit为事务提交,每次执行sql语句后需要提交,然后操作结束时需要close,cursor是游标
用cu = cx.cursor()创建一个游标,游标对应操作如下
#execute()--执行sql语句
#executemany--执行多条sql语句
#close()--关闭游标
#fetchone()--从结果中取一条记录,并将游标指向下一条记录
#fetchmany()--从结果中取多条记录
#fetchall()--从结果中取出所有记录
#scroll()--游标滚动
用这些即可完成对sqlite的基本操作
给自己列了一个模板,以便日后使用
import sqlite3
import tkinter
import tkinter.messagebox
import pymysql
import os #本文教程
#https://www.cnblogs.com/liaocheng/p/5198574.html #commit() 事务提交
#roolback() 事务回滚
#close() 关闭数据库连接
#cursor() 创建一个游标 #若数据库不存在,则自动创建一个数据库
cx = sqlite3.connect("text"); #游标操作
#-------*------
#execute()--执行sql语句
#executemany--执行多条sql语句
#close()--关闭游标
#fetchone()--从结果中取一条记录,并将游标指向下一条记录
#fetchmany()--从结果中取多条记录
#fetchall()--从结果中取出所有记录
#scroll()--游标滚动 #创建游标
cu = cx.cursor() #建表 test 含id name nickname
try:
cu.execute("create table test (id integer,name varchar(10),nickname blob )")
except:
print("表已存在") lb = "libo"
for t in [(2,lb,""),(3,'','')]:
cx.execute("insert into test values (?,?,?)",t)
cx.commit() sql1 = "select name from test"
cu.execute(sql1)
x = cu.fetchall()
print(x) def tk():
window = tkinter.Tk()
window.geometry("400x300")
tkinter.Label(window,text='用户名:').place(x=100,y=80)
username = tkinter.StringVar()
entryname = tkinter.Entry(window,textvariable = username)
entryname.place(x=150,y=80)
username.set(x) tk()
emmmm,自己写了一个记密码的程序用来测验对sqlite的操作,有兴趣可以看下
需要用到tkinter库,需要另外pip inastall一下
import sqlite3
import tkinter
import tkinter.messagebox
import pymysql
import os
window = tkinter.Tk() def userpass():
window.geometry("400x300")
canvas = tkinter.Canvas(window,height=300,width=500)
imagefile = tkinter.PhotoImage(file = "pokemon.png")
image = canvas.create_image(0,0,anchor='nw',image=imagefile)
canvas.pack(side='left')
tkinter.Label(window,text='用户名:').place(x=100,y=80)
tkinter.Label(window,text='密码:').place(x=100,y=140)
username = tkinter.StringVar()
entryname = tkinter.Entry(window,textvariable = username)
entryname.place(x=150,y=80)
password = tkinter.StringVar()
entrypass = tkinter.Entry(window,textvariable = password,show = '*')
entrypass.place(x=150,y=140)
def register():
#输入框获取用户名密码
usernameg = username.get()
passwordg = password.get()
print(usernameg,passwordg) #创建数据库
cx = sqlite3.connect("password.db");
#创建游标
cu = cx.cursor()
#创建数据库
try:
cu.execute("create table uspass (name varchar(10),password text )")
except:
print("表已存在,数据也已存入") t = (usernameg,passwordg)
cx.execute("insert into uspass values (?,?)",t)
cx.commit()
def login():
usernameg = username.get()
passwordg = password.get()
cx = sqlite3.connect("password.db");
#创建游标
cu = cx.cursor()
#创建数据库
sql1 = "select * from uspass"
cu.execute(sql1)
x = cu.fetchall()
if x[0][0] == usernameg and x[0][1] == passwordg:
window.destroy()
manage()
else:
tkinter.messagebox.showerror(message='账号错误')
#print("这就登录了") student = tkinter.Button(window,text ="注册&新增", command = register)
student.place(x=110,y=200)
student = tkinter.Button(window,text ="登录", command = login)
student.place(x=230,y=200)
window.mainloop() def manage():
man_win = tkinter.Tk()
man_win.geometry("400x300")
name = tkinter.StringVar()
entryname = tkinter.Entry(man_win,textvariable = name)
entryname.place(x=150,y=40) tkinter.Label(man_win,text='用户名:').place(x=100,y=80)
tkinter.Label(man_win,text='密码:').place(x=100,y=140)
username = tkinter.StringVar()
entryname = tkinter.Entry(man_win,textvariable = username)
entryname.place(x=150,y=80)
password = tkinter.StringVar()
entrypass = tkinter.Entry(man_win,textvariable = password)
entrypass.place(x=150,y=140) cx = sqlite3.connect("password.db");
cu = cx.cursor()
sql = "select name from uspass"
cu.execute(sql)
x = cu.fetchall()
name.set(x) def cha():
user = username.get()
cx = sqlite3.connect("password.db");
cu = cx.cursor()
sql = "select password from uspass where name = '" + user+"'"
#print(sql)
cu.execute(sql)
x = cu.fetchall()
password.set(x) def zeng():
user = username.get()
passw = password.get()
cx = sqlite3.connect("password.db");
cu = cx.cursor()
t = (user,passw)
cx.execute("insert into uspass values (?,?)", t)
cx.commit() def gai():
user = username.get()
passw = password.get()
cx = sqlite3.connect("password.db");
cu = cx.cursor()
sql = "update uspass set password='"+passw+"'"+ "where name ='" +user+"'"
#print(sql)
cu.execute(sql)
cx.commit() cha = tkinter.Button(man_win,text ="查询", command = cha)
zeng = tkinter.Button(man_win,text = "添加",command = zeng )
gai = tkinter.Button(man_win,text = "修改",command = gai )
cha.place(x=110,y=200)
zeng.place(x=180,y=200)
gai.place(x=250,y=200)
window.mainloop() userpass() os.system("pause")
python调用sqlite的更多相关文章
- Python读取SQLite文件数据
近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...
- Python操作SQLite数据库的方法详解
Python操作SQLite数据库的方法详解 本文实例讲述了Python操作SQLite数据库的方法.分享给大家供大家参考,具体如下: SQLite简单介绍 SQLite数据库是一款非常小巧的嵌入式开 ...
- 大华摄像头报警接口中图片加密,python调用c++方式实现解密
项目中,大华摄像头的报警信息,里面有图片地址,需要1天内取下来,保持留痕 可惜,图片下载后,加密了,大华提供了android,ios,c++例子,没有提供java解密例子 没办法,只好先用c++例子简 ...
- 【初学python】使用python调用monkey测试
目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
- python调用其他程序或脚本方法(转)
python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...
- python调用c\c++
前言 python 这门语言,凭借着其极高的易学易用易读性和丰富的扩展带来的学习友好性和项目友好性,近年来迅速成为了越来越多的人们的首选.然而一旦拿python与传统的编程语言(C/C++)如来比较的 ...
- Python调用C++
/***gcc -o libpycall.so -shared -fPIC pycall.c*/ #include <stdio.h> #include <stdlib.h> ...
- 使用Python调用Flickr API抓取图片数据
Flickr是雅虎旗下的图片分享网站,上面有全世界网友分享的大量精彩图片,被认为是专业的图片网站.其API也很友好,可以实现多种功能.这里我使用了Python调用其API获得了大量的照片数据.需要注意 ...
随机推荐
- 牛客网 —— 爱奇艺2020校招C++方向笔试题 总结
错了5,6个,主要集中在数据库和操作系统上. C++还需要提高熟练度. 总结 第6题: 折半查找树:根据二分查找构造得到的树,它一定是一个二叉排序树,是一个特殊的二叉排序树. (接近于平衡二叉 ...
- 日常问题--解决 ‘Could not fetch URL https://pypi.python.org’的问题
难题描述: 解决方法: 使用命令python -m pip install Scrapy --trusted-host=pypi.python.org --trusted-host=pypi.org ...
- .NetCore学习笔记:一、UnitOfWork工作单元
Maintains a list of objects affected by a business transaction and coordinates the writing out of ch ...
- Latex中遇到 No room for a new \count 问题的解决
在tex文件中加入etex宏包. \usepackage{etex} 最好加载第一个宏包位置 PDF合并 \documentclass[a4paper]{article}\usepackage{pdf ...
- 查看Sql Server库中某张表的结构
--快速查看表结构(比较全面的) SELECT CASE WHEN col.colorder = THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 ...
- 多项式乘法逆元 - NTT
递归求解即可 #include <bits/stdc++.h> using namespace std; #define int long long namespace NTT { #de ...
- 1级迁移类Q101-Oracle ASM 迁移文件系统(File System)非公
项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...
- 动态生成sku组合输入列表
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...
- ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline[上篇]
链接:https://www.cnblogs.com/artech/archive/2007/09/13/891262.html 相信大家都使用过ASP.NET进行过基于Web的应用开发,ASP.NE ...
- 09 部署nginx web服务器(转发uwsgi请求)
1 配置nginx转发 $ whereis nginx $ cd /usr/local/nginx/conf $ vi nginx.conf 注释掉原来的html请求,增加uwsgi请求. locat ...