Python SQLite3数据库日期与时间常见函数用法分析

Python SQLite3数据库日期与时间常见函数用法分析,第1张

概述本文实例讲述了PythonSQLite3数据库日期与时间常见函数。分享给大家供大家参考,具体如下:

本文实例讲述了Python sqlite3数据库日期与时间常见函数。分享给大家供大家参考,具体如下:

import sqlite3#con = sqlite3.connect('example.db')con = sqlite3.connect(":memory:")c = con.cursor()# Create tablec.execute('''CREATE table stocks       (date text,trans text,symbol text,qty real,price real)''')# Insert a row of datac.execute("INSERT INTO stocks VALUES (?,?,?)",('2006-03-27','BUY','RHAT',100,60.14))# Larger example that inserts many records at a timepurchases = [('2006-03-28','IBM',1000,45.00),('2006-04-05','MSFT',72.00),('2006-04-06','SELL',500,53.00),('2006-04-07',74.00),('2006-04-08',54.00),('2006-04-09',73.00),('2006-04-10',75.00),('2006-04-12',55.00),]c.executemany('INSERT INTO stocks VALUES (?,?)',purchases)# Save (commit) the changescon.commit()# Do this insteadt = ('RHAT',)c.execute('SELECT * FROM stocks WHERE symbol=?',t)#print(c.fetchone())#for row in c.execute('SELECT * FROM stocks ORDER BY price'):#  print(row)#for row in c.execute('SELECT * FROM stocks liMIT 5 OFFSET 0'):#  print(row)for row in c.execute('SELECT * FROM stocks liMIT 5 OFFSET 1'):  print(row)#Select top N * From# ====================================================================================# sqlite 日期 & 时间# ====================================================================================print('='*30)print('sqlite 日期 & 时间')print('='*30)# 计算当前日期c.execute("SELECT date('Now')")print(c.fetchone())# 计算当前月份的最后一天:c.execute("SELECT date('Now','start of month','+1 month','-1 day');")print(c.fetchone())# 计算给定 UNIX 时间戳 1092941466 的日期和时间:c.execute("SELECT datetime(1092941466,'unixepoch');")print(c.fetchone())# 计算给定 UNIX 时间戳 1092941466 相对本地时区的日期和时间:c.execute("SELECT datetime(1092941466,'unixepoch','localtime');")print(c.fetchone())# 计算当前的 UNIX 时间戳:c.execute("SELECT datetime(1092941466,'localtime');")print(c.fetchone())# 计算美国"独立宣言"签署以来的天数:c.execute("SELECT julianday('Now') - julianday('1776-07-04');")print(c.fetchone())# 计算从 2004 年某一特定时刻以来的秒数:c.execute("SELECT strftime('%s','Now') - strftime('%s','2004-01-01 02:34:56');")print(c.fetchone())# 计算当年 10 月的第一个星期二的日期:c.execute("SELECT date('Now','start of year','+9 months','weekday 2');")print(c.fetchone())# 计算从 UNIX 纪元算起的以秒为单位的时间(类似 strftime('%s','Now') ,不同的是这里有包括小数部分):c.execute("SELECT (julianday('Now') - 2440587.5)*86400.0;")print(c.fetchone())# 在 UTC 与本地时间值之间进行转换,当格式化日期时,使用 utc 或 localtime 修饰符,如下所示:c.execute("SELECT time('12:00','localtime');")print(c.fetchone())#c.execute("SELECT time('12:00','utc');")print(c.fetchone())con.close()# ====================================================================================# sqlite 常用函数# ====================================================================================print('='*30)print('sqlite 常用函数')print('='*30)con = sqlite3.connect(":memory:")c = con.cursor()# Create tablec.execute('''CREATE table COMPANY      (ID integer,name text,AGE integer,ADDRESS text,SALARY real)''')# Larger example that inserts many records at a timepurchases = [(1,'Paul',32,'California',20000.0),(2,'Allen',25,'Texas',15000.0),(3,'Teddy',23,'norway',(4,'Mark','Rich-Mond',65000.0),(5,'DavID',27,85000.0),(6,'Kim',22,'South-Hall',45000.0),(7,'James',24,'Houston',10000.0)]c.executemany('INSERT INTO COMPANY VALUES (?,purchases)# Save (commit) the changescon.commit()# 返回数据库表最后 n 行记录# 先计算一个数据库表中的行数c.execute("SELECT count(*) FROM COMPANY;")last = c.fetchone()[0]n = 5c.execute("SELECT * FROM COMPANY liMIT ? OFFSET ?;",(n,last-n))for row in c:  print(row)# 计算一个数据库表中的行数c.execute("SELECT count(*) FROM COMPANY;")print(c.fetchone())# 选择某列的最大值c.execute("SELECT max(salary) FROM COMPANY;")print(c.fetchone())# 选择某列的最小值c.execute("SELECT min(salary) FROM COMPANY;")print(c.fetchone())# 计算某列的平均值c.execute("SELECT avg(salary) FROM COMPANY;")print(c.fetchone())# 为一个数值列计算总和c.execute("SELECT sum(salary) FROM COMPANY;")print(c.fetchone())# 返回一个介于 -9223372036854775808 和 +9223372036854775807 之间的伪随机整数c.execute("SELECT random() AS Random;")print(c.fetchone())# 返回数值参数的绝对值c.execute("SELECT abs(5),abs(-15),abs(NulL),abs(0),abs('ABC');")print(c.fetchone())# 把字符串转换为大写字母c.execute("SELECT upper(name) FROM COMPANY;")print(c.fetchone())# 把字符串转换为小写字母c.execute("SELECT lower(name) FROM COMPANY;")print(c.fetchone())# 返回字符串的长度c.execute("SELECT name,length(name) FROM COMPANY;")print(c.fetchone())# 返回 sqlite 库的版本c.execute("SELECT sqlite_version() AS 'sqlite Version';")print(c.fetchone())#c.execute("SELECT CURRENT_TIMESTAMP;")print(c.fetchone())

PS:这里再为大家推荐2款sql工具,附带常用语句,供大家参考:

sql在线压缩/格式化工具:
http://tools.jb51.net/code/sql_format_compress

在线sql格式化/压缩工具:
http://tools.jb51.net/code/sql_fmt_yasuo

另:关于时间戳转换还可参考本站时间戳转换工具(附带各种常用编程语言时间戳 *** 作):

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python *** 作SQLite数据库技巧总结》、《Python日期与时间 *** 作技巧总结》、《Python常见数据库 *** 作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串 *** 作技巧汇总》、《@L_502_9@》及《Python文件与目录 *** 作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Python SQLite3数据库日期与时间常见函数用法分析全部内容,希望文章能够帮你解决Python SQLite3数据库日期与时间常见函数用法分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1201741.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存