从限制的行中获取数据

时间:2016-06-25 16:48:22

标签: python python-2.7 sqlite

我从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 个答案:

答案 0 :(得分:0)

  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)
    
  3. 如果您对python sqlite3感兴趣,请重定向到here