sqlite python 插入 [英] sqlite python insert

查看:29
本文介绍了sqlite python 插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过类似的问题,这里是我想要实现的目标的详细说明

I have asked similar question before , here is detailed explanation of what i'm trying to achieve

我有两个 sqlite 表
table1 - 是标准表 - 具有服务器、id、状态等字段table2 - 具有服务器、id、状态、编号、日志文件等字段

I have two sqlite tables
table1 - is standard table - has fields like server,id,status table2 - has fields like server,id,status,number,logfile

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

Table2 is empty and Table1 has values. I'm trying to fill table2 with entries from table1. I can retrieve records from table1 ,but while trying to insert into table2 it fails.(but inserting a single field works)

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],));

还让我知道如何在插入失败时产生更多有用的错误消息

And also let me know how to produce more useful error messages when insert fails

推荐答案

此声明已失效:

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 因为我认为它可以为你节省大量时间.

And you might want to look into executemany as I think it could save you a ton of time.

这篇关于sqlite python 插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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