So, I am creating a python code using SQLite database that is stored in the memory. I first wrote this code with the values of the employees already given. Then I made some changes to the code where the user inputs the values that will be stored in the database, but it seems like I am doing something wrong because no matter what I try it still returns the same mistake.
I tried different things. For instance, I tried instead of using the class I created, creating lists where everything is stored. Still the same error. I'm not exactly sure what I am doing wrong.
import sqlite3
from Employee import Employee
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""CREATE TABLE employees(
first text,
last text,
pay integer
)""")
# here I create a table called 'employees'. It stores the first name, the last name and the payment
def insert_emp(emp): # This is used for inserting employees in the table
with conn:
c.execute("INSERT INTO employees VALUES (:first, :last, :pay)", {'first': emp.first, 'last': emp.last, 'pay': emp.pay})
def get_emp_by_name(lastname): # Searches for all results with the given LAST name
c.execute("SELECT * FROM employees WHERE last = :last", {'last': lastname})
return c.fetchall()
def update_pay(emp, pay): # Updates payment info
with conn:
c.execute("""UPDATE employees SET pay = :pay WHERE first = :first AND last = :last""",
{'first': emp.first, 'last': emp.last, 'pay': pay})
def del_emp(emp): # Deletes employee
with conn:
c.execute("DELETE from employees WHERE first = :first AND last = :last",
{'first': emp.first, 'last': emp.last})
a = input("First name: ")
b = input("Last name: ")
c = int(input("Payment: ")) # Turn the payment into an integer, because input automatically sets the value as a str
emp_1 = Employee(a, b, c) # Here I try to add the values given by the user in my class that I have created.
insert_emp(emp_1)
emps = get_emp_by_name('Doe')
print(emps)
conn.close()
This is what I tried to do, by using the class that I created.
Here is the class:
class Employee:
def __init__(self, first, last, pay):
self.first = first
self.last = last
self.pay = pay
I also wrote the code with already given values.
Here is the end of the code that is with the given values (It works without a problem):
emp_1 = Employee('John', 'Doe', 80000) # Add the employee, using the class that I have created
emp_2 = Employee('Jane', 'Doe', 90000)
insert_emp(emp_1)
insert_emp(emp_2)
emps = get_emp_by_name('Doe')
print(emps)
update_pay(emp_2, 95000)
del_emp(emp_1)
emps = get_emp_by_name('Doe')
print(emps)
conn.close()
If we input, for instance, Gabriel; Doe; 5000;
the result should be:
[('Gabriel', 'Doe', 5000)]
Process finished with exit code 0
But, the result I actually get is:
Traceback (most recent call last):
File *location*, line 56, in <module>
insert_emp(emp_1)
File *location*, line 17, in insert_emp
c.execute("INSERT INTO employees VALUES (:first, :last, :pay)", {'first': emp.first, 'last': emp.last, 'pay': emp.pay})
AttributeError: 'int' object has no attribute 'execute'
cis integer number instead ofcursorso it try to do something like5.execute(..)