Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vivekananda_Session3_4VP20CS005_Ankita Dattatreya Hegde #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AnkitaHegde
Copy link

Assignment is within myworld1.

# This function will add the entry to database
sql = """INSERT INTO members_blog (title, release_date, blog_time, created_date,content, author,recommended,path) VALUES (%s, %s::DATE, %s::TIME, NOW(),%s,%s,%s,%s)"""

with conn:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde Why with is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because with will perform automatic closing of resources. If not used we have explicitly close those connection and cursors.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde Ok, so you mentioned about "closing of resources". I'll agree that it's correct. But you're actually inserting data into the table and in another method you're truncating the table, I don't see where you're committing the transaction. So how is the data stored in the database if there's no commit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default database will be in auto-commit mode . So commit statement is not explicitly written.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde Could you add the resource which says that the database will be in auto-commit mode by default or how did you conclude it?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde which database are you using to store the data in your code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its postgres. Here its about sql but I thought those auto-commit things are similar in these, aren't they?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde The line quoted by you in that article clearly talks about mysql behaviour and not SQL in general. Also whatever mentioned in that paragraph(even in case of postgres) holds true for console where you're actually executing the SQL queries directly and it is auto-committed.
But in your case, you're actually executing queries using your python script and you're using psycopg2 for it. This means you're not directly interacting with the database but using a API(psycopg2) to interact. So my original question still stands.
So, I would want you to understand what is with and then how/why with is used?
You can comment your answer regarding what you've understood and then we can move on to answering the original question(provided your understanding of with is correct).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i am following what you are saying. Could you please brief out about 'with' , I think I went wrong from here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde As I said before, first I want you understand with and why would someone use with, on your own and then comment out your understanding on it, so that I can correct you(if you're wrong at some point) or I can help you connect the dots.
You can write your own simple scripts and experiment with it. Take your time to understand it.


with conn:
with conn.cursor() as curs:
time=time.replace('\u202f',"")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde Why are you replacing it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Over there, as we scrape the time we were getting white-spaces which was causing InvalidSyntax error so just replaced that unicode with empty string.

fp=open("scrapped_data.html","w")
path=os.path.abspath("scrapped_data.html")

for blog in blogs:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnkitaHegde If any exception occurs in this loop, what happens to the file?

Copy link
Author

@AnkitaHegde AnkitaHegde Jun 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As to my knowledge file will get closed automatically when exception occurs. And even some resource on internet said so and they also even suggested to use with as a precaution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants