tweepy stream to sqlite database - invalid synatx

后端 未结 4 1752
你的背包
你的背包 2021-01-07 10:05

The code below is streaming the twitter public timeline for a variable which output any tweets to the console. I\'d like the save the same variables (status.text, status.aut

相关标签:
4条回答
  • 2021-01-07 10:26

    I'm quite new to tweepy . But these are the modifications that worked for me . You need to add VALUES after INSERT INTO TWEETS . Also , don't forget to commit the changes . This is the link I referred to : related post

         cur.execute("INSERT INTO TWEETS VALUES(?, ?, ?, ?)", (status.text, 
                                                            status.author.screen_name, 
                                                            status.created_at, 
                                                            status.source))
    
         con.commit()
    
    0 讨论(0)
  • 2021-01-07 10:27

    You are missing a closing parenthesis on the last line of the following code (lines 34–37 from what you posted):

                cur.executemany("INSERT INTO TWEETS(?, ?, ?)", (status.text, 
                                                            status.author.screen_name, 
                                                            status.created_at, 
                                                            status.source)
    

    Just add a parenthesis to close the method call immediately after your tuple parameter.

    0 讨论(0)
  • 2021-01-07 10:33
    import sqlite3 as lite
    con = lite.connect('test.db')
    cur = con.cursor()   
    
    cur.execute("CREATE TABLE TWEETS(txt text, author text, created int, source text)")
    

    then later:

    cur.executemany("INSERT INTO TWEETS(?, ?, ?, ?)", (status.text, 
                                          status.author.screen_name, 
                                          status.created_at, 
                                          status.source))
    
    0 讨论(0)
  • 2021-01-07 10:37

    Full disclosure: still new to this stuff. However, I got your code working by changing it to:

    cur.execute("INSERT INTO TWEETS VALUES(?,?,?,?)", (status.text, status.author.screen_name, status.created_at, status.source))
    con.commit()
    

    It seems to me that you're reading in one status at a time. The executemany method would be for when you have more than one status. For example:

    (['sometext', 'bob','2013-02-01','Twitter for Android'], ['someothertext', 'helga', '2013-01-31', 'MacSomething'])

    I'm definitely not a wizard and am not sure what sort of impact the commit() has on every entry... I'm guessing the performance is terrible, but it works for a single term in the query.

    Thanks for posting your code, I finally learned how to do streaming.

    0 讨论(0)
提交回复
热议问题