SQLite Markdown: Format Query Results In Markdown

If you’re an SQLite user, exporting data from your queries directly to Markdown might be useful. This article shows you how to export SQLite queries to Markdown format. The good news is that you can do this with built-in SQLite features!

Using SQLite’s Markdown mode

By far the easiest method is to change the SQLite output mode to markdown. To enable SQLite’s Markdown output mode, simply enter the command .mode markdown in your SQLite browser/command prompt. Because the output looks so clear, I actually do this all the time, even when I don’t want to export to Markdown. Here’s an example:

sqlite> .mode markdown
sqlite> select * from customers limit 2;
| name | age |
|------|-----|
| Erik | 40  |
| Mary | 53  |

You can copy/paste this to a Markdown file as-is. In this example, I’ve used the simple table schema as defined in this article.

Using HTML mode

A good alternative is to change the SQLite output mode to HTML. As you may know, Markdown tables can also be written in HTML, and sometimes, this is preferred because it gives you more options.

To enable HTML output mode, all you need to do is enter the .mode html command in your SQLite browser/command prompt. Here’s an example of this at work:

sqlite> .mode html
sqlite> select * from customers limit 2;
<TR><TH>name</TH>
<TH>age</TH>
</TR>
<TR><TD>Erik</TD>
<TD>40</TD>
</TR>
<TR><TD>Mary</TD>
<TD>53</TD>
</TR>

As you can see, you’ll get an almost complete HTML table. All you need to add are the HTML open and closing tags.

Conclusion

You’ve learned two output modes that you can use to export SQLite tables to either Markdown or to HTML. Depending on your needs, both output formats can be used in your Markdown file.