【问题标题】:Update mulitple values of a single column in an SQLite DB using Python使用 Python 更新 SQLite DB 中单个列的多个值
【发布时间】:2014-09-22 09:35:34
【问题描述】:

我正在使用 Python 访问 SQLite DB (3.7.11):

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()

要插入/添加单行,我可以使用例如:

c.execute("INSERT INTO random_points (value) VALUES ("42")")

我的表已经有 100 行,我不会添加一行 喜欢用随机数更新整个值列 mu=0 和 sigma=1 的高斯分布:

import random
random_numbers = [random.gauss(0,1) for x in range(0,100)]

如何使用这些随机数将它们填充到值列中 我的 Sqlite 数据库?

【问题讨论】:

  • 您真的需要更新吗?你不能扔掉旧行并插入新行吗?
  • 我不能丢弃旧行,因为该表还包含除 value-col 之外的其他一些列。

标签: python sqlite random sql-update


【解决方案1】:

如果您有一列,您可以使用for 循环:

random_numbers = [random.gauss(0,1) for x in range(0,100)]
for number in random_numbers :
    cur.execute("UPDATE word SET column_name=? WHERE your_condition ",(number)) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 2011-02-11
    • 2015-04-24
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多