python - Error loading data to postgres with if statement
问题描述
I am trying to save in a database a serie of tweets from a local newspaper on a postgres database using python. My SQL query worked perfectly from PgAdmin when I tried to load data manually. My problem comes when I try to automate the process with Python. Can anyone review my code, I tried everything I could think of.
def obtiene_tweets(usuario, auth = auth_titter(), cantidad = 100):
fuente = obtiene_id(usuario)
##Conección a twitter
##API
api = tweepy.API(auth)
## Aertura de conección a base
conn = psycopg2.connect(dbname = config.DB_NAME,
user = config.DB_USER,
password = config.DB_PASSWORD,
host = config.DB_HOST)
##Tweets
### Este loop accede a los tweets del usuario y obtiene la información de interés para que despúes
### Se la pongamos a la base de datos
cur = conn.cursor()
for tweet in tweepy.Cursor(api.user_timeline, screen_name = fuente.loc['usuario',].values[0], tweet_mode= 'extended').items(cantidad):
#print(json.dumps(tweet._json, indent=2))
full_text = tweet._json['full_text']
id = fuente.loc['id',].values[0]
fecha = tweet._json['created_at']
try:
hashtags = str(tweet._json['entities']['hashtags']).replace('[','')
hashtags = hashtags.replace(']','')
if hashtags == '':
hashtags = 'Null'
except:
pass
try:
url = tweet._json['entities']['urls'][0]['expanded_url']
except:
url = 'Null'
try:
likes = tweet._json['favorite_count']
rts = tweet._json['retweet_count']
except:
pass
cur.execute(sql_statements.CARGA_TWEETS, (full_text.replace("'",'"'),id,full_text.replace("'",'"'), fecha, hashtags, url, likes, rts))
cur.close()
conn.close()
Here is the sql query:
CARGA_TWEETS = """
DO $$
BEGIN
IF EXISTS(SELECT tweet FROM tweets WHERE tweet = %s ) THEN
raise notice 'Se encuentra el tweet';
ELSE
insert into tweets(id_usuario, tweet, fecha, hashtags, urls, likes, rts)
values(%s, %s, %s, %s, %s, %s, %s );
END IF;
END $$
"""
My error looks like this:
(resumen_noticias) mariano@mariano-HP-Laptop-15-da2xxx:/media/mariano/Nuevo vol/CarpetaProtegida/Provincia/Propuestas/Proyectos/resumen-noticias (main)$ /home/mariano/anaconda3/envs/resumen_noticias/bin/python "/media/mariano/Nuevo vol/CarpetaProtegida/Provincia/Propuestas/Proyectos/resumen-noticias/funciomes.py"
Traceback (most recent call last):
File "/media/mariano/Nuevo vol/CarpetaProtegida/Provincia/Propuestas/Proyectos/resumen-noticias/funciomes.py", line 121, in <module>
obtiene_tweets('clarineconomico')
File "/media/mariano/Nuevo vol/CarpetaProtegida/Provincia/Propuestas/Proyectos/resumen-noticias/funciomes.py", line 111, in obtiene_tweets
cur.execute(sql_statements.CARGA_TWEETS, (full_text.replace("'",''),id,full_text.replace("'",''), fecha, hashtags, url, likes, rts))
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(200)
CONTEXT: SQL statement "insert into tweets(id_usuario, tweet, fecha, hashtags, urls, likes, rts)
values(9, 'José Mujica: Me he puesto viejo sintiendo de crisis en Argentina y sin embargo, no sé cómo, pero siempre salen adelante url', 'Fri Aug 27 13:26:22 +0000 2021', 'Null', 'extended_url', 7, 1 )"
PL/pgSQL function inline_code_block line 8 at SQL statement
Thank you everyone who takes time to answer.
解决方案
请分享tweet
表格定义。
看起来你的字段太长了。
value too long for type character varying(200)
您有 2 个选项:(或在选项之间)
- 截断字段
- 更改表定义
推荐阅读
- postgresql - 在单个插入语句中是否可以有多个 on 冲突条件?
- c - 为什么即使我设置了 DataTypeReplacement = 'CBuiltIn',MATLAB Coder 也不会生成 C 内置类型?
- python - 为什么我的图像没有在 python tkinter 中显示?
- javascript - Firestore 如何在文档列表中获取顶级集合?
- core-data - SwiftUI 中 FetchRequest 的独立托管对象上下文
- reactjs - 我的计时器在随机时间停止工作 - 反应
- node.js - 在 Material UI 组件中显示来自 Redux 的外键值和图像
- reactjs - React useState 在渲染中没有改变值
- embedded - UART、SPI、I2C 等实现是通用的吗?
- sql - 在两个日期之间选择时出现 SQL 语法错误