python3 与 sqlite3 开发(1)
首先写一个简单的 python 使用 sqlite3 的程序,理解一下 sqlite3的 结构
包括 密码 ,用户名,windows 下面的查看方式.
打开和创建,
shelve模块pickle
http://zetcode.com/python/sqlite/
http://zetcode.com/python/sqlite/
Python 《SQLite教程》
https://blog.csdn.net/pansaky/article/details/99674449
http://www.sqlcourse.com
Clare Churcher Beginning SQL Queries
http://www.python.org/topics/database/
http://www.sqlalchemy.org
http://www.sqlobject.org
http://wiki.python.org/moin/DatabaseProgramming
它是我们磁盘上的一个文件。如果存在,则打开。如果没有,则创建它。
https://www.php.cn/python-tutorials-376881.html
3、查询某一条或多条记录
4、避免重复注入
有时候我们会有重复的记录,为了避免把相同的记录多次插入到数据库,可以使用如下语句:
sql="INSERT OR REPLACE INTO "+my_table+" values (?,?,?,?,?,?,?) "
#假设my_table有7项
5、插入中文记录至SQLite3
Python是unicode编码,但数据库对中文是使用GBK编码,比如stock_name变量含有中文,则需要做unicode(name, “gbk”)处理
batch=[(stock_num, unicode(stock_name, "gbk") )]
cs.executemany( "INSERT OR REPLACE INTO my_table values (?,?) ",batch)
以上就是Python中SQLite3的基本操作实例讲解的详细内容,更多请关注php中文网其它相关文章!
SQLite:
SQLite是一种数据库,Python中集成了SQLite3,所以在Python中使用SQLite,可以直接导入SQLite包,不需要做额外的配置。
更多的SQLite简介和相关知识可以查看专门的教程:http://www.runoob.com/sqlite/sqlite-tutorial.html
#首先,导入包
import sqlite3
#然后连接到数据库TEST
#如果数据库存在,直接连接;否则会创建相应的数据库
connect = sqlite3.connect('TEST.db')
#创建一个游标,用于数据库操作:
cursor = conn.cursor()
#创建表
cursor.execute('create table T_BASE (ID INT PRIMARY KEY, NAME VARCHAR(20) NOT NULL)')
#插入数据
cursor.execute('insert into T_BASE (ID, NAME) values (1,'JACK')')
#查询数据
cursor.execute('SELECT * FROM T_BASE WHERE ID=1')
#更新数据
cursor.execute('UPDATE T_BASE SET NAME= 'MARY' WHERE ID = 1')
#删除数据
cursor.execute('DELETE FROM T_BASE WHERE ID = 1')
#结束
#关闭游标
cursor.close()
#提交事务
connect.commit()
#关闭数据库连接
connect.close()
http://zetcode.com/db/sqlitepythontutorial/
https://docs.python.org/3/library/sqlite3.html
http://www.blog.pythonlibrary.org/2012/07/01/a-simple-sqlalchemy-0-7-0-8-tutorial/
https://www.tutorialspoint.com/python_sqlite/python_sqlite_useful_resources.htm
Learn SQLite with Python: Building Database-Driven Desktop Projects
how to create a database in python using sql lite 3
Learn SQLite with Python: Building Database-Driven Desktop Projects
SQL AND Python Programming: 2 Books IN 1!
Python, ventanas y base de datos sqlite: programa una aplicacion con ventanas desde Python (Spanish Edition)
Python and Sql Programming: 2 Books in 1: “ Python Coding and Sql Coding for beginners “
参考制作:
PyQt5搭建图书馆管理系统(1)——数据库
https://blog.csdn.net/weixin_38312031/article/details/80015027