使用system.data.dataset更新PostgreSQL JSON
我正在尝试使用PowerShell System.Data.DataSet方法在PostgreSQL(V14)中更新JSON字段,但请继续获取错误:
“错误[42883]错误:运算符:运算符不存在:JSON = UNKNOWER; >
我只想将JSON字段读取为字符串,然后更新其中的一部分(我不在乎识别JSON字段,我只想将其处理并将其更新为字符串)
$DBConnectionString = "Driver={PostgreSQL UNICODE(x64)};Server=$Server;Port=$Port;Database=$DB;Uid=$Uid;Pwd=$Pass;"
$Con = New-Object System.Data.Odbc.OdbcConnection;
$Con.ConnectionString = $DBConnectionString;
$Con.Open();
$Query = "Select ID, INFO from TestTable"
$ODBCDataAdapter = New-Object system.Data.odbc.odbcDataAdapter
$DataSet = New-Object System.Data.DataSet
$ODBCCommandSelect = New-Object System.Data.Odbc.OdbcCommand($query,$Con)
$ODBCDataAdapter.SelectCommand = $ODBCCommandSelect #execute Query
#setup the command to execute to fill the in-memory table
$ODBCDataAdapter.SelectCommand = $ODBCCommandSelect #execute Query
$ODBCDataAdapter.Fill($dataSet) #in memory database table created
$Con.Close();
#I now have a $dataset containing the rows from the table
For($i=0; $i -le ($dataset.tables[0].rows.Count -1); $i++){
IF($dataset.Tables[0].rows[$i].Info -match "Josh"){
$dataSet.Tables[0].Rows[$i].Info = $dataSet.Tables[0].Rows[$i].Info -replace "Josh", "Barbara"
}
$ODBCCommandUpdate = New-Object System.Data.Odbc.OdbcCommandBuilder($ODBCDataAdapter)
$ODBCDataAdapter.UpdateCommand = $ODBCCommandUpdate.GetUpdateCommand()
#all working fine until here:
$ODBCDataAdapter.Update($Dataset.tables[0])
$ODBCDataAdapter.Dispose()
$Dataset.Dispose()
$con.Close()
$con.Dispose()
。 NON JSON)。 我尝试了这样的选择语句: $ query =“从测试台上选择ID,cast(info as varchar)” 尽管没有出错,但它仍然没有更新该字段。 这是桌子看起来像
ID INFO(JSON)
1 { "customer": "John Doe", "items": {"product": "Beer","qty": 6}}
2 { "customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}
3 {"customer": "Josh William", "items": {"product": "Toy Car", "qty": 1}}
4 { "customer": "Mary Clark", "items": {"product": "Toy Train","qty": 2}}
帮助我Stackoverflowbi Wan Kenobi,您是我唯一的希望。
I'm trying to update a JSON field in PostgreSQL (v14) using PowerShell system.data.dataset method but keep getting error:
"ERROR [42883] ERROR: operator does not exist: json = unknown;
I just want to read the JSON field as a string then update a part of it (I don't care of identifying JSON fields I just want to treat it and update it as a string).
$DBConnectionString = "Driver={PostgreSQL UNICODE(x64)};Server=$Server;Port=$Port;Database=$DB;Uid=$Uid;Pwd=$Pass;"
$Con = New-Object System.Data.Odbc.OdbcConnection;
$Con.ConnectionString = $DBConnectionString;
$Con.Open();
$Query = "Select ID, INFO from TestTable"
$ODBCDataAdapter = New-Object system.Data.odbc.odbcDataAdapter
$DataSet = New-Object System.Data.DataSet
$ODBCCommandSelect = New-Object System.Data.Odbc.OdbcCommand($query,$Con)
$ODBCDataAdapter.SelectCommand = $ODBCCommandSelect #execute Query
#setup the command to execute to fill the in-memory table
$ODBCDataAdapter.SelectCommand = $ODBCCommandSelect #execute Query
$ODBCDataAdapter.Fill($dataSet) #in memory database table created
$Con.Close();
#I now have a $dataset containing the rows from the table
For($i=0; $i -le ($dataset.tables[0].rows.Count -1); $i++){
IF($dataset.Tables[0].rows[$i].Info -match "Josh"){
$dataSet.Tables[0].Rows[$i].Info = $dataSet.Tables[0].Rows[$i].Info -replace "Josh", "Barbara"
}
$ODBCCommandUpdate = New-Object System.Data.Odbc.OdbcCommandBuilder($ODBCDataAdapter)
$ODBCDataAdapter.UpdateCommand = $ODBCCommandUpdate.GetUpdateCommand()
#all working fine until here:
$ODBCDataAdapter.Update($Dataset.tables[0])
$ODBCDataAdapter.Dispose()
$Dataset.Dispose()
$con.Close()
$con.Dispose()
This works for all the other fields (i.e. non JSON).
I tried a select statement like this:
$Query = "Select ID, CAST(INFO as Varchar) from TestTable"
And although it didn't error out, it still didn't update the field.
here's what the table looks like
ID INFO(JSON)
1 { "customer": "John Doe", "items": {"product": "Beer","qty": 6}}
2 { "customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}
3 {"customer": "Josh William", "items": {"product": "Toy Car", "qty": 1}}
4 { "customer": "Mary Clark", "items": {"product": "Toy Train","qty": 2}}
Help me Stackoverflowbi Wan Kenobi, you're my only hope.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到答案:
我添加了一行,该行改变了CommandBuilder创建更新语句的方式:
$ odbccommandupdate.conflictoption = 3
$现在,构建器更改了更新语句中的Where子句,仅包括主要密钥。
Found the answer:
I added a line that changes the way that CommandBuilder creates the update statement:
$ODBCCommandUpdate.ConflictOption =3
$now the builder changes the Where clause in the update statement to only include the primary key.