If I have the following document for example:
{
“make”:“BMW”,
“make1”:“AUDI”
}
When I do an UPDATE query UPDATE Translations SET make2 = “MERCEDES” WHERE META().id = “CARS” , there is no problem with the query and make2 is added to the document.
When I do an UPDATE query UPDATE Translations SET make2.MODEL = “CLS”, make2.MODIFICATION = “500” WHERE META().id = "CARS"
The query returns “sucess”, but nothing is added. If make2 already exists everything is updated as expected, the problem only appears if the object does not exist.
You can only update the nested properties if the parent property is a JSON Object. If the parent property is set to a string, the update will not succeed, contrary to what you stated.
This will work:
UPDATE Translations USE KEYS "CARS"
SET make2 = {};
UPDATE Translations USE KEYS "CARS"
SET make2.MODEL = "CLS", d.make7.MODIFICATION = "500";
Or you can do this in one line:
UPDATE Translations USE KEYS "CARS"
SET make2 = {}, make2.MODEL = "CLS", d.make7.MODIFICATION = "500";
Related
I'm new to LUA and I still haven't gotten the hang of how classes work in LUA, so my question
probably has a very simple answer. I'm trying to make a function that takes a CSV file and turns it into a lua table.
The input file would be something like this
PropertyKey1,Propertykey2,Propertykey3
object1property1,object1property2,object1property3
object2property1,object2property2,object2property3
object3property1,object3property2,object3property3
and I want the resulting lua table to look something like this
objects = {
{
PropertyKey1 = object1property1
PropertyKey2 = object1property2
PropertyKey3 = object1property3
}
{
PropertyKey1 = object2property1
PropertyKey2 = object2property2
PropertyKey3 = object2property3
}
{
PropertyKey1 = object3property1
PropertyKey2 = object3property2
PropertyKey3 = object3property3
}
}
this is what I have thus far
function loadcsv(path)
local OutTable = {}
local file = io.open(path, "r")
local linecount = 0
for line in file:lines() do
local data = {}
local headers = {}
local headerkey = 1
if linecount < 1 then
for val in line:gmatch("([^,]+),?") do
table.insert(headers, val)
end
else
for word in line:gmatch("([^,]+),?") do
key = headers[headerkey]
data[headerkey] = word
headerkey = headerkey + 1
table.insert(OutTable, data)
end
end
linecount = linecount + 1
end
file:close()
return OutTable
end
The above code does not run. When I try to print any of the values, they come as nil.
The problem is this bit
key = headers[headerkey]
data[headerkey] = word
I wanted to use the values I stored in one table as keys on the second table, but it looks like since LUA only passes the references, that doesn't work.
I did a quick experiment to confirm it. I first set up 2 tables.
test = {}
test2 = {}
test[1]={"index"}
key = test[1]
key2 = "index"
First I tried assigning the value directly form the table
test2[test[1]] = "text"
print(test2.index) --This did not work
then I tried going trough another variable
test2[key] = "texto"
print(test2.index) --This did not work
I even tried using tostring()
key = tostring(test[1])
test2[key] = "texto"
print(test2.index) --This did not work
I wrote the string directly in the variable "key2" to confirm that I was using the right notation.
test2[key2] = "text"
print(test2.index) --This one worked
I read a bit on metatables, but I'm not fully clear on those. Would that be the simplest way to do what I'm trying to do, or is my approach flawed in some other way?
key = headers[headerkey]
key is not used so why assign a value to it?
data[headerkey] = word
headerkey is a numeric key. You start at 1 for each line and add 1 for each word in a line. So you end up with
data = {
[1] = "object1property1",
[2] = "object1property2",
[3] = "object1property3"
}
Instead of the intended
data = {
PropertyKey1 = "object1property1",
PropertyKey2 = "object1property2",
PropertyKey3 = "object1property3"
}
So you probably meant to write
local key = headers[headerkey]
data[key] = word
But you have to move headers out of the loop. Otherwise you'll end up with an empty table for line 1 resulting in key being nil which would cause Lua errors for using a nil table index.
The following line is called for every word
table.insert(OutTable, data)
You need to do this for every line!
Your code basically produces this output:
local tableA = {"object1property1", "object1property2", "object1property3"}
local tableB = {"object2property1", "object2property2", "object2property3"}
local tableC = {"object3property1", "object3property2", "object3property3"}
OutTable = {
tableA, tableA, tableA, tableB, tableB, tableB, tableC, tableC, tableC
}
I suggest you formulate your program in your first language and then translate it into Lua. This helps to avoid such errors.
Your problem is not related to metatables, classes or anything else mentioned. You simply used the wrong variable and messed up your inner loop.
I'm having a json format stored in the database like this
{"fooKey": "fooValue", "barKey": "barValue"}
I'm trying to run an UPDATE query using JSON_SET in order to add some additional stuff, so
UPDATE `table`
SET `dbcol` = JSON_SET(`dbcol`, '$.newitem', 'newvalue')
WHERE `id` = 12;
The problem is I want to add some nested elements so my final result should look like this
{"fooKey":"fooValue","barKey":"barValue", "newItem":"newValue", "anotherNewItem":{"subItemKey":"subItemValue", "anotherSubItemKey":"anotherSubItemValue"}}
How can I do this?
For the specific example you have given, you can nest two JSON_SET calls, and use JSON_OBJECT to create the data to be inserted for key anothernewitem:
UPDATE table
SET dbcol = JSON_SET(JSON_SET(dbcol, '$.newitem', 'newvalue'),
'$.anothernewitem',
JSON_OBJECT('subitemkey', 'subitemvalue', 'anothersubitemkey', 'anothersubitemvalue'))
WHERE id = 12;
Demo on dbfiddle
I have a object like this
{ emId: '1', emEmail: 'sreepurna#xxx.com' }
I am using nodejs at backend and mysql for database. I want to update table only when my value is not equal to specific value. Is there any possibility to write this within a query.
I tried the few sql statements but it doesnt work:
let sql = `UPDATE details SET name = CASE WHEN '${req.body.emName}' != 'hi' THEN ${req.body.emName} END where id=${req.body.emId}`;
There is not such a thing called undefined inside the SQL language, you have to validate your values via JavaScript
if(req.body.emName){
// This code block will run when req.body.emName contains value and is not undefined or null
let sql = `UPDATE details SET name = CASE WHEN '${req.body.emName}' THEN ${req.body.emName} END where id=${req.body.emId}`;
}else{
// req.body.emName is either empty/undefined or null
// ...
}
I'm updating a set of objects, but the update fails on a SqlException that says "Incorrect Syntax near 'Where'".
So I crack open SqlProfiler, and here is the generated SQL:
exec sp_executesql N'UPDATE [dbo].[Addresses]
SET
WHERE ([AddressID] = #p0) AND ([StreetAddress] = #p1) AND ([StreetAddress2] = #p2) AND ([City] = #p3) AND ([State] = #p4) AND ([ZipCode] = #p5) AND ([CoordinateID] = #p6) AND ([CoordinateSourceID] IS NULL) AND ([CreatedDate] = #p7) AND ([Country] = #p8) AND (NOT ([IsDeleted] = 1)) AND (NOT ([IsNonSACOGZip] = 1))',N'#p0 uniqueidentifier,#p1 varchar(15),#p2 varchar(8000),#p3 varchar(10),#p4 varchar(2),#p5 varchar(5),#p6 uniqueidentifier,#p7 datetime,#p8 varchar(2)',#p0='92550F32-D921-4B71-9622-6F1EC6123FB1',#p1='125 Main Street',#p2='',#p3='Sacramento',#p4='CA',#p5='95864',#p6='725E7939-AEE3-4EF9-A033-7507579B69DF',#p7='2010-06-15 14:07:51.0100000',#p8='US'
Sure enough, no set statement.
I also called context.GetChangeSet() and the proper values are in the updates section.
Also, I checked the .dbml file and all of the properties Update Check values are 'Always'.
I am completely baffled on this one, any help out there?
I had overriden GetHashCode to return a concatenation of a few of the fields. When I changed it to return solely the hash of the primary key, it worked.
The root cause is that the updates will fail for an object whose hash code changes during its lifecycle, so when you override GetHashCode you need to pick attributes that cannot be updated, like the primary key,
I have to add new columns in existing table. I can able to successfully add new column, but following exception occur while tying to modify the column attribute to nullable.
Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done
Here my code,
HRESULT hr = S_OK;
ADOX::_CatalogPtr pCatalog = NULL;
ADOX::_TablePtr pTable = NULL;
ADOX::TablesPtr pTables = NULL;
hr = pCatalog.CreateInstance(__uuidof(Catalog));
pCatalog->PutActiveConnection("Provider='Microsoft.JET.OLEDB.4.0';data source='C:\\sample.mdb';");
pTables = pCatalog->GetTables();
pTable = pTables->Item["sampletable"];
hr = pTable->Columns->Append( "age", ADOX::adInteger, 0);
ASSERT(hr == S_OK);
pTable->Columns->Item["age"]->Attributes = ADOX::adColNullable;
The equivalent code in VBA works for me without error (assuming I have translated it faithfully).
Something perhaps to try is to create a Column object, set its properties including NULLable then append it to the Table object's Columns collection e.g. this in VBA:
Set oColumn = New ADOX.Column
oColumn.Name = "age"
oColumn.Type = ADOX.adInteger
oColumn.Attributes = ADOX.adColNullable
oTable.Columns.Append oColumn