Vivekananda_Session3_4VP20CS005_Ankita Dattatreya Hegde#6
Vivekananda_Session3_4VP20CS005_Ankita Dattatreya Hegde#6AnkitaHegde wants to merge 1 commit intoUniCourt:mainfrom
Conversation
| # 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: |
There was a problem hiding this comment.
Because with will perform automatic closing of resources. If not used we have explicitly close those connection and cursors.
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
By default database will be in auto-commit mode . So commit statement is not explicitly written.
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
@AnkitaHegde which database are you using to store the data in your code?
There was a problem hiding this comment.
Its postgres. Here its about sql but I thought those auto-commit things are similar in these, aren't they?
There was a problem hiding this comment.
@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).
There was a problem hiding this comment.
I think i am following what you are saying. Could you please brief out about 'with' , I think I went wrong from here.
There was a problem hiding this comment.
@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',"") |
There was a problem hiding this comment.
@AnkitaHegde Why are you replacing it?
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
@AnkitaHegde If any exception occurs in this loop, what happens to the file?
There was a problem hiding this comment.
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.
Assignment is within myworld1.