How to work effectively with the database in this code?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
20 views
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'] // looking for posts in the database post cursor.execute("SELECT id FROM blog_instagram WHERE id LIKE "+id) count = 0 // if there is such id then increment counter by 1 for row in post: count += 1 values = "'" + str(like) + "', '" + str(comment) + "', '" + str(photo) + "', '" + str(id) + "'" // if counter > 0 then skip because the posts must be unique in the database if (count == 0): cursor.execute("INSERT INTO blog_instagram VALUES(" + values + ")") conn.commit()


It seems to me that it is not effective in the database, let's say if the posts will be a couple of hundred thousand it will be hard to machine so much of information to process quickly how to do it?
by | 20 views

2 Answers

0 like 0 dislike
1. To love read the documentation to know what are placeholders and how to insert data in sql query https://docs.python.org/3.6/library/sqlite3.html#s...
2. Read basically about sql to do this "WHERE id LIKE" to know how to count the number of records to organize and uniqueness.
3. About inserting large amounts of data, each DBMS has its own nuances from special teams, to tuning database settings.
PS sqlite is developing and indulge, it is better to switch to the normal database.
by
0 like 0 dislike
Maxim Stiharev,

what you have written, should look approximately 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']] )


but there immediately arises a lot of questions )
1) you correctly understand the meaning of the id field in a relational database (I ask because it is usually not inserted)
if this refers to the post_id or something like that then you there is a mess in the names which confuses the whole community.
2) what is the structure of the table
3) what exactly you want to achieve
4) usually the uniqueness of the field guarantees the developer for its curves additional queries and the database. read about the UNIQUE Constraint
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked May 3, 2019 by SnoupS
0 like 0 dislike
1 answer
asked Apr 28, 2019 by guitarjedi
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
35,536 users