Search code examples
pythonsqlpython-2.7rdbmssql-parser

Stimulate SQL like query tool in python/C++


Implement SQL like query tool where from STDIN following things have to be read in the given order:-

1. Number of rows of the table (n) and Number of queries (q).
2. Table fields separated by Comma.
3. n line containing table rows of the table
4. q queries where clause.

We have to print output for each query on STDOUT.

Eg:-

Input:

4,2

"Name","Age","Sex","Salary"

1,"Joy",21,"M",2000

2,"Alan",28,"M",500

3,"John",30,"M",1000

4,"Nemo",45,"F",1500

Salary>=1000

Sex="M" and Salary>499 

Output:

2

3

Can you guys tell how should I approach the problem? And What data structure should I be using to store the table and process the queries ?

PS: I am not asking for the ready made solution, I just need step wise help in solving this question.


Solution

  • I'm guessing the excersise is impelenting SQL like for practice (and not real SQL becuase this is the obvious solution).

    I'd have a list of dictionaries (a dict for each row) fill it with the data and than use list comprehension to filter the lines

    [ line for line in lines if (line['Sex']=='M') and (line['Salary'] > 499)]
    

    Of course you have a lot of parsing to do to create such a python command - but this is the direction