Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can queries be exported to CSV? #128

Open
normansimonr opened this issue Feb 20, 2023 · 3 comments
Open

Can queries be exported to CSV? #128

normansimonr opened this issue Feb 20, 2023 · 3 comments

Comments

@normansimonr
Copy link

Hi! I am creating a notebook for teaching SQL and need learners to be able to export the results of their queries to CSV files. I was wondering if that can be easily done from within the notebook with the Xeus-SQLite kernel. Thanks!

@marimeireles
Copy link
Member

It might be... Specially if there's a SQLite command for that.
Or you could try xeus-sql and check if SOCI has a command for that.
If not it's not suuper hard to implement a functionality like this... Someone with some understanding of C++ could do it :)
I'm around to help!

@normansimonr
Copy link
Author

@marimeireles Thanks! Do you mean something like this? Is it possible to use these commands from within the notebook?

@marimeireles
Copy link
Member

Ah no, it seems like I wrote all of these functions by hand as jupyter magic.
I guess it's not too hard to do the same...
We need a library to parse data and convert it to csv, then we just write a file to the user.
This is how load is currently implemented in xeus-sql

    static std::unique_ptr<soci::session> load_db(
            const std::vector<std::string> tokenized_input)
    {
        std::string aux;
        for (std::size_t i = 2; i < tokenized_input.size(); i++)
        {
            aux += tokenized_input[i] + ' ';
        }
        return std::make_unique<soci::session>(
                xv_bindings::to_lower(tokenized_input[1]), aux);
    }

So to implement an output mode I guess we'd do smth similar...
We need a function to grab the data and generate a csv file with it, not sure how to do it, but seems like a task that these robots that generate code could do and then you can just adapt it.
I guess it's in this line: nl::json data = process_SQL_input(code, xv_sql_df); (the data we'd like to convert).
The most complicated thing is how to make the context of which table you're generating clear... I'd have to get deeper into the code and right now I don't have the time, sorry.
But if you or anyone are interested in giving it a try I'm around to answer questions.
Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants