如何删除Sqlite中的记录 [英] How to delete record in Sqlite

查看:188
本文介绍了如何删除Sqlite中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask的教程应用程序学习Python,我无法想象如何引用数据库中的某一行。我试图通过使用模板中的删除链接删除一条记录。我花了一整天的尝试,我能找到的一切,真正理解这一点。我知道这很简单,但我不明白!



以下是html模板:

 < ul class = entries> 
{%用于输入条目%}
< li>< h2> {{entry.title}}
< h2> {{entry.text | safe}}< / H2>
{%if session.logged_in%}
< a href ={{url_for('delete_entry')}}>删除< / a>
{%endif%}
{%else%}
{%endfor%}
< / ul>

正如您所看到的,每个条目旁边都有一个删除链接。我应该如何引用
在数据库中的正确id?



以下是我到目前为止的Python函数:

<$ p $ ():
中断(401)
g.db.execute('delete from entries where id =(select max(id)from entries)')
g.db.commit()
flash('条目已被删除')
return redirect(url_for('show_entries'))

作品,但这不是我想要的。提前致谢!

解决方案

Sebastien的delete_entry函数正在删除db中的最后一个条目

< > 其中id =(从条目中选择max(id))



您需要做的是插入参数在url中删除,像'/ delete /:id',其中id是要删除的条目的id,然后更改模板以生成url:

 < a href ={{url_for('delete_entry',entry.id)}}>删除< / a> 

还要更改删除条目的db查询:

  g.db.execute('delete from entries where id ='+ id)

Edit1:

我检查了Flask文档,为了获得变量到你的路由中,你必须修改路由如下:

  @ app.route('/ delete /< int:entry_id>')

 

def delete_entry(entry_id):
g.db.execute('delete from entries where id ='+ entry_id)

最后,您必须在url_for函数中指定您的入口ID:

  < a href ={{url_for('delete_entry',entry_id = entry.id)}}>删除< / a> 


I'm learning Python with the tutorial app from Flask and I can't figure how to reference a certain row in the database. I m trying to delete a record by using the delete link in the template.I spent a whole day trying everything I could find and to really understand this. I know it's simple but I don't get it!

Here's the html template:

<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry.title }}
<h2>{{ entry.text|safe }}</h2>
{% if session.logged_in %}
<a href="{{ url_for('delete_entry') }}">Delete</a>
{% endif %}
{% else %}
{% endfor %}
</ul>

As you can see, next to each entry there's a delete link.How am I supposed to reference the right id in the database?

Here's the Python function I have so far:

@app.route('/delete')
def delete_entry():
if not session.get('logged_in'):
    abort(401)
g.db.execute('delete from entries where id=(select max(id)from entries)') 
g.db.commit()
flash('The entry was deleted')
return redirect(url_for('show_entries'))

What I have so far works but It's not what I want. Thanks in advance!

解决方案

Sebastien the delete_entry function is deleting the last entry in the db

where id=(select max(id)from entries)

What you have to do is insert a parameter in the url for deletion like '/delete/:id' where the id is the id of the entry you want deleted, then change the template to generate the url as so:

<a href="{{ url_for('delete_entry', entry.id) }}">Delete</a>

And also change the db query that deletes the entry:

g.db.execute('delete from entries where id=' + id)

Edit1:

I checked the Flask documentation, in order to get variables into your routes you have to modify the route as follows:

@app.route('/delete/<int:entry_id>')

In this way you obtain the entry_id in your python function:

def delete_entry(entry_id):
g.db.execute('delete from entries where id=' + entry_id)

And finally you have to specify your entry id in the url_for function:

<a href="{{ url_for('delete_entry', entry_id = entry.id) }}">Delete</a>

这篇关于如何删除Sqlite中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆