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

Support variables in the place of table names in standard SQL queries #28

Open
ghost opened this issue Mar 7, 2018 · 4 comments
Open

Comments

@ghost
Copy link

ghost commented Mar 7, 2018

I'm trying to export some data to text files:

Declare $TablesCount = (select count(name) from sqlite_master where type='table')

Declare $TableName
Declare $FileName

For $counter = 0 To $TablesCount-1
Begin

    Set $TableName = (Select name From sqlite_master Where type='table' Limit 1 Offset $Counter)
    Set $FileName = printf(D:\Data\Clientlists\SW - %s.txt', $TableName)
    
    Print printf('Exporting names from %s to %s', $TableName, $FileName)
    EXPORT TEXT $FileName FROM ( SELECT Name FROM $TableName )

End

Some hints to get it working? Is there a better way to do so?

@electroly
Copy link
Member

Hi Churanos, I believe the issue here is that there's no printf function, but you can concatenate strings together using the || operator. For instance:

SET $FileName = 'D:\Data\Clientlists\SW - ' || $TableName || '.txt';
PRINT 'Exporting names from ' || $TableName || ' to ' || $FileName;

Let me know if that works for you.

In the future I hope to add some kind of string formatting function like printf but it isn't available currently. I also hope to add some kind of FOR EACH structure to make it easier to iterate over rows like you're doing here.

@electroly
Copy link
Member

Ah, I didn't read far enough into the code. I see that the SQLite engine doesn't like using a variable as the table name in SELECT Name FROM $TableName. Hmm. I think this is something I could add support for, but I don't think there's a way to accomplish it in the current version.

@electroly electroly changed the title EXPORT TEXT Select statement with variables Support variables in the place of table names in standard SQL queries Mar 7, 2018
@ghost
Copy link
Author

ghost commented Mar 7, 2018

The printf function is a core function of sqlite, no need to reimplement something like it.
For a For Each statement it would be great if variables can handle table data, not only single statement values.
Might it be a good idea to serialize ARRAY values and, if possible, something like Maps as simple json data?

For the moment i can use temp tables to overcome with the variables in Select-Statements thing.

@electroly
Copy link
Member

I see, thanks for the info! I can pre-process function arguments before they hit SQLite, so it would be possible to convert an array blob into a readable string, etc. I'm open to suggestion on how you might want the API for this to look.

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

No branches or pull requests

1 participant