Getting an update column's new value in a row in rethinkdb - rethinkdb

I am trying to get and update a column in a row in rethinkdb using a similar statement.
User.get_table() \
.get(self.id) \
.update(return_changes = True,
{'reward_amount' :r.row['reward_amount'].default(0)+amount}) \
.run(User.get_conn())
I am getting new value by passing return_changes=True, but is it possible to write a anonymous function that just returns new value as a int instead of array of old and new objects (which is what return_changes=True does)

You can write update(...)['changes'][0]['new_val']['reward_amount'].

Related

Prepared statements with TBS

I'm trying to merge the results of a prepared statement to TBS. Here's my code:
$s = $link->prepare("SELECT * FROM newsletters WHERE newsletter_id = :newsletter_id");
$s->bindParam(":newsletter_id",$newsletter_id);
$s->execute();
$newsletter = $s->fetch(PDO::FETCH_ASSOC);
$tbs->MergeBlock('$newsletter ', $newsletter );
But I can't get the results fields. I get errors like the following:
TinyButStrong Error in field [newsletter.title...]: item 'title' is not an existing key in the array.
I can't find my error.
MergeBlock() is for merging a recordset , so you should use $s->fetchAll() instead of $s->fetch(). The section of the template will be repeated for each record.
But if you have to merge a standalone record, use MergeField() instead of MergeBlock(). The single fields will be merged one by one without repeating.

Linq to entity update query not updating the table

Its just a simple Linq to Entity update query i tried with the following code but it doesn't update the "User" column in the DB.
Its not even throwing any exception also,please some one point me what am missing here.
MyEntities db = new MyEntities ();
var query = from SEVTs in db.SEVTs
where SESID == "4747747"
select SEVTs;
foreach (var SEVTs in query) {
SEVTs.USER = "Test";
}
db.SaveChanges();
Quite interesting, when i try the follwing query in the sql server its not update the record
update Schedwin.SEVT
set
USER3='Test'
Where
SESID='4747747' // here i pass the value as a string
SESID data type is CHAR and its a primary key. if i pass the value as SESID=4747747 then it update that record.
Please ignore my question.
Here what i missed my input value SESID == "4747747" //this have whitespace that's why it didn't updated that particular record.
Thanks All
You are nor modifying SEVTs.USER, but some local variable.
(BTW: create a context in a using construct)

How to remove records from a dataset with LINQ

I have a dataset (called dataSet below) with a single table and some records in it. One of the columns is called Message and contains an error message. If any records have a value in this field I want to copy it into an error dataset (errorDataSet below) and then remove it from the original dataset. I managed to get this far with LINQ:
DataSet errorDataSet = dataSet.Copy();
//find all records that have a Message column value
var query = from row in errorDataSet.Tables[0].AsEnumerable()
where !String.IsNullOrEmpty(row.Field<string>("Message"))
select row;
DataSet tempErrorDataSet = errorDataSet.Clone();
foreach (var row in query)
{
tempErrorDataSet.Tables[0].Clear();
tempErrorDataSet.Tables[0].ImportRow(row);
utility.WriteError(connectorName, row["Message"].ToString(), tempErrorDataSet);
//remove the error row from the good data
dataSet.Tables[0].Rows.Remove(row);
}
The bottom line throws an exception or I get errors regarding modifying a collection etc. I'm sure there is a simple way of doing this in LINQ.
Note: The reason I have tempErrorDataSet is that I convert it to XML and pass it into a stored proc - it only takes a record at a time in that format, hence I clear it each time.
Your query is enumerating (indirectly) through the rows in the table. As you should already know, you cannot modify a collection (remove something in this case) as you enumerate over it. Throw the contents into a list beforehand. That way you're not enumerating through the actual table but a copy of some rows.
var query = (from row in errorDataSet.Tables[0].AsEnumerable()
where !String.IsNullOrEmpty(row.Field<string>("Message"))
select row).ToList();

LINQ to DataSet and xml help

I created a strongly-typed dataset in the dataset designer. The DataSet has a Table called FocusOffsetsTable and that table has four colums; SerialNumber, Filter, Wheel and Offset. I use the ReadXml() method of the DataSet class to load the strongly typed data from the xml file into the dataset. That seems to be working just fine.
I am trying to use a LINQ expression to try to get a Single row from this table but I can't seem to get the syntax correct. I want to use the Single() or SingleOrDefault() method to get just one row of data at a time but I am not sure how.
I have tried this FocusOffsets.FocusOffsetsTableRow x = FocusOffsetData.FocusOffsetsTable. but the Single() method is not available here. I also tried this...
FocusOffsets.FocusOffsetsTableRow x = (from offset in FocusOffsetData.FocusOffsetsTable
where offset.SerialNumber == mydevice.SerialNumber
where offset.Wheel == WheelID
where offset.Filter == FilterNum
select offset).Single();
but the Single method is not available here either.
I have done this before with tables in a SQL database before but this is my first time using a dataset from the dataset designer.
Have you added a using statement for System.Linq and included a reference to System.Data.DataSetExtensions. I think (but can't confirm since I'm on my Mac), that you ought to be able to do:
var x = FocusOffsetData.FocusOffsetsTable
.AsEnumerable()
.SingleOrDefault( o => o.SerialNumber == mydevice.SerialNumber
&& o.Wheel = WheelID
&& o.Filter = FilterNum );

How to delete all records in a table using SubSonic 3

I'm trying to delete all the records from a table using this approach:
new Delete<Contact>().Execute();
This statement fails with a NullReferenceException in BuildDeleteStatement method at line:
sb.Append(query.FromTables[0].QualifiedName);
Because, although FromTables has one entry, it is set to null. I also tried this but it doesn't worked either:
var provider = ProviderFactory.GetProvider("MonitorData");
new Delete<Contact>(provider).Execute();
What am I doing wrong?
You can do this with the repo DeleteMany method:
SubSonicRepository<Contact> repo = new SubSonicRepository<Contact>(new YourDB());
repo.DeleteMany(contact => true);
The lambda I'm using is just to ensure all records are selected.

Resources