Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的Sqlite格式输出_Python_List_Sqlite_Flask_Tuples - Fatal编程技术网

python中的Sqlite格式输出

python中的Sqlite格式输出,python,list,sqlite,flask,tuples,Python,List,Sqlite,Flask,Tuples,我已经建立了一个简单的flask web应用程序,用户可以在其中添加和删除数据库中的任务。数据库中的所有条目都显示在模板中,并按分配的类型排序。不过,我无法将输出格式化为至少具有一定可读性。我该怎么做 实际的数据库使用不同的值和诸如此类的东西,所以现在并非所有这些都有意义 这是从我的sqlite数据库获取所有条目的函数: def get_tsk_by_type(type): c.execute("SELECT * FROM tasks WHERE type=:type", {'type':

我已经建立了一个简单的flask web应用程序,用户可以在其中添加和删除数据库中的任务。数据库中的所有条目都显示在模板中,并按分配的类型排序。不过,我无法将输出格式化为至少具有一定可读性。我该怎么做

实际的数据库使用不同的值和诸如此类的东西,所以现在并非所有这些都有意义

这是从我的sqlite数据库获取所有条目的函数:

def get_tsk_by_type(type):
  c.execute("SELECT * FROM tasks WHERE type=:type", {'type': type})
  result = c.fetchall()
  return result
c.execute("""CREATE TABLE IF NOT EXISTS tasks (
            type text,
            description text,
            amount integer,
            id integer
            )""")
数据库:

def get_tsk_by_type(type):
  c.execute("SELECT * FROM tasks WHERE type=:type", {'type': type})
  result = c.fetchall()
  return result
c.execute("""CREATE TABLE IF NOT EXISTS tasks (
            type text,
            description text,
            amount integer,
            id integer
            )""")
下面是我如何返回模板中显示的所有条目。如果您输入任务id,还可以删除任务

@app.route('/', methods = ['GET', 'POST'])
def index():
  form = DeleteForm()
  curr_homework = str(get_tsk_by_type("homework"))
  curr_cleaning = str(get_tsk_by_type("cleaning"))
  curr_cooking = str(get_tsk_by_type("cooking"))
  if form.validate_on_submit():
    try:
      conn = sqlite3.connect('tasks.db', check_same_thread=False)
      c = conn.cursor()
      delete = request.form['delete']
      if (delete):
        remove_tsk(delete)
      return redirect('/')
      conn.close()
    except:
      return "Something went wrong while submitting the form"
  return render_template('index.html', curr_homework = curr_homwork, curr_cleaning = curr_cleaning, curr_cooking = curr_cooking, form = form)

my index.html的相关部分如下所示:

{% block content %}
    <div>
        <p>
            <span>Currently registered homework: {{ curr_homework }}</span><br />
            <span>Currently registered cleaning tasks: {{ curr_cleaning }}</span><br />
            <span>Currently registered cooking tasks {{ curr_cooking }}</span>
        </p>
    </div>
{% endblock content %}

我尝试过循环之类的东西,但它只会返回列表中的第一个元组,该元组通过类型()返回。我也试过panda,但我也不能让它以我想要的方式输出。我该如何美化它,使其易于阅读?没有括号等。?稍后,我希望单独显示每个任务,最好是以div显示。

我建议使用dict光标,以便您可以按名称访问结果元素。
您可以这样做(从:):

然后你会得到这样的结果:

result = c.fetchall()
result
# [{'type':'homework','description':'math',
#   'amount':1,'id':'df19c0b1-a2128-431274-2e32-3a2f901b1b26'}]
然后在模板中,您可以执行以下操作:

  {% for homework in curr_homework %}
    <div>
      <h6>{{ homework['type'] }}</h6>
      <div>{{ homework['description'] }}</div>
    </div>
    {% if not loop.last %}
      <hr>
    {% endif %}
  {% endfor %}
然后在你看来,这样做:

db = get_db()
db.execute('your query here')

我建议使用dict游标,以便您可以按名称访问结果元素。
您可以这样做(从:):

然后你会得到这样的结果:

result = c.fetchall()
result
# [{'type':'homework','description':'math',
#   'amount':1,'id':'df19c0b1-a2128-431274-2e32-3a2f901b1b26'}]
然后在模板中,您可以执行以下操作:

  {% for homework in curr_homework %}
    <div>
      <h6>{{ homework['type'] }}</h6>
      <div>{{ homework['description'] }}</div>
    </div>
    {% if not loop.last %}
      <hr>
    {% endif %}
  {% endfor %}
然后在你看来,这样做:

db = get_db()
db.execute('your query here')

谢谢你的帮助!
conn.row\u factory=dict\u factory
必须去特定的地方吗
get_tsk\u by_type()
仍然返回元组。因此,我的模板中的代码返回所有
和div,但不返回实际数据。或者我应该以任何方式更改
dict\u factory()
?是的,它需要在您的游标初始化为
get\u tsk\u by\u type()
函数之前进行。让我添加一个示例,说明如何更好地组织此操作。@Lacrowx:添加了如何将连接和游标行工厂代码移动到另一个函数的示例。非常感谢,这很管用,
get_db()
让我的代码更干净了!谢谢你的帮助!
conn.row\u factory=dict\u factory
必须去特定的地方吗
get_tsk\u by_type()
仍然返回元组。因此,我的模板中的代码返回所有
和div,但不返回实际数据。或者我应该以任何方式更改
dict\u factory()
?是的,它需要在您的游标初始化为
get\u tsk\u by\u type()
函数之前进行。让我添加一个示例,说明如何更好地组织此操作。@Lacrowx:添加了如何将连接和游标行工厂代码移动到另一个函数的示例。非常感谢,这很管用,
get_db()
让我的代码更干净了!