最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python SQLite3數(shù)據(jù)庫(kù)日期與時(shí)間常見(jiàn)函數(shù)用法分析

 更新時(shí)間:2017年08月14日 11:11:44   作者:羅兵  
這篇文章主要介紹了Python SQLite3數(shù)據(jù)庫(kù)日期與時(shí)間常見(jiàn)函數(shù)用法,結(jié)合實(shí)例形式分析了Python連接、查詢(xún)SQLite3數(shù)據(jù)以及數(shù)據(jù)庫(kù)日期與時(shí)間常見(jiàn)操作方法,需要的朋友可以參考下

本文實(shí)例講述了Python SQLite3數(shù)據(jù)庫(kù)日期與時(shí)間常見(jiàn)函數(shù)。分享給大家供大家參考,具體如下:

import sqlite3
#con = sqlite3.connect('example.db')
con = sqlite3.connect(":memory:")
c = con.cursor()
# Create table
c.execute('''CREATE TABLE stocks
       (date text, trans text, symbol text, qty real, price real)''')
# Insert a row of data
c.execute("INSERT INTO stocks VALUES (?,?,?,?,?)", ('2006-03-27','BUY','RHAT',100,60.14))
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
       ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
       ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
       ('2006-04-07', 'SELL', 'MSFT', 500, 74.00),
       ('2006-04-08', 'SELL', 'IBM', 500, 54.00),
       ('2006-04-09', 'SELL', 'MSFT', 500, 73.00),
       ('2006-04-10', 'SELL', 'MSFT', 500, 75.00),
       ('2006-04-12', 'SELL', 'IBM', 500, 55.00),
      ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
# Save (commit) the changes
con.commit()
# Do this instead
t = ('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 日期 & 時(shí)間
# ====================================================================================
print('='*30)
print('SQLite 日期 & 時(shí)間')
print('='*30)
# 計(jì)算當(dāng)前日期
c.execute("SELECT date('now')")
print(c.fetchone())
# 計(jì)算當(dāng)前月份的最后一天:
c.execute("SELECT date('now','start of month','+1 month','-1 day');")
print(c.fetchone())
# 計(jì)算給定 UNIX 時(shí)間戳 1092941466 的日期和時(shí)間:
c.execute("SELECT datetime(1092941466, 'unixepoch');")
print(c.fetchone())
# 計(jì)算給定 UNIX 時(shí)間戳 1092941466 相對(duì)本地時(shí)區(qū)的日期和時(shí)間:
c.execute("SELECT datetime(1092941466, 'unixepoch', 'localtime');")
print(c.fetchone())
# 計(jì)算當(dāng)前的 UNIX 時(shí)間戳:
c.execute("SELECT datetime(1092941466, 'unixepoch', 'localtime');")
print(c.fetchone())
# 計(jì)算美國(guó)"獨(dú)立宣言"簽署以來(lái)的天數(shù):
c.execute("SELECT julianday('now') - julianday('1776-07-04');")
print(c.fetchone())
# 計(jì)算從 2004 年某一特定時(shí)刻以來(lái)的秒數(shù):
c.execute("SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');")
print(c.fetchone())
# 計(jì)算當(dāng)年 10 月的第一個(gè)星期二的日期:
c.execute("SELECT date('now','start of year','+9 months','weekday 2');")
print(c.fetchone())
# 計(jì)算從 UNIX 紀(jì)元算起的以秒為單位的時(shí)間(類(lèi)似 strftime('%s','now') ,不同的是這里有包括小數(shù)部分):
c.execute("SELECT (julianday('now') - 2440587.5)*86400.0;")
print(c.fetchone())
# 在 UTC 與本地時(shí)間值之間進(jìn)行轉(zhuǎn)換,當(dāng)格式化日期時(shí),使用 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 常用函數(shù)
# ====================================================================================
print('='*30)
print('SQLite 常用函數(shù)')
print('='*30)
con = sqlite3.connect(":memory:")
c = con.cursor()
# Create table
c.execute('''CREATE TABLE COMPANY
      (ID integer, NAME text, AGE integer, ADDRESS text, SALARY real)''')
# Larger example that inserts many records at a time
purchases = [(1,'Paul',32,'California',20000.0),
       (2,'Allen',25,'Texas',15000.0),
       (3,'Teddy',23,'Norway',20000.0),
       (4,'Mark',25,'Rich-Mond',65000.0),
       (5,'David',27,'Texas',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 changes
con.commit()
# 返回?cái)?shù)據(jù)庫(kù)表最后 n 行記錄
# 先計(jì)算一個(gè)數(shù)據(jù)庫(kù)表中的行數(shù)
c.execute("SELECT count(*) FROM COMPANY;")
last = c.fetchone()[0]
n = 5
c.execute("SELECT * FROM COMPANY LIMIT ? OFFSET ?;", (n, last-n))
for row in c:
  print(row)
# 計(jì)算一個(gè)數(shù)據(jù)庫(kù)表中的行數(shù)
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())
# 計(jì)算某列的平均值
c.execute("SELECT avg(salary) FROM COMPANY;")
print(c.fetchone())
# 為一個(gè)數(shù)值列計(jì)算總和
c.execute("SELECT sum(salary) FROM COMPANY;")
print(c.fetchone())
# 返回一個(gè)介于 -9223372036854775808 和 +9223372036854775807 之間的偽隨機(jī)整數(shù)
c.execute("SELECT random() AS Random;")
print(c.fetchone())
# 返回?cái)?shù)值參數(shù)的絕對(duì)值
c.execute("SELECT abs(5), abs(-15), abs(NULL), abs(0), abs('ABC');")
print(c.fetchone())
# 把字符串轉(zhuǎn)換為大寫(xiě)字母
c.execute("SELECT upper(name) FROM COMPANY;")
print(c.fetchone())
# 把字符串轉(zhuǎn)換為小寫(xiě)字母
c.execute("SELECT lower(name) FROM COMPANY;")
print(c.fetchone())
# 返回字符串的長(zhǎng)度
c.execute("SELECT name, length(name) FROM COMPANY;")
print(c.fetchone())
# 返回 SQLite 庫(kù)的版本
c.execute("SELECT sqlite_version() AS 'SQLite Version';")
print(c.fetchone())
#
c.execute("SELECT CURRENT_TIMESTAMP;")
print(c.fetchone())

PS:這里再為大家推薦2款SQL工具,附帶常用語(yǔ)句,供大家參考:

SQL在線(xiàn)壓縮/格式化工具:
http://tools.jb51.net/code/sql_format_compress

在線(xiàn)SQL格式化/壓縮工具:
http://tools.jb51.net/code/sql_fmt_yasuo

另:關(guān)于時(shí)間戳轉(zhuǎn)換還可參考本站時(shí)間戳轉(zhuǎn)換工具(附帶各種常用編程語(yǔ)言時(shí)間戳操作):

Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Python日期與時(shí)間操作技巧總結(jié)》、《Python常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

巩留县| 柳州市| 公安县| 玉树县| 丘北县| 永仁县| 永寿县| 克拉玛依市| 夏津县| 武胜县| 肥城市| 镇沅| 仙游县| 绥中县| 泾源县| 乳源| 阿拉尔市| 平南县| 谢通门县| 阳信县| 翼城县| 耒阳市| 靖州| 星子县| 巴楚县| 即墨市| 那曲县| 浮山县| 玉林市| 九寨沟县| 廉江市| 靖边县| 富锦市| 宁陵县| 大渡口区| 鲜城| 怀仁县| 柘城县| 信宜市| 阳西县| 徐闻县|