{"id":21184564,"url":"https://github.com/skreweverything/sqlight","last_synced_at":"2026-05-07T21:38:41.803Z","repository":{"id":95354634,"uuid":"96651108","full_name":"SkrewEverything/SQLight","owner":"SkrewEverything","description":"A simple Swift layer over SQLite3.","archived":false,"fork":false,"pushed_at":"2017-10-17T23:10:56.000Z","size":151,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-21T14:47:34.306Z","etag":null,"topics":["ios","macos","sqlite3","sqlite3-database","swift"],"latest_commit_sha":null,"homepage":null,"language":"Objective-C","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/SkrewEverything.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}},"created_at":"2017-07-09T00:11:29.000Z","updated_at":"2021-08-30T03:03:21.000Z","dependencies_parsed_at":"2023-08-29T13:49:25.652Z","dependency_job_id":null,"html_url":"https://github.com/SkrewEverything/SQLight","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkrewEverything%2FSQLight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkrewEverything%2FSQLight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkrewEverything%2FSQLight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SkrewEverything%2FSQLight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SkrewEverything","download_url":"https://codeload.github.com/SkrewEverything/SQLight/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243654399,"owners_count":20325892,"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":["ios","macos","sqlite3","sqlite3-database","swift"],"created_at":"2024-11-20T18:09:21.883Z","updated_at":"2026-05-07T21:38:41.773Z","avatar_url":"https://github.com/SkrewEverything.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLight\n\n\nSQLight is a simple Swift layer over SQLite3.\n\nWorking with SQLite3 in Swift is usually a headache due to the data types used in C. \n\nSQLight handles all the headaches for you so that you can peacefully use it with Swift. \n\n### How is it different from other swift implementations?\n\nhmmm...\n\nMost of the famous GitHub projects on sqlite3-swift are too complex to begin for swift beginners.\n\nAnd also it takes time to learn the workings of the framework which *I think* it is a *@#$@ing* waste of time.\n\n..\n\n....\n\n......\n\nand **its fun to create when you are capable!**\n\n## Usage\n\n#### Steps to follow:\n\n1) Open database connection.\n2) Prepare a statement using SQL query.\n3) Do operations on prepared statement.\n4) Destroy the prepared statement.\n5) Close the database connection.\n\n#### Code:\n\n```swift\ndo\n{\n    var rowsChanged: Int\n    \n    /**************************************/\n    // Open a connection to database\n    let db = try SQLight(type: .file(\"test.db\"))\n    /**************************************/\n    \n    \n    /**************************************/\n    // create a table\n    let createQuery = \"create table testing(num int, name varchar);\"\n    let createPS = try PreparedStatement(SQLQuery: createQuery, SQLightDB: db)\n    \n    // executes the query\n    rowsChanged = try createPS.modify()\n    \n    //  always destroy the query after its use\n    try createPS.destroy()\n    /**************************************/\n    \n    \n    /**************************************/\n    // insert values using already constructed string\n    let insertQuery = \"insert into testing values(12,'anything');\"\n    let insertPS = try PreparedStatement(SQLQuery: insertQuery, SQLightDB: db)\n    \n    // executes the query\n    rowsChanged = try insertPS.modify()\n    \n    //  always destroy the query after its use\n    try insertPS.destroy()\n    /**************************************/\n    \n    \n    /**************************************/\n    // Preparing statement is costly, so preparing every insert statement is not recommened and prone to sql injections\n    // For performance and security, parameter binding is recommened.\n    let bindQuery = \"insert into testing values(@num, @name);\"\n    let bindPS = try PreparedStatement(SQLQuery: bindQuery, SQLightDB: db)\n    \n    // Binding can be done in 3 ways\n    for i in 0...5 // -\u003e 1st\n    {\n        try bindPS.bindValues([i,\"jon\"])  // takes [Any]. Binds values from left to right\n        rowsChanged = try bindPS.modify() // execute\n        bindPS.resetBindValues()          // removes the binded values for next use\n    }\n    \n    for i in 0...5 // -\u003e 2nd\n    {\n        try bindPS.bindValues([1:i,2:\"john\"]) // takes [Int:Any]. key -\u003e parameter index, value -\u003e value to bind\n        rowsChanged = try bindPS.modify()     // execute\n        bindPS.resetBindValues()              // removes the binded values for next use\n    }\n    \n    for i in 0...5 // -\u003e 3rd\n    {\n        try bindPS.bindValues([\"@num\":i,\"@name\":\"nothing\"]) // takes [String:Any]. key -\u003e parameter name, value -\u003e value to bind\n        rowsChanged = try bindPS.modify()                   // execute\n        bindPS.resetBindValues()                            // removes the binded values for next use\n    }\n    \n    // You can get the number of bind parameters\n    print(\"Number of bind parameters: \",bindPS.getParameterCount())\n    \n    // You can get the name of the parameters by passing index\n    print(\"Parameter name of index 1: \",bindPS.getParameterName(parameterIndex: 1)!)\n    print(\"Parameter name of index 2: \",bindPS.getParameterName(parameterIndex: 2)!)\n    \n    // You can get the index of a parameter by passing its name\n    print(\"Parameter index of name \\\"@num:\\\" \",bindPS.getParameterIndex(parameterName: \"@num\"))\n    print(\"Parameter index of name \\\"@name:\\\" \",bindPS.getParameterIndex(parameterName: \"@name\"))\n    \n    //  always destroy the query after its use\n    try bindPS.destroy()\n    /**************************************/\n    \n    \n    /**************************************/\n    let selectQuery = \"select *from testing;\"\n    let selectPS = try PreparedStatement(SQLQuery: selectQuery, SQLightDB: db)\n    \n    // If the data is small enough to store in RAM\n    let result = try selectPS.fetchAllRows() // returns [[Any]]\n    for i in result\n    {\n        for j in i\n        {\n            print(j, terminator: \" \")\n        }\n        print(\"\")\n    }\n    \n    // If the data is large, retrieve 1 row at a time\n    while true\n    {\n        if let row = try selectPS.fetchNextRow() // returns [Any]?\n        {\n            print(row)\n        }\n        else\n        {\n            break\n        }\n    }\n    \n    // Or use a callback. The closure is called for every row. But parameter bindings is not supported\n    try selectPS.execute(callbackForSelectQuery: {\n        void, columnCount, values, columns in\n        \n        for i in 0 ..\u003c Int(columnCount)\n        {\n            guard let value = values?[i]\n                else { continue }\n            guard let column = columns?[i]\n                else { continue }\n            \n            let strCol = String(cString:column) // column name as string\n            let strVal = String(cString:value)  // value as string\n            print(strVal, terminator: \" \")\n        }\n        print(\"\")\n        \n        return 0\n    })\n    \n    // Get columns used in select query\n    if let columnNames = selectPS.getColumnNames() // returns [String]?\n    {\n        print(\"Column Names: \", columnNames)\n    }\n    \n    //  always destroy the query after its use\n    try selectPS.destroy()\n    /**************************************/\n    \n    \n    /**************************************/\n    // If you want to execute multiple queries where parameter binding is not required.\n    // Multiple queries as String\n    let customQuery = \"create table test(num int);insert into test values(10);insert into test values(11);insert into test values(12);insert into test values(12);\"\n    let customPS = try PreparedStatement(SQLQuery: customQuery, SQLightDB: db)\n    \n    // The callback is only required if there is any select query\n    try customPS.execute(callbackForSelectQuery: nil)\n    \n    //  always destroy the query after its use\n    try customPS.destroy()\n    /**************************************/\n    \n    \n    /**************************************/\n    // close the database connection\n    try db.close()\n    /**************************************/\n}\ncatch let error as DBError\n{\n    print(\"Error message: \",error.message, \"\\nError code: \",error.errorCode)\n}\n\n```\n\u003eNote: Currently it doesn't support `blob` data type.\n\n\n## Installation\n\n#### For macOS:\n1) Including Source files.\n2) Including Framework.\n\n##### Including Source Files:\n\n1) Copy `SQLight.swift`, `PreparedStatement.swift`, `CustomTypes.swift` to your project.\n2) Create a Bridging Header and add line `#include\u003csqlite3.h\u003e` in it.\n3) Add `libsqlite3.tbd` framework to your project under **Linked Frameworks and Libraries**.\n\n##### Including Framework:\n\n1) Open the SQLight Xcode project and build the project for framework.\n2) Add the framework to your project.\n\n#### For iOS:\n\n##### Including Source Files: (same as macOS)\n\n1) Copy `SQLight.swift`, `PreparedStatement.swift`, `CustomTypes.swift` to your project.\n2) Create a Bridging Header and add line `#include\u003csqlite3.h\u003e` in it.\n3) Add `libsqlite3.tbd` framework to your project under **Linked Frameworks and Libraries**.\n\n\n## Contributing\n\nFeel free to fork the project and submit a pull request with your changes!\n\n##### Not experienced or lazy to fork and submit a pull request ?\nOpen an issue for adding new features, enhancement, bugs etc.\n \nLicense\n----\n\nMIT\n\n\n**Free Software, Hell Yeah!**\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskreweverything%2Fsqlight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskreweverything%2Fsqlight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskreweverything%2Fsqlight/lists"}