Skip to content

Code improvements #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 46 additions & 32 deletions code-snippet.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import psycopg2

mydb = psycopg2.connect(
host="localhost",
user="yourUsername",
password="yourPassword",
database="company"
)

mycursor = mydb.cursor()

mydb.set_session(autocommit=True)

mycursor.execute('''CREATE TABLE employee(
EmployeeID int,
Name varchar(255),
Email varchar(255));
''')

mycursor.execute('''
INSERT INTO employee (EmployeeID, Name, Email)
VALUES (101, 'Mark', '[email protected]'),
(102, 'Robert', '[email protected]'),
(103, 'Spencer', '[email protected]');
''')

mycursor.execute("SELECT * FROM employee")

print(mycursor.fetchall())

mycursor.close()
mydb.close()
import psycopg2

# Creation of the connection class

class Connection:
def __init__(self):
try:
self.connection = psycopg2.connect(
host = "localhost",
user = "yourUsername",
password = "yourPassword",
database = "company"
)
self.cursor = self.connection.cursor()
except Exception as e:
print(f"Error: {e}")

self.cursor.execute("""CREATE TABLE Employee(
EmployeeID int,
name varchar(255),
email varchar(255)
);
""")

# Creation of the method that inserts data

def insertData(self, name, email):
cursor = self.cursor
sql = "INSERT INTO Employee (name, email) VALUES (%s, %s)"
try:
cursor.execute(sql, (name, email))
self.connection.commit()
print("Data inserted successfully")
except Exception as e:
self.connection.rollback()
print(f"Error inserting data: {e}")

# An instance of the connection class is created

connection = Connection()

# Insert data into table

connection.insertData("Mark", "[email protected]")
connection.insertData("Robert", "[email protected]")
connection.insertData("Spencer", "[email protected]")