【问题标题】:sqlite python insertsqlite python 插入
【发布时间】:2010-01-25 06:37:23
【问题描述】:

我之前也问过类似的问题,这里是我想要达到的目标的详细解释

我有两个 sqlite 表
table1 - 是标准表 - 具有 server、id、status 等字段 table2 - 有 server,id,status,number,logfile 等字段

Table2 为空,Table1 有值。我正在尝试用 table1 中的条目填充 table2。 我可以从 table1 检索记录,但是在尝试插入 table2 时它失败了。(但插入单个字段有效)

self.cursor.execute("select * from server_table1 limit (?)",t)
#insert into server_process

for record in self.server_pool_list:
    print "--->",record # geting output like ---> (u'cloud_sys1', u'free', u'192.168.1.111')
    self.cursor.execute("insert into server_table2(server,status,id) values (?,?,?)",(record[0],)(record[1],),(record[2],));

还告诉我如何在插入失败时生成更有用的错误消息

【问题讨论】:

  • 复制并粘贴生成的错误。
  • 是的..谢谢我解决了。我应该使用这个 (?,?,?"),(record[0],record[1],record[2],) 而不是上面...现在它正在工作:)

标签: python sqlite


【解决方案1】:

这句话被打破了:

self.cursor.execute("insert into server_table2(server,status,id) values (?,?,?)",(record[0],)(record[1],),(record[2],));

应该是:

self.cursor.execute("insert into server_table2(server,status,id) values (?,?,?)",record[0:2])

您可能想查看executemany,因为我认为它可以为您节省大量时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2014-02-05
    • 1970-01-01
    相关资源
    最近更新 更多