1

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'
1
  • it looks like c is integer number instead of cursor so it try to do something like 5.execute(..) Commented Apr 1, 2019 at 21:50

1 Answer 1

3

You are overwriting your cursor c with

c = int(input("Payment: "))

Don't use global variables, and use cursors as short living objects:

def insert_emp(conn, emp):  # This is used for inserting employees in the table
    with conn:
        cur = conn.cursor()
        cur.execute("INSERT INTO employees VALUES (:first, :last, :pay)", {'first': emp.first, 'last': emp.last, 'pay': emp.pay})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.