Python - 更新数据库 [英] Python - update database

查看:48
本文介绍了Python - 更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 sqlite3 更新已存在于数据库中的条目?

How would I update an entry that is already in a database using sqlite3?

我试过了:

db_curs.execute("INSERT INTO `database` (cID) VALUES ('Updated')")

但这当然会创建一个新条目.我可以获得(行(?))号(条目的)以及更新数据库而不是创建新条目的命令是什么?

But this, of course, creates a new entry. I can obtain the (line(?)) number (of the entry) and what is the command to update the database rather than create a new entry?

说得好一点,当我将 SQL 条目转换为 python 列表时,我得到以下内容,

Put a bit better, when I convert the SQL entry to a python list I get the following,

(1, u'GGS-04', u'John', u'Smith', 9, u'0')

我需要能够在最后一项中添加一个数字.这是我所拥有的.

I need to be able to add a digit to the last item. Here is what I have.

info = (1, u'GGS-04', u'John', u'Smith', 9, u'0')

for result in info:
    ln           = str(result[0])
    ggs          = str(result[1])
    first_name   = str(result[2])
    last_name    = str(result[3])
    dob          = str(result[4])
    spend        = str(result[5])
while True:
    res = raw_input("Enter points to add: ")
    try:
        int(res)
        break
    except:
        print "Please enter a number..."
        pass
spend = str(int(spend) + int(res))
db_curs.execute("UPDATE `" + db_name + "` (cID, first_name, last_name, date_of_birth, spend) VALUES ('" + srce + "', '" + first_name + "', '" + last_name + "', '" + dob + "' WHERE '" + spend + "')")
db_connection.commit()

有人能解释一下这个命令的正确语法吗?如果我可以只更新支出"列而不是必须从前缀变量中更新它们,那就太棒了.谢谢:)

Could someone please explain the correct syntax for this command? It would be awesome if I could just update the "Spend" column than having to update them all from prefixed variables. Thank you :)

推荐答案

这应该有效:

db_curs.execute("UPDATE database SET spend =`" + spend + "` WHERE cID="+ cID)

另见 SQL 更新语法

顺便说一句,数据库"对于数据库表来说不是一个有用的名称.用户"或花费时间"或更具描述性的内容呢?

Btw, "database" isn't a useful name for a database's table. What about "users" or "spendtime" or sth more descriptive?

这篇关于Python - 更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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