Is it a good practice to separate the handlers and database queries in different classes?
There will be a separate class containing all the necessary event handlers, and there would also be another class for database queries?
Absolutely yes, your code should (almost) always have a separation of concerns:
There are a ton of software design philosophies and patterns behind this, but the Single Responsibility Principle (SRP) is probably a good default to always use:
Rough translation is: "Your class/method/function should do one thing and do it well"
+1 for Emmad Kareems answer, but I would also add that your database queries should be implemented (if possible) as stored procedures in your database becase they are easier to unit test/debug (because you can run them without your code). They make change easier as for some changes you only need to change the procedure, and not your code base. They also run faster because the DB engine will pre compile them and store statistics on them which it probably wont do for queries that are coded into you app.
And yes, the N teir approach works with any language you like - even scripting languages - you just need to find the correct pattern/code/library for the situation.
The answer to your question is Yes. It is a good idea to separate your application into classes, components and layers. A more sophesticated separation can be achieved using a famous design architecture called N-Tier Architecture where an application physically separate its components into separate physical tiers (not only logical layers). It has many advantages that I would recommend you get to know if you haven't already done so. See for example this:
Another famous design pattern that promotes more independence of the GUI (Wikipidia cosniders it an Architecture) is MVC (Model-View-Controler) - See about it here:
A discussion about N-Tier and MVC may also be useful - See this:
Before you use any of this stuff, you need to justify the effort and cost of the learning curve. N-Tier and MVC applications may be classified as "advanced" programming techniques and may be harder for those not familiar with the concepts, so is it worth it?