Answer the question
In order to leave comments, you need to log in
How to effectively work with the database in this code?
There is a code
for post in media:
id = post['node']['id']
like = post['node']['edge_liked_by']['count']
comment = post['node']['edge_media_to_comment']['count']
photo = post['node']['display_url']
// ищем посты в базе данных
post = cursor.execute("SELECT id FROM blog_instagram WHERE id LIKE "+id)
count = 0
// если есть такой id то увеличиваем счетчик на 1
for row in post:
count += 1
values = "'" + str(like) + "', '" + str(comment) + "', '" + str(photo) + "', '" + str(id) + "'"
// если счетчик > 0 то пропускаем т.к. посты должны быть уникальны в базе
if (count == 0):
cursor.execute("INSERT INTO blog_instagram VALUES(" + values + ")")
conn.commit()
Answer the question
In order to leave comments, you need to log in
Maxim Stikharev ,
what you have written should look something like this (if I understand correctly):
for post in media:
id = post['node']['id']
if cursor.execute("SELECT count(id) FROM blog_instagram WHERE id = ?", id):
continue
cursor.execute("INSERT INTO blog_instagram(id, like, comment, photo) VALUES(?, ?, ?, ?)",
[id,
post['node']['edge_media_to_comment']['count'],
post['node']['edge_liked_by']['count'],
post['node']['display_url']]
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question