0

I make an employee record program in python. In this program, user can make search name, duty or education and can see for example all names contains 'Jennifer'. Employee informations holds on database therefore i am using some sql queries. For example, if u want to add new employee to database, my program run this query :

def insert(self, name, duty, date, education):
    self.cur.execute("INSERT INTO worker VALUES (NULL,?,?,?,?)", (name, duty, date, education))
    self.conn.commit()
    self.view()

In the search function, name duty, date or education takes as a parameter. If user click the name and write 'jennifer', user should see employees who name or surname contain Jennifer. However, I can't do that. I write this sql query and it works:

SELECT * FROM worker WHERE name LIKE '%Jennifer%'

but i cant replace Jennifer part with the name for input. I tried like this but it is not working(dont return anything to me):

def search(self, name="", duty="", date="", education=""):
    self.cur.execute("SELECT * FROM worker WHERE name LIKE('%' || name=? || '%')", (name,))
    rows = self.cur.fetchall()
    return rows

(I am trying only name in this part but i want to make search query for all features)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    Hint: `self.cur.execute("SELECT * FROM worker WHERE name LIKE CONCAT('%', ?, '%')", (name))` – Zhorov Mar 25 '20 at 11:33
  • self.cur.execute("SELECT * FROM worker WHERE name LIKE CONCAT('%', 'name=?', '%')", (name,)) I tried like this but i take this error : "no such function: CONCAT" – Oguzhan Kırlar Mar 25 '20 at 11:38
  • What is your database engine. SQL Server supports `CONCAT()` frunction from version 2012. Or try with `self.cur.execute("SELECT * FROM worker WHERE name LIKE '%' + ? + '%'"` – Zhorov Mar 25 '20 at 11:43
  • Omg you are right. I tried my worker file different database engine since yesterday. Not sql, SQLite is my database engine – Oguzhan Kırlar Mar 25 '20 at 11:50
  • @Zhorov now its working when i tried that : `self.cur.execute("SELECT * FROM worker WHERE name LIKE '%' || ? || '%'"` . Thank you so much. I want to ask one more question. How can i search according to their duty ? – Oguzhan Kırlar Mar 25 '20 at 11:59
  • `self.cur.execute("SELECT * FROM worker WHERE LIKE('%' || ? || '%')", (name, duty, date, education)) ` i change like that but i know it is using only one of them. Therefore i have too much argument – Oguzhan Kırlar Mar 25 '20 at 12:01
  • May be this - `self.cur.execute("SELECT * FROM worker WHERE (name LIKE('%' || ? || '%')) OR (duty LIKE('%' || ? || '%'))", (name, duty)) ` will help as a basic example for two-column search. – Zhorov Mar 25 '20 at 12:07
  • It is returning all rows but no problem. I can handle with the rest of them (i hopee) Thank you so much again. – Oguzhan Kırlar Mar 25 '20 at 12:14

0 Answers0