Databases 9 min read

Cross‑Platform SQLite Development with Free Pascal and CodeTyphon

This tutorial demonstrates how to build a single, reusable SQLite library in Free Pascal using CodeTyphon, covering platform‑specific APIs, export conventions for Android, iOS, PC, and detailed compilation and usage examples across Android, iOS, macOS, Linux, and Windows.

Hujiang Technology
Hujiang Technology
Hujiang Technology
Cross‑Platform SQLite Development with Free Pascal and CodeTyphon

The article explains why a unified SQLite solution is needed when different platforms (Android, iOS, PC) expose distinct SDKs and APIs, and introduces CodeTyphon as a convenient Free Pascal environment that already includes the sqlite3conn unit.

It starts by creating a simple test database with the sqlite3 command‑line tool and defines a Pascal record TDemoRec that mirrors an equivalent C++ struct.

A Pascal class TSQLite is then presented, encapsulating TSQLite3Connection , TSQLQuery , and TSQLTransaction objects, together with methods ExecuteSQL (with optional error output) and Select for running queries.

Utility functions dbGetSelectResultCount and dbGetSelectResult are provided to retrieve the number of rows and a specific record, respectively, handling string conversion and memory allocation.

The article details how to expose these functions for each platform: standard cdecl declarations for iOS/PC, and JNI‑compatible stdcall signatures for Android, emphasizing the need for the correct calling convention.

Compilation instructions use the Free Pascal cross‑compiler ( fpc64 ) to produce libsample.dylib (macOS), libsample.so (Linux), and Android .so libraries, followed by steps to create static .a libraries for iOS and combine them with lipo for universal binaries.

Sample usage code is shown for Android (Java native interface), iOS (Objective‑C extern declarations), and PC (dynamic loading with dlopen and dlsym ), illustrating how the same API can be called uniformly across all targets.

Finally, a complete iOS example demonstrates copying the database, opening it, executing a query, iterating over results, and closing the database, confirming that the cross‑platform library works consistently.

The source code is open‑source and available at https://github.com/rarnu/cross_sqlite .

cross-platformiOSAndroiddatabaseSQLiteJNICodeTyphonFree Pascal
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.