2016-09-21 59 views
0

我在网站上查询数据的json,然后将该数据保存到变量中,以便我可以将它放入sqlite表中。我为3我想要做的3,但sqlite方面只是神秘。我能够请求数据,从那里我可以验证该变量有数据,当我测试打印,但我的所有sqlite的东西都失败了。它甚至不创建表格,更不用说更新表格(但由于某种原因它将所有结果打印到缓冲区中)任何想法我在这里做错了?免责声明:一个python noob的位。我已经成功地创建测试表只是复制的东西掉python sqlite doc将python变量数据插入sqlite表中不保存

# 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 64位,并使用pycharm做到这一点的。

回答

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} 
)