{"id":20833855,"url":"https://github.com/denissimon/sqliteadapter","last_synced_at":"2026-01-19T14:32:05.942Z","repository":{"id":217608131,"uuid":"744330285","full_name":"denissimon/SQLiteAdapter","owner":"denissimon","description":"A simple wrapper around SQLite3.","archived":false,"fork":false,"pushed_at":"2024-08-01T10:36:13.000Z","size":105,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T22:38:34.796Z","etag":null,"topics":["adapter","carthage","cocoapods","ios","macos","sqlite","sqlite-wrapper","sqlitedb","swift","swift-library","swift-package-manager","xcode"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/denissimon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-17T04:28:36.000Z","updated_at":"2024-08-11T09:13:36.000Z","dependencies_parsed_at":"2024-03-06T06:42:39.701Z","dependency_job_id":"1e83e1b0-31e2-4583-af7a-7fd069e19782","html_url":"https://github.com/denissimon/SQLiteAdapter","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"6850e4fa958cf24338f905e9db014a00f19f37b0"},"previous_names":["denissimon/sqliteadapter"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denissimon%2FSQLiteAdapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denissimon%2FSQLiteAdapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denissimon%2FSQLiteAdapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/denissimon%2FSQLiteAdapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/denissimon","download_url":"https://codeload.github.com/denissimon/SQLiteAdapter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198400,"owners_count":20900078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adapter","carthage","cocoapods","ios","macos","sqlite","sqlite-wrapper","sqlitedb","swift","swift-library","swift-package-manager","xcode"],"created_at":"2024-11-18T00:17:03.407Z","updated_at":"2026-01-19T14:32:05.926Z","avatar_url":"https://github.com/denissimon.png","language":"Swift","readme":"# SQLiteAdapter\n\n[![Swift](https://img.shields.io/badge/Swift-6-orange.svg?style=flat)](https://swift.org)\n[![Platform](https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20watchOS%20%7C%20tvOS%20%7C%20Linux-lightgrey.svg)](https://developer.apple.com/swift/)\n\nA simple wrapper around SQLite3.\n\nInstallation\n------------\n\n#### Swift Package Manager\n\nTo install SQLiteAdapter using [Swift Package Manager](https://swift.org/package-manager), add the following in your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/denissimon/SQLiteAdapter.git\", from: \"0.8.1\")\n]\n```\n\nOr through Xcode:\n\n```txt\nFile -\u003e Add Package Dependencies\nEnter Package URL: https://github.com/denissimon/SQLiteAdapter\n```\n\n#### CocoaPods\n\nTo install SQLiteAdapter using [CocoaPods](https://cocoapods.org), add this line to your `Podfile`:\n\n```ruby\npod 'SQLiteAdapter', '~\u003e 0.8'\n```\n\n#### Carthage\n\nTo install SQLiteAdapter using [Carthage](https://github.com/Carthage/Carthage), add this line to your `Cartfile`:\n\n```ruby\ngithub \"denissimon/SQLiteAdapter\"\n```\n\n#### Manually\n\nCopy folder `SQLiteAdapter` into your project.\n\nUsage\n-----\n\n### Open the database\n\n```swift\nimport SQLiteAdapter\n\n// The sqlite file will be created if it does not already exist\nguard let dbPath = try? (FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)\n    .appendingPathComponent(\"db.sqlite\")).path else { return }\n\nguard let sqlite = try? SQLite(path: dbPath) else { return }\n\nprint(sqlite.dbPath) // -\u003e path of the sqlite file\n```\n\nWhen initializing `SQLite` with `recreate: true`, the sqlite file will be deleted and recreated.\n\n### Model and create a table\n\n```swift\nlet sqlTable = SQLTable(\n    name: \"ExampleTable\",\n    columns: [\n        (\"id\", .INT),\n        (\"json\", .TEXT),\n        (\"isDeleted\", .BOOL),\n        (\"updated\", .DATE)\n    ],\n    primaryKey: \"id\" // \"id\" by default\n)\n\nlet statementCreateTable = \"\"\"\n    CREATE TABLE IF NOT EXISTS \"\\(sqlTable.name)\"(\n        \"\\(sqlTable.primaryKey)\" INTEGER NOT NULL,\n        \"json\" TEXT NULL,\n        \"isDeleted\" BOOLEAN DEFAULT 0 NOT NULL CHECK (isDeleted IN (0, 1)),\n        \"updated\" DATETIME NOT NULL,\n        PRIMARY KEY(\"\\(sqlTable.primaryKey)\" AUTOINCREMENT)\n    );\n    \"\"\"\n\ntry? sqlite.createTable(sql: statementCreateTable)\n```\n\n### SQL operations\n\nSome examples of SQL operations:\n\n```swift\ndo {\n    var sql = \"INSERT INTO \\(sqlTable.name) (json, updated) VALUES (?, ?);\"\n    try sqlite.insertRow(sql: sql, params: [\"someJson\", Date()])\n    \n    sql = \"INSERT INTO \\(sqlTable.name) (json, updated) VALUES (?, ?), (?, ?), (?, ?);\"\n    let date = Date()\n    let (changes, lastInsertID) = try sqlite.insertRow(sql: sql, params: [nil, date, nil, date, nil, date])\n    assert((changes, lastInsertID) == (3, 4))\n    \n    sql = \"UPDATE \\(sqlTable.name) SET isDeleted = ?, updated = ? WHERE \\(sqlTable.primaryKey) IN (2, 3)\"\n    try sqlite.updateRow(sql: sql, params: [true, Date()])\n    assert(sqlite.changes == 2)\n    \n    sql = \"SELECT * FROM \\(sqlTable.name) WHERE isDeleted = ?\"\n    if let rows = try sqlite.getRow(from: sqlTable, sql: sql, params: [true]) {\n        assert(rows.count == 2)\n    }\n    \n    try sqlite.deleteByID(in: sqlTable, id: 2)\n    try sqlite.deleteByID(in: sqlTable, id: 3)\n    \n    let rowCount = try sqlite.getRowCount(in: sqlTable)\n    assert(rowCount == 2)\n    \n    if let row = try sqlite.getFirstRow(from: sqlTable) {\n        assert(row[0].value as! Int == 1) // \"id\"\n        assert(row[1].value as? String == \"someJson\") // \"json\"\n        assert(row[2].value as! Bool == false) // \"isDeleted\"\n    }\n    \n    if let row = try sqlite.getLastRow(from: sqlTable) {\n        assert(row[0].value as! Int == 4) // \"id\"\n        assert(row[1].value as? String == nil) // \"json\"\n        assert(row[2].value as! Bool == false) // \"isDeleted\"\n    }\n} catch {\n    print(error.localizedDescription)\n}\n```\n\nRead methods return `nil` if no rows have been read:\n\n```swift\nlet row = try sqlite.getByID(from: sqlTable, id: 10) // -\u003e nil\n```\n\nInsert, update, and delete methods return the number of changes made:\n\n```swift\nlet changes = try sqlite.deleteAllRows(in: sqlTable) // -\u003e 2\n```\n\n### Optional settings\n\n```swift\nsqlite.dateFormatter.locale = Locale(identifier: \"en_US_POSIX\")\nsqlite.dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)\nsqlite.dateFormatter.dateFormat = \"yyyy-MM-dd HH:mm:ss\"\n```\n\nMore usage examples can be found in [tests](https://github.com/denissimon/SQLiteAdapter/blob/main/Tests/SQLiteAdapterTests/SQLiteAdapterTests.swift) and [iOS-MVVM-Clean-Architecture](https://github.com/denissimon/iOS-MVVM-Clean-Architecture) where this adapter was used.\n\nSupported SQLite types\n----------------------\n\n```swift\nINT // Includes INT, INTEGER, INT2, INT8, BIGINT, MEDIUMINT, SMALLINT, TINYINT\nBOOL // Includes BOOL, BOOLEAN, BIT\nTEXT // Includes TEXT, CHAR, CHARACTER, VARCHAR, CLOB, VARIANT, VARYING_CHARACTER, NATIONAL_VARYING_CHARACTER, NATIVE_CHARACTER, NCHAR, NVARCHAR\nREAL // Includes REAL, DOUBLE, FLOAT, NUMERIC, DECIMAL, DOUBLE_PRECISION\nBLOB // Includes BLOB, BINARY, VARBINARY\nDATE // Includes DATE, DATETIME, TIME, TIMESTAMP\n```\n\nPublic methods \n--------------\n\n```swift\nfunc createTable(sql: String) throws\nfunc checkIfTableExists(_ table: SQLTable) throws -\u003e Bool\nfunc dropTable(_ table: SQLTable, vacuum: Bool) throws\nfunc addIndex(to table: SQLTable, forColumn columnName: String, unique: Bool, order: SQLOrder) throws\nfunc checkIfIndexExists(in table: SQLTable, indexName: String) throws -\u003e Bool\nfunc dropIndex(in table: SQLTable, forColumn columnName: String) throws\nfunc beginTransaction() throws\nfunc endTransaction() throws\nfunc insertRow(sql: String, params: [Any]?) throws -\u003e (changes: Int, lastInsertID: Int)\nfunc updateRow(sql: String, params: [Any]?) throws -\u003e Int\nfunc deleteRow(sql: String, params: [Any]?) throws -\u003e Int\nfunc deleteByID(in table: SQLTable, id: Int) throws -\u003e Int\nfunc deleteAllRows(in table: SQLTable, vacuum: Bool, resetAutoincrement: Bool) throws -\u003e Int\nfunc getRowCount(in table: SQLTable) throws -\u003e Int\nfunc getRowCountWithCondition(sql: String, params: [Any]?) throws -\u003e Int\nfunc getRow(from table: SQLTable, sql: String, params: [Any]?) throws -\u003e [SQLValues]?\nfunc getAllRows(from table: SQLTable) throws -\u003e [SQLValues]?\nfunc getByID(from table: SQLTable, id: Int) throws -\u003e SQLValues?\nfunc getFirstRow(from table: SQLTable) throws -\u003e SQLValues?\nfunc getLastRow(from table: SQLTable) throws -\u003e SQLValues?\nfunc vacuum() throws\nfunc query(sql: String, params: [Any]?) throws -\u003e Int\n```\n\nLicense\n-------\n\nLicensed under the [MIT license](https://github.com/denissimon/SQLiteAdapter/blob/main/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenissimon%2Fsqliteadapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenissimon%2Fsqliteadapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenissimon%2Fsqliteadapter/lists"}