{"id":14064289,"url":"https://github.com/jdhitsolutions/MySQLite","last_synced_at":"2025-07-29T17:33:25.880Z","repository":{"id":47405475,"uuid":"174228676","full_name":"jdhitsolutions/MySQLite","owner":"jdhitsolutions","description":"A small set of PowerShell commands for working with SQLite database files.","archived":false,"fork":false,"pushed_at":"2024-08-22T13:51:55.000Z","size":5344,"stargazers_count":44,"open_issues_count":5,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-01T09:11:59.202Z","etag":null,"topics":["databases","powershell","sqlite"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","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/jdhitsolutions.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"License.txt","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":"2019-03-06T22:08:11.000Z","updated_at":"2024-11-15T23:22:49.000Z","dependencies_parsed_at":"2024-10-27T11:13:45.001Z","dependency_job_id":"879fd4e6-2b9d-4900-a985-1bd6b33d1728","html_url":"https://github.com/jdhitsolutions/MySQLite","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FMySQLite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FMySQLite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FMySQLite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdhitsolutions%2FMySQLite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdhitsolutions","download_url":"https://codeload.github.com/jdhitsolutions/MySQLite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228032927,"owners_count":17858918,"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":["databases","powershell","sqlite"],"created_at":"2024-08-13T07:03:47.766Z","updated_at":"2024-12-04T02:31:38.546Z","avatar_url":"https://github.com/jdhitsolutions.png","language":"PowerShell","readme":"# MySQLite\n\n[![PSGallery Version](https://img.shields.io/powershellgallery/v/MySQLite.png?style=for-the-badge\u0026label=PowerShell%20Gallery)](https://www.powershellgallery.com/packages/MySQLite/) [![PSGallery Downloads](https://img.shields.io/powershellgallery/dt/MySQLite.png?style=for-the-badge\u0026label=Downloads)](https://www.powershellgallery.com/packages/MySQLite/)\n\nA set of PowerShell functions for working with SQLite database files. The goal of the module is to integrate the use of SQLite databases into daily PowerShell work or module development where a lightweight database would be beneficial. You might use this module as a library in your PowerShell projects.\n\n## Background\n\nI started work on this module years ago and had it almost complete except for managing the assembly to provide the .NET interface. After letting the project remain idle, I happened across a [similar module by Tobias Weltner](https://github.com/TobiasPSP/ReallySimpleDatabase). He had a brilliant technique to manage the .NET assembly which I freely \"borrowed.\" With this missing piece, I dusted off my module, polished it, and published it to the PowerShell Gallery.\n\n## Installation\n\nThis module should work on 64-bit versions of Windows PowerShell 5.1 and PowerShell 7 running on Windows or Linux platforms. __The module is not supported on MacOS.__\n\n\u003e :raised_hand: I would love to be able to support MacOS on PowerShell 7. Please see [Issue #21](https://github.com/jdhitsolutions/mysqlite/issues/21)\n\nYou can install this module from the PowerShell Gallery.\n\n```powershell\nInstall-Module -name MySQLite -repository PSGallery\n```\n\n## Commands\n\n- [ConvertTo-MySQLiteDB](docs/ConvertTo-MySQLiteDB.md)\n- [ConvertFrom-MySQLiteDB](docs/ConvertFrom-MySQLiteDB.md)\n- [Convert-MySQLiteByteArray](docs/Convert-MySQLiteByteArray.md)\n- [Get-MySQLiteDB](docs/Get-MySQLiteDB.md)\n- [Get-MySQLiteTable](docs/Get-MySQLiteTable.md)\n- [Invoke-MySQLiteQuery](docs/Invoke-MySQLiteQuery.md)\n- [New-MySQLiteDB](docs/New-MySQLiteDB.md)\n- [New-MySQLiteDBTable](docs/New-MySQLiteDBTable.md)\n- [Export-MySQLiteDB](docs/Export-MySQLiteDB.md)\n- [Import-MySQLiteDB](docs/Import-MySQLiteDB.md)\n- [Get-SQLiteVersion](docs/Get-SQLiteVersion.md)\n\n## Converting PowerShell Output\n\nThe primary benefit of this module is storing the results of a PowerShell expression or script into a SQLite database file and later retrieving it back into PowerShell as the original objects, or as close as possible.\n\nFor example, you might have code like this that creates a dataset.\n\n```powershell\n$computers= \"win10\",\"dom1\",\"srv1\",\"srv2\",\"thinkx1-jh\"\n\n$data = Get-CimInstance Win32_OperatingSystem -ComputerName $computers |\nSelect-Object @{Name=\"Computername\";Expression={$_.CSName}},\n@{Name=\"OS\";Expression = {$_.caption}},InstallDate,Version,\n@{Name=\"IsServer\";Expression={ If ($_.caption -match \"server\") {$True} else {$False}}}\n```\n\nUsing `ConvertTo-MySQLiteDB` you can easily dump this into a database file.\n\n```powershell\n$data | ConvertTo-MySQLiteDB -Path c:\\work\\Inventory.db -TableName OS -TypeName myOS -force\n```\n\nRun [Get-MySqLiteDB](docs/Get-MySQLiteDB.md) to view the database file.\n\n```powershell\nPS C:\\\u003e  Get-MySQLiteDB -Path C:\\work\\Inventory.db | Format-List\n\nDatabaseName  : main\nTables        : {Metadata, propertymap_myos, OS}\nPageSize      : 4096\nPageCount     : 6\nEncoding      : UTF-8\nFileName      : Inventory.db\nPath          : C:\\work\\Inventory.db\nSize          : 24576\nCreated       : 1/14/2021 1:26:24 PM\nModified      : 2/21/2023 3:31:27 PM\nAge           : 219.19:08:29.4914134\nSQLiteVersion : 3.42.0\n```\n\nOr drill down to get table details.\n\n```powershell\nPS C:\\\u003e Get-MySQLiteTable -Path C:\\work\\Inventory.db -Detail\n\n   Database: C:\\work\\Inventory.db Table:Metadata\n\nColumnIndex ColumnName   ColumnType\n----------- ----------   ----------\n0           Author       TEXT\n1           Created      TEXT\n2           Computername TEXT\n3           Comment      TEXT\n\n   Database: C:\\work\\Inventory.db Table:propertymap_myos\n\nColumnIndex ColumnName   ColumnType\n----------- ----------   ----------\n0           Computername Text\n1           OS           Text\n2           InstallDate  Text\n3           Version      Text\n4           IsServer     Text\n\n   Database: C:\\work\\Inventory.db Table:OS\n\nColumnIndex ColumnName   ColumnType\n----------- ----------   ----------\n0           Computername Text\n1           OS           Text\n2           InstallDate  Text\n3           Version      Text\n4           IsServer     Int\n```\n\nAs you can see, the database file will include a table called `propertymap_myOS` which contains a mapping of properties to types.\n\n```powershell\nPS C:\\\u003e Invoke-MySQLiteQuery -Path C:\\work\\Inventory.db -query \"Select * from propertymap_myos\" -as Hashtable\n\nName                           Value\n----                           -----\nComputername                   System.String\nOS                             System.String\nInstallDate                    System.DateTime\nVersion                        System.String\nIsServer                       System.Boolean\n```\n\nYou can then query the data.\n\n```powershell\nPS C:\\\u003e Invoke-MySQLiteQuery \"Select * from os where IsServer = 1\" -path C:\\work\\Inventory.db\n\nComputername : DOM1\nOS           : Microsoft Windows Server 2019 Standard Evaluation\nInstallDate  : 5/24/2022 3:07:58 PM\nVersion      : 10.0.17763\nIsServer     : 1\n\nComputername : SRV2\nOS           : Microsoft Windows Server 2016 Standard Evaluation\nInstallDate  : 5/24/2022 3:16:44 PM\nVersion      : 10.0.14393\nIsServer     : 1\n\nComputername : SRV1\nOS           : Microsoft Windows Server 2016 Standard Evaluation\nInstallDate  : 5/24/2022 3:16:51 PM\nVersion      : 10.0.14393\nIsServer     : 1\n```\n\nOr dump it back out to PowerShell in its original format.\n\n```powershell\nPS C:\\\u003e ConvertFrom-MySQLiteDB -Path C:\\work\\Inventory.db -TableName OS -PropertyTable propertymap_myos\n\nComputername : DOM1\nOS           : Microsoft Windows Server 2019 Standard Evaluation\nInstallDate  : 5/24/2022 3:07:58 PM\nVersion      : 10.0.17763\nIsServer     : True\n\nComputername : THINKX1-JH\nOS           : Microsoft Windows 11 Pro\nInstallDate  : 5/17/2022 2:54:52 PM\nVersion      : 10.0.22622\nIsServer     : False\n...\n```\n\nYou also use `Invoke-MySQLiteQuery`.\n\n```powershell\nPS C:\\\u003e Invoke-MySQLiteQuery -path D:\\temp\\sales2.db -Query \"Select name,sid,SamAccountName,members from grp\"\n\nName  SID                  SamAccountName Members\n----  ---                  -------------- -------\nSales {60, 79, 98, 106...} Sales          {60, 79, 98, 106...}\n```\n\nNested objects will be stored as byte arrays. You can restore these properties on a granular basis using `Convert-MySQLiteByteArray`.\n\n```powershell\nPS C:\\\u003e Invoke-MySQLiteQuery -path D:\\temp\\sales2.db -Query \"Select name,sid,samaccountname,members from grp\" | Select-Object Name,SamAccountName,\n@{Name=\"SID\";Expression={Convert-MySQLiteByteArray $_.sid}},@{Name=\"Members\";Expression={Convert-MySQLiteByteArray $_.Members}}\n\nName  SamAccountName SID                                          Members\n----  -------------- ---                                          -------\nSales Sales          S-1-5-21-3554402041-35902484-4286231435-1147 {CN=SamanthaS,OU=Sales,DC=Company,DC=Pri, CN=Sonya...\n```\n\n\u003e :warning: Storing objects in a database requires serializing nested objects. This is accomplished by converting objects to cliXML and storing that information as an array of bytes in the database. To convert back, the data must be converted to the original clixml string, deserialized, and then re-imported. This process is not guaranteed to be 100% error free. The converted object property should be the deserialized version of the original property.\n\nThe remaining commands can be used to create SQLite files on a more granular basis.\n\n## Malformed Queries\n\nSQLite can be unforgiving when it comes to queries that might involve odd characters, especially quotes. The module commands will not attempt to validate your queries or syntax. It is up to you to handle that process. Here's an example.\n\nA command like this will fail.\n\n```powershell\n$Name= \"Carol'sPC\"\nInvoke-MySQLiteQuery -Path C:\\temp\\inventory.db -Query \"Insert Into OS (Computername,OS,InstallDate,Version,IsServer) values ('$Name','Microsoft Windows 11 Pro','$(Get-Date)','11.0.0','0')\"\n```\n\nSQLite doesn't like the single quote in the computer name. You need to escape it.\n\n```powershell\n$Name=  $name.Replace(\"'\",\"''\")\n```\n\nNow the query will work as expected. It is assumed that you know the data you want to work with and know how to handle potential issues.\n\n## Creating a New Database\n\nYou can create a new database file with the `New-MySQLiteDB` command.\n\n```powershell\nNew-MySQLiteDB -Path c:\\work\\data.db -Comment \"work data and reporting\"\n```\n\nWhen you create a new database with this command, PowerShell will retain a lock on the file for a few minutes until the garbage collector releases it. If you need to work with the new database immediately ***outside*** of PowerShell, you will need to wait a few minutes, or manually invoke garbage collection by running:\n\n```powershell\n[System.GC]::Collect()\n```\n\nOnce created you can view the database.\n\n```powershell\nPS C:\\\u003e Get-MySQLiteDB -Path c:\\work\\data.db\n\nPath            FileName Size Modified             Tables\n----            -------- ---- --------             ------\nC:\\work\\data.db data.db  8192 2/22/2024 9:41:50 AM Metadata\n```\n\nYou can add tables to the database.\n\n```powershell\nPS C:\\\u003e New-MySQLiteDBTable -Path C:\\work\\data.db -TableName ComputerNames -ColumnNames \"Name\",\"Date\",\"Version\"\nPS C:\\\u003e Invoke-MySQLiteQuery c:\\work\\data.db -query \"Pragma table_info(ComputerNames)\" | Select cid,name\n\ncid name\n--- ----\n  0 Name\n  1 Date\n  2 Version\n\nPS C:\\\u003e Get-MySQLiteTable -Path C:\\work\\data.db -Detail\n\n   Database: C:\\work\\data.db Table:Metadata\n\nColumnIndex ColumnName   ColumnType\n----------- ----------   ----------\n0           Author       TEXT\n1           Created      TEXT\n2           Computername TEXT\n3           Comment      TEXT\n\n   Database: C:\\work\\data.db Table:ComputerNames\n\nColumnIndex ColumnName ColumnType\n----------- ---------- ----------\n0           Name\n1           Date\n2           Version\n```\n\nAnd then add data to the table.\n\n```powershell\nPS C:\\\u003e $q = \"Insert into ComputerNames Values ('$env:computername','$(Get-Date)','$($PSVersionTable.OS)')\"\nPS C:\\\u003e Invoke-MySQLiteQuery -Path c:\\work\\data.db -Query $q\nPS C:\\\u003e Invoke-MySQLiteQuery -Path c:\\work\\data.db -Query \"Select * from ComputerNames\"\n\nName     Date                Version\n----     ----                -------\nPROSPERO 02/22/2024 09:59:55 Microsoft Windows 10.0.22631\n```\n\n## Sample Databases\n\nI have provided several sample database files in the Samples folder.\n\n## Learn More\n\nIf you want to learn more about SQLite databases, take a look at \u003chttps://www.sqlite.org/index.html\u003e and \u003chttp://www.sqlitetutorial.net/\u003e.\n\n## Related Modules\n\nIf you would like to see how this module can be used in other tools, take a look at my [PSWorkItem](https://github.com/jdhitsolutions/PSWorkItem) and [PSReminderLite](https://github.com/jdhitsolutions/PSReminderLite) modules. If you have written something that incorporates this module, I'd love to hear about it. Post a desciption and link the [Discussions section of this repository](https://github.com/jdhitsolutions/MySQLite/discussions).\n","funding_links":[],"categories":["PowerShell"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdhitsolutions%2FMySQLite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdhitsolutions%2FMySQLite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdhitsolutions%2FMySQLite/lists"}