Getting Started with gsqlcmd

Getting Started with gsqlcmd

gsqlcmd is a command-line tool for executing SQL scripts, downloading, converting, importing, and exporting data.

For example, to import AAPL dividends from www.nasdaq.com into a database, you can use the following commands:

gsqlcmd download https://www.nasdaq.com/symbol/AAPL/dividend-history dividends.htm

gsqlcmd convert "SELECT 'AAPL' AS Symbol, RecordDate, CashAmount FROM dividends.htm" dividends.csv

gsqlcmd import db dividends.csv /table=dividends /merge

You can also accomplish this in a single import command:

gsqlcmd import db "SELECT 'AAPL' AS Symbol, RecordDate, CashAmount FROM https://www.nasdaq.com/symbol/AAPL/dividend-history" /table=dividends /merge

gsqlcmd makes it easy to automate operations using task files. For instance, you can import dividends for all symbols listed in a symbols.txt file with this command:

gsqlcmd import db "SELECT :symbol AS Symbol, RecordDate, CashAmount FROM https://www.nasdaq.com/symbol/{symbol}/dividend-history" /table=dividends /merge /taskfile=symbols.txt

In the examples above, gsqlcmd extracts data from an HTML page. It also supports extracting data from plain text, CSV, XML, and JSON.

The examples use the named connection "db," which can connect to databases on SQL Server, Oracle, MySQL, PostgreSQL, SQLite, or any other supported platform.

gsqlcmd can execute SQL scripts for any supported platform and understands platform-specific features and SQL dialects. You can run scripts like this for any platform:

gsqlcmd exec db application-install.sql

You can also use gsqlcmd to convert and export data into various formats, including plain text, CSV, HTML, XML, and JSON. Here are some examples:

gsqlcmd exec db "SELECT * FROM dividends" dividends.txt
gsqlcmd exec db "SELECT * FROM dividends" dividends.csv
gsqlcmd exec db "SELECT * FROM dividends" dividends.htm
gsqlcmd exec db "SELECT * FROM dividends" dividends.xml
gsqlcmd exec db "SELECT * FROM dividends" dividends.json

You can customize the output using various formatting and template options.

As you might expect, use the exec mode to export data and the import mode to import.

The gsqlcmd Enterprise edition offers an even more powerful solution. You can export and import data in a single command:

gsqlcmd db1 import "db2::SELECT * FROM dividends" /table=dividends /merge

Additionally, db1 and db2 can be on different database platforms, such as SQL Server and MySQL, even if their tables have different structures and column names.

We recommend exploring the examples included with the gsqlcmd download package. You'll find many templates to help with your solutions.

Also, check out the following topics:

This website is using cookies. By continuing to browse, you give us your consent to our use of cookies as explained in our Cookie Policy.