Home>

Currently, I want to store the information obtained from the API in a SQL database.

I want to transfer the information obtained from the Twitter API to the SQL database, so
https://www.analyticsvidhya.com/blog/2020/08/analysing-streaming-tweets-with-python-and-postgresql/
When I entered the following code with reference to, an error occurred.

>>>from twitter import *
>>>auth = OAuth (
... consumer_key ='QyVL7V9u4m61xeUxRSignDdVR',
... consumer_secret ='fgPDeUP0WKhXSRgkYiH7CrU3qiUEmtUV5DTRboMuq3WSEysATO',
... token = '000',
... token_secret = '000'
...)
>>>params = dict ()
>>>params ["locations"] = "139.48, 35.30, 139.57, 35.34"
>>>twitter_stream = TwitterStream (auth = auth, domain ='stream.twitter.com')
>>>import psycopg2
>>>conn = psycopg2.connect (host ='localhost', dbname ='twitterdb', user ='postgres', password = '000', port = '5432')
>>>cur = conn.cursor ()
>>>command ='''INSERT INTO TwitterUser (id, text, geo.coordinates) VALUES (% s,% s) ON CONFLICT
... (User_Id) DO NOTHING;'''
>>>cur.execute (command, (id, text, geo.coordinates))
Traceback (most recent call last):
  File "<stdin>", line 1, in<module>
NameError: name'text' is not defined

Therefore, I considered the following two possibilities by investigating.
● The exported Twitter data name is incorrect
On the Twitter developer site
Link content
When I referred to, the data name matched the one described in the response field.
● The table information of pgadmin4 does not match.
The datatype described on the site was not on the datatype selection screen of pgadmin4.
Therefore, I think this may be the cause.

Is it because the datatype set in table does not match?
Also, when setting datatype in pgadmin4, is there a type corresponding to string, object, and array?

The image of pgadmin4 is also attached.

  • Answer # 1

    Since the number of values ​​of values ​​was small in insert, it was supported.

Related articles