繁体   English   中英

从具有限制的行中获取数据

[英]Fetch the data from rows with limit

我正在从sqlite3数据库中获取完整数据列表。 我想为每个通道提取20行数据,但是在我的代码中,它将获取每个通道的所有数据。

这是代码:

#get the programs list
profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db'))
conn = database.connect(profilePath)
cur = conn.cursor()
cur.execute('SELECT channel, title, start_date, stop_date FROM programs where channel=?', [channel])
programList = list()
programs = cur.fetchall()

start_pos = 375    # indent for first program

for ind, row in enumerate(programs):
   title = row[1]

您能告诉我如何从数据库中获取20行数据而不获取每个通道的所有数据吗?

  1. 请在您选择的查询中使用limit 20

     cur.execute('SELECT channel, title, start_date, stop_date FROM programs where channel=? limit 20', [channel]) 
  2. 我们也可以使用python sqlite做到这一点

     #get the programs list profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', 'source.db')) conn = database.connect(profilePath) cur.execute('SELECT channel, title, start_date, stop_date FROM programs where channel=?', [channel]) for id, row in enumerate(programs): if id == 20: break else: print(id, row) 

如果您对python sqlite3感兴趣,请重定向到此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM