将python变量数据插入sqlite表而不保存

时间:2016-09-21 23:31:39

标签: python sqlite zendesk

我在网站上查询数据的json,然后将数据保存到变量中,这样我就可以把它放到sqlite表中了。对于我正在尝试做的事情,我只有3分,但sqlite方面只是神秘莫测。我能够请求数据,从那里我可以验证变量有数据,当我用打印测试它,但我的所有sqlite东西都失败了。它甚至没有创建一个表,更不用说更新表了(但是由于某种原因它将所有结果打印到缓冲区)任何想法我在这里做错了什么?免责声明:一点蟒蛇菜鸟。我已经成功创建了测试表,只是从python sqlite doc

复制了这些东西
# this is requesting the data and seems to work
for ticket in zenpy.search("bananas"):
id = ticket.id
subj = ticket.subject
created = ticket.created_at
for comment in zenpy.tickets.comments(ticket.id):
    body = comment.body

# connecting to sqlite db that exists. things seem to go awry here
conn = sqlite3.connect('example.db')
c = conn.cursor()

# Creating the table table (for some reason table is not being created at all)
c.execute('''CREATE TABLE tickets_test
         (ticket id, ticket subject, creation date, body text)''')

# Inserting the variables into the sqlite table 
c.execute("INSERT INTO ticketstest VALUES (id, subj, created, body)")

# committing changes the changes and closing
c.commit()
c.close()

我在Windows 64bit上并使用pycharm来执行此操作。

1 个答案:

答案 0 :(得分:0)

您的表可能未创建,因为您尚未提交,并且您的sql在提交之前失败。它应该在您修复第二个sql语句时起作用。

您没有将您创建的变量插入表格中。您需要使用参数。有two ways of parameterizing your sql statement。我将向命名的占位符显示一个:

c.execute("INSERT INTO ticketstest VALUES (:id, :subj, :created, :body)", 
    {'id':id, 'subj':subj, 'created':created, 'body':body}
)