jdhitsolutions / MySQLite

A small set of PowerShell commands for working with SQLite database files.
MIT License
41 stars 4 forks source link

Failing on inserting text containing ' #24

Open martincaddick opened 3 months ago

martincaddick commented 3 months ago

I've got text similar to this in a json file that I am importing where it contains the single quotes.

"Item": "Restrict user ability to access groups features in My Groups. Group and User Admin will have read-only access when the value of this setting is 'Yes'.",

Adding to the database I get this error

WARNING: SQL logic error WARNING: Insert into database Values ('Self Service Group Management', '', '', '"General"', 'configuration\entra-id\groups\general.md', '10', 'https://entra.microsoft.com/#view/Microsoft_AAD_IAM/GroupsManagementMenuBlade/~/General', 'Restrict user ability to access groups features in My Groups. Group and User Admin will have read-only access when the value of this setting is 'e[7mYese[0;93m'.', 'No')

jdhitsolutions commented 3 months ago

I run into problems with quotes often. That is a SQL issue more than anything. I would hesitate to modify the query. But I could try to perform some type of validation check and present a warning.

martincaddick commented 3 months ago

Dodgy workaround Jeff, if it works it's ok isn't it ;-)?

$jsonData | ForEach-Object -Begin { $cx = Open-MySQLiteDB $dbpath } -Process { try { $item = $.Item -replace "'", "''" $value = $.Value -replace "'", "''" $query = "INSERT INTO asd VALUES ( '$($.H3)', '$($.H4)', '$($.H5)', '$($.Title)', '$($.Filename)', '$($.Weight)', '$($.Url)', '$item', '$value' )" Invoke-MySQLiteQuery -Connection $cx -KeepAlive -Query $query } catch { Write-Host "Error: $" } } -End { Close-MySQLiteDB $cx }