Modify Or Update Live Database In Python And Django
- Shivam Rohilla
- May 22, 2022
- 3 comments
- 793 Views
<meta name="clckd" content="6301244fb7ed470eb1cd6b8c31d01ac4" />
Hello guys, in this post I'm gonna tell you about how to update the live database in Python and Django, you'll see in this blog how to connect with the live database and update or modify that. we update and convert all the emails into lowercase. We'll use LOWER(column) function for converting all the emails into the lowercase.
pip install psycopg2
Psycopg2 is used to implement a connection pool.
Make a connection and enter your database name and credentials or host and password.
conn = psycopg2.connect( database = "dindin", user="postgres", host="localhost", port="5432", password="Shivam123", )
cur = conn.cursor()then execute a update command
cur.execute("UPDATE auth_user SET email = LOWER(email) WHERE email != LOWER(email);")
after then commit this
then use the select command and select all the users from your table name
cur.execute("SELECT * from auth_user") rows = cur.fetchall()
and then print all the user details you want to print or want to know that your emails are converted successfully or not. Use these print commands, and please write their indexes carefully otherwise it shows errors.
for row in rows: print("ID :", row ) print("username :", row ) print("email :", row ) print("/n")
and print this command for checking your program compiled successfully or not.
then close the connection.
and name this file as update.py and run this program in your terminal
after running this command you will see the complete message in your terminal.
Thank You | Shivam Rohilla
My social media links