Py学习  »  Python

在python中与pandas一起实现sqlite sort by函数和select函数

ksananthu • 5 年前 • 1621 次点击  

我正在写一个程序来搜索数据库。我的表包含id,title,date,content。我只需要日期和内容作为输出。输出必须根据“id”进行排序。但我不希望输出id。我该如何实现? 我试图在选择id和内容之前对数据进行排序,不幸的是,它没有给出所需的结果。

import os
import pandas as pd

conn = sqlite3.connect('insight.db')
c = conn.cursor()

def search_keyword(term):
    c.execute("SELECT * FROM GkData ORDER BY Id DESC")
    print(pd.read_sql_query("SELECT date, content FROM GkData WHERE {} LIKE '%{}%'".format('content', term), conn))
    c.execute("SELECT content FROM GkData WHERE {} LIKE '%{}%'".format('content', term))
    data = c.fetchall()
    for idx, row in enumerate(data):
        new_data = str(row).replace("',)", "  ").replace("('", " ")
        print('[ ' + str(idx) + ' ] =====> ' + new_data)

while True:
    search = input("Search term: ")
    search_keyword(str(search))

conn.close()

Output image of the program

Image of Table

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/42976
 
1621 次点击  
文章 [ 1 ]  |  最新文章 5 年前
balderman
Reply   •   1 楼
balderman    6 年前

“输出必须根据'id'进行排序。但我不希望输出id。我如何实现这一点?”

SELECT date, content FROM GkData WHERE content LIKE %something% ORDER BY id ASC