Blog

SQLite Implementation

We’ve all been in situations where we needed to store data on a device within a specific application. Not only should the database reside on the machine where the application is running, but it also shouldn’t matter if we have multiple instances connecting to the database. In other words, we only need to provide data to one specific application. For a desktop solution, we could easily setup MSSQL or MySQL; but what if the aforementioned application is running on a mobile device? Similarly, what if we need our database to be portable between devices?

SQLite solves all of these requirements. SQLite is a light-weight and self-contained SQL database engine that enables us to put a database on just about any type of device. Also, since SQLite uses a flat-file for the actual database, there is no worry of a complicated setup. Typically, a SQLite installation consists of a flat-file, the SQLite library (in a format such as a .dll file), and the actual application that will be using the database; That’s it!

Perhaps now you’re thinking, “That’s all good, but how complicated is SQLite to code for?” The answer to that question is, “Not complicated at all!” To demonstrate the simplicity of implementing SQLite, consider the following C# code snippet:

string connectionString = "@"Data Source="C:exampledb.sqlite3"
 SQLiteConnection db = new SQLiteConnection(connectionString);

db.Open();

SQLiteCommand query = db.CreateCommand();
 query.CommandText = "SELECT * FROM tblExampleTable";

DataTable dtExampleTable = new DataTable();
 SQLiteDataReader dr;
 dr = query.ExecuteReader(CommandBehavior.CloseConnection);
 dtExampleTable.Load(drSqliteReader);

Now, let’s discuss what this code actually does. In the first line, we’re instantiating a new instance of SQLiteConnection. The argument passed into the constructor provides the location of the flat-file that we will be using for the database. In the case of this example, I used the extension .sqlite3 for my flat-file. Because of the way the file is parsed, the actual extension that you use is irrelevant. Nonetheless, providing a meaningful extension (such as .sqlite3) can give a good indication to others of what the file is as well as the version of SQLite being used.

From here, we call the Open() method on our newly-created connection object. This method simply tells SQLite to go ahead and connect to our database, whose connection string was passed to the SQLiteConnection its constructor.

Next, using our connection object, we create a SQLiteCommand that will be used to provide the engine with our actual query that we will be running. After assigning said query, using query.CommandText, we instantiate an instance of a standard .NET DataTable. After that, we simply execute our query that we just defined and load the results into the DataTable object that we created.

Voila! We now have an object that contains results that we queried straight from a SQLite flat-file. We didn’t have to perform any complicated code (like serialization between .NET and SQLite) to get this going. You would of course want to continue beyond our short code example (e.g. displaying the data in a GridView in ASP.NET), but for the purpose of this blog, that’s all we need.

Related Blogs
See All Blogs
Blog
Jul 19, 2024

Soft Skills for Tech Leads: How They’ll Help You Excel at Your Role

If you manage a team, you already know that this experience can be as challenging as it is rewarding. At Sparq we have different management roles and one of the most essential is the Tech Lead position. In addition to being seasoned developers, Teach Leads need to be able to mentor up and coming talent. One of the ways they do that is by utilizing soft skills. In this article, Delivery Lead Rodrigo Trelles explores the most important soft skills to be a successful Tech Lead.

Read More
Blog
Jul 16, 2024

Exploring the World of a Site Reliability Engineer (SRE)

Technology is advancing rapidly and the new advances introduce new roles and responsibilities. Among the most significant roles is the Site Reliability Engineer (SRE). This article will explore the role of an SRE, how it differs from a software developer and the essential skills required to succeed in the field.

Read More
Blog
Jul 10, 2024

Automated Testing Applied: How This Practice Can Help Developers Across Projects

In this article, Technical Lead Pablo Carballo shares how automated testing practices have made his team's work more efficient and diverse.

Read More
Blog
Jul 2, 2024

Inside the Minds of Digital Innovators: Highlights from OctoTalks Podcast

Creating exceptional digital experiences involves a blend of creativity, strategy and technology. Octobot, a company that was recently acquired by Sparq, hosted a podcast that delves into these elements by showcasing the visionaries who dream up and build these experiences. In this blog post, we want to share some highlights about the conversations and invite you to give it a listen!

Read More
See All Blogs
noun-arrow-2025160 copy 2
noun-arrow-2025160 copy 2
See All Blogs