質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

651閲覧

ツイートデータをSQLデータベースに移す際のtweet情報について

suwakou

総合スコア13

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Twitter

Twitterは、140文字以内の「ツイート」と呼ばれる短文を投稿できるサービスです。Twitter上のほぼ全ての機能に対応するAPIが存在し、その関連サービスが多く公開されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/11/16 06:29

サイトリンク内容を参考に
pythonを使ってTwitterAPIから得た情報をpostgreSQLに格納したいのですが、
ツイートデータをデータベースに挿入するところでnameエラーとなってしまいました。

>>> 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, user_name, text, bounding_box_coordinates) VALUES (%s,%s) ON CONFLICT ... (User_Id) DO NOTHING;''' >>> cur.execute(command,(id, user_name, text, bounding_box_coordinates)) #エラー Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'user_name' is not defined

取得したtweetデータと合致していないのか、SQL作成したtableと合致していないのかわからないです。
エラーの条件を探るべく、取得する情報を変えながらtable名を合致させつつやってみましたが、選択する情報をuser_idではなくid(どちらもtweet情報にある)に変えたところ、nameエラーがuser_nameに移りました。

>>> command = '''INSERT INTO twitteruser (user_id, user_name, text, bounding_box_coordinates) VALUES (%s,%s) ON CONFLICT ... (User_Id) DO NOTHING;''' >>> cur.execute(command,(user_id,user_name,text, bounding_box_coordinates)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'user_id' is not defined >>> command = '''INSERT INTO twitteruser (id, user_name, text, bounding_box_coordinates) VALUES (%s,%s) ON CONFLICT ... (User_Id) DO NOTHING;''' >>> cur.execute(command,(id,user_name,text, bounding_box_coordinates)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'user_name' is not defined >>> command = '''INSERT INTO twitteruser (id, name, text, bounding_box_coordinates) VALUES (%s,%s) ON CONFLICT ... (User_Id) DO NOTHING;''' >>> cur.execute(command,(id,name,text,bounding_box_coordinates)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'name' is not defined >>> command = '''INSERT INTO twitteruser (id, user_name, text, bounding_box_coordinates) VALUES (%s,%s) ON CONFLICT ... (User_Id) DO NOTHING;''' >>> cur.execute(command,(id, user_name, text, bounding_box_coordinates)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'user_name' is not defined >>> cur.execute(command,(id, text, user_name, bounding_box_coordinates)) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'text' is not defined

以下の画像がコードに対応させているtable情報です。
イメージ説明

以下が取得したいtweet情報です。
{'id': 13000,,,, 'text': '佐世保にしらせ来とったんか〜い https://t.co/775xk4UW4o', 'user': {'id': 2000,,,, 'id_str': '000', 'name': 'あ000','bounding_box': {'type': 'Polygon', 'coordinates': [[[000, 000], [34, 4], [17, 45.56000], [000, 000]]]}, 'attributes': {}}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問