Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am working on the Leave management system. Suppose Empid=00001, and he had taken leave for 8 days from 30-05-2019 to 06-06-2019 and these data saved in a database table. Now again if the same Empid=0001, try to apply leave between 30-05-2019 to 06-06-2019 again, suppose he is trying to apply one day leave 03-06-2019 to 03-06-2019 which lies between above date. So it should check first that for this employee record there is a leave from 30-05-2019 to 06-06-2019 already in the database. So it shouldn't allow the employee to apply for any other leave on this date duration.

LeavefromDate -- table column
StartDate-- a variable that holds current leave start date.
EndDate--a variable that holds current leave End date.

My database is in sqlite3.

Looking for the perfect SQLite query to work

What I have tried:

Select * from empleave where Empcode ='" + txtLeaveempcode.Text + "' and IsUploaded='0' and (date(substr(LeavefromDate, 7, 4) || '-' || substr(LeavefromDate, 4, 2) || '-' || substr(LeavefromDate, 1, 2) ) between '" + StartDate + "' and '" + EndDate + "') OR (date(substr(LeavefromDate, 7, 4) || '-' || substr(LeavefromDate, 4, 2) || '-' || substr(LeavefromDate, 1, 2) ) between '" + StartDate + "' and '" + EndDate + "');
Posted
Updated 20-May-19 20:01pm

1 solution

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And then there is your DB design ... oh dear. Part of why your query is so ugly and difficult to work out is that you are storing everything as strings. That's bad - very bad - and you need to fix that throughout your app as well. Always store data in the appropriate formats: numbers go in INT, DECIMAL, or FLOAT columns, Dates go in DATE, DATETIME, or DATETIME2 for example. If you don;t you make your job a whole load more complex as you have already seen with this task.

When you've fixed that through your whole app, you can start looking at how to fix the problem you have noticed - but there is no point in trying until you have!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900