派森下sqlite3的基本操作

本文介绍了一个使用Python的sqlite3模块进行数据库操作的例子,包括创建数据库、检查表存在性、创建表、插入数据及查询数据的过程。

import sqlite3
import datetime
DBname='PrintSrv.DB'
conn=sqlite3.connect(DBname)
conn.execute("PRAGMA synchronous = OFF;")
theCur=conn.cursor()
findTableSql="select * from sqlite_master where type='table' and name='PrintList'"
createTableSql='create table PrintList(iID integer PRIMARY KEY autoincrement,sContent varchar(3000),iStatus integer)'
theCur.execute(findTableSql)
if len(theCur.fetchall())==0:
    theCur.execute(createTableSql)
sIDvalue=datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
insertSql="insert into PrintList(sContent,iStatus) values('{0}',{1})".format(sIDvalue,'0')
theCur.execute(insertSql)
conn.commit()
selSql='select * from PrintList'
theCur.execute(selSql)
print((theCur.fetchall()))

theCur.close()
conn.close()
 

### 西南科技大学(SWUST)教材P113 集合操作 Python Set Operations Python 中的 `set` 是一种无序且不重复的数据结构,适用于存储唯一的元素集合。以下是基于问题描述以及引用内容所涉及的内容展开讨论。 #### 字符串转列表并统计字符频率 给定字符串可以通过内置函数轻松转化为列表形式,并进一步对其进行字符频次统计: ```python txt = '''If there is only one argument, it must be a dictionary mapping Unicode | ordinals (integers) or characters to Unicode ordinals, strings or None. | Character keys will then be converted to ordinals. | If there are two arguments, they must be strings of equal length, and | in the resulting dictionary, each character in x will be mapped to the | character at the same position in y. If there is a third argument, it | must be a string, whose characters will be mapped to None in the result.''' lt = list(txt.replace(' ', '').replace('\n', '')) # 去除空格和换行符后转换为列表[^1] from collections import Counter frequency = Counter(lt) # 统计字符出现次数 print(frequency) ``` 上述代码片段展示了如何将多行字符串去除多余空白字符后拆分为单独字符组成的列表,并利用 `collections.Counter` 对其进行频次统计。 #### 插入操作与时间复杂度分析 对于插入操作而言,在不同数据结构上的表现各有优劣。例如数组实现方式虽然能够快速定位目标索引处的值(查找时间为 \(O(1)\)),但在执行插入动作时可能需要移动大量后续元素从而导致整体性能下降至线性级别 \(O(n)\)[^3]。相比之下,链表则更适合频繁增删场景下的应用需求。 然而值得注意的是,这里提到的方法主要是针对简单的一维序列化对象如整型或者浮点数值等基本类型适用;而对于更复杂的自定义类实例或者其他高级别的抽象层次上,则需考虑更多因素才能决定最佳实践方案。 #### 关于集合(Set)的具体操作说明 在 Python 当中,我们可以创建一个新集合并通过一系列预定义好的方法来进行各种运算处理: - **Union**: 合集 A ∪ B 表示属于A 或者 属于B 的所有成员构成的新集合. ```python s1 = {'apple','banana'} s2 = {'orange','grape'} union_set = s1.union(s2) print(union_set) #{'orange', 'banana', 'apple', 'grape'} ``` - **Intersection**:交集 A ∩ B 则指既存在于A 又存在于是B当中的那些共同组成部分. ```python intersection_set=s1.intersection(s2) print(intersection_set)#如果没有任何重叠部分会返回空集:set() ``` 还有差集(A-B),对称差等等诸多实用功能等待探索学习. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值