SQL-only Programming and Templating Techniques #207
matthewlarkin
started this conversation in
Show and tell
Replies: 1 comment 2 replies
-
Thank you very much for these useful tips. When working with sqlite and wanting more advanced features another useful thing to know is that SQLPage supports sqlite extensions. There are many useful extensions; I like this list: https://github.com/nalgeon/sqlean |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
SQL for Programming and Templating
While using SQLPage, I now find myself asking "how can I do this in SQL?" for use cases I might have typically reached for PHP or Python for.
So I've come across some techniques for using SQL (in these exapmles, SQLite) for tasks typically associated with the above mentioned programming languages, with focus on recursive looping and data templating.
Here are some simple examples that illustrate some core concepts. I wonder if you have any to share? 🙂
🌿 - - 🌿 - - 🌿
⭐️ Creating Sequences with Recursive CTEs
Creating a simple number sequence can also be achieved using Recursive CTEs in SQLite.
SQL Query for Number Sequence
Number Sequence Output
🌿 - - 🌿 - - 🌿
⭐️ Generating a Fibonacci Sequence
Generating a Fibonacci sequence is a classic example of a task involving loops in programming. SQLite can achieve this using a Recursive Common Table Expression (CTE).
SQL Query for Fibonacci Sequence
Fibonacci Sequence Output
Note: You'll notice at row 94, we start using exponential notation to represent the numbers, and at row 1478, we get
Inf
for infinity due to the precision limits of 8-byte floating point numbers.🌿 - - 🌿 - - 🌿
⭐️ Managing Hierarchical Data
Managing hierarchical data, such as organizing blog posts into categories and subcategories, is a complex task that SQLite can handle effectively. This showcases how SQL can be used for templating (very typically done with languages like PHP, JavaScript, or Python).
SQL Setup for Blog Posts and Categories
We create tables for blog posts and categories, with the latter including a self-referencing foreign key for hierarchy, and insert some sample records.
Querying Hierarchical Data
Now, we can use a recursive CTE to retrieve the blog posts along with their categories and subcategories, resulting in a breadcrumb-like path.
Hierarchical Data Output
The recursive query above produces a list of blog posts with their category paths 🙌
🌿 - - 🌿 - - 🌿
⭐️ Generating Calendar Dates
Generating a calendar with dates and weekdays can be a useful task for scheduling applications or reports. SQLite, through its recursive CTEs, can be employed to create a date series along with their corresponding weekdays.
SQL Query for Calendar Generation
Calendar Output
This query generates a list of dates for a month, along with their respective day of the week, -- a handy tool for calendar-related operations.
🌿 - - 🌿 - - 🌿
So, SQL's capabilities can extend beyond simple data storage and retrieval. As demonstrated, it can handle more complex tasks like generating sequences and managing hierarchical data.
I find this exciting, and I'm looking forward to exploring these concepts further with SQLPage! If you have other examples or want to build upon these, I'd love to see what techniques you use!
Beta Was this translation helpful? Give feedback.
All reactions