Ruby MYSQL conditional statement - mysql

I have a ruby script which queries the database, extracts some info and publishes the result. The row in the code snippet below is a record(I'm looping through the rows). I have a flag Cancelled [Which is of datatype bit(1)] in the table.
Irrespective of the flag value, I am always publishing Cancelled = 'No'. Basically the conditional statement is always returning false even though that's not the case. Can someone tell me what I'm doing wrong?
db = Mysql2::Client.new(parameters....)
query_str = "Some query"
row = db.query(query_str).first
Cancelled = 'No'
if row['Cancelled'] == true
Cancelled = 'Yes'
end

Related

Google App Script SQL query returns bool 'True' instead of Int value but query works outside App Script? [duplicate]

When I execute a SQL query from Java and store the boolean returned, the query always returns true which shouldn't be the case at all. So I emptied the table and fired the query again, and yet it returns true for the emptied table. I have attached a picture of the table. I want the query to return true or false, so I can store it in Java. Can someone please specify an alternate code for this, please?
This is my code on java for the query.
boolean avail = st.execute("SELECT EXISTS(SELECT * from sales WHERE product='"+n+"' AND ord_date='"+sqlDate+"');");
And this is my code for result set
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
This is the table, name of the table is 'sales'
I'm new to MySQL, a more specific approach is appreciated.
Statement.execute will return true regardless of what the query returns. You are still supposed to retrieve the actual result of the query.
Returns
true if the first result is a ResultSet object; false if it is an update count or there are no results
As you execute an EXISTS statement, there will always be a result (true or false). The actual value still has to be retrieved:
You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).
For reference: https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#execute-java.lang.String-String
Also note that you are directly embedding strings into your query, this will leave you vulnerable to SQL injections. Please read: How can prepared statements protect from SQL injection attacks?. Recommended reading: Introduction to JDBC
The return value of Statement.execute() signals whether the query produces a result set (true) or - possibly - an update count (false). The query you execute is a select which will always produce a result set (even if empty). In other words, it will always return true for a select.
If you want to get the result of your query, then the recommend approach is to use executeQuery. However, you are also concatenating values into your query string, which is a bad idea because it leave you vulnerable to SQL injection. The recommended approach is to switch to prepared statements:
try (PreparedStatement pstmt = con.prepareStatement(
"SELECT EXISTS(SELECT * from sales WHERE product = ? AND ord_date = ?)")) {
pstmt.setString(1, n);
pstmt.setDate(2, sqlDate);
try (ResultSet rs = st.executeQuery() {
boolean avail = rs.next() && rs.getBoolean(1);
// use avail...
}
}

SSIS Derived Column translated from SSMS query

New to SSIS, been dealing with SSMS mostly. Anyone can help translating the below SSMS statement into SSIS Derived Column Transformation? Many thanks.
ReliabilityFactorInput = Case
When (isnull(pn.LBOXMATL, 'OTHER') = 'OTHER' AND (round(ISNull(edd.cal_year, eqd.YearManuf) + 1, -4)/10000<=2003) OR pn.LBOXMATL ='Cast Iron') AND (ceiling((pn.NOWAYS+1)/2)*2 >= 4) then '1.3'
When (isnull(pn.LBOXMATL, 'OTHER') = 'OTHER' AND (round(ISNull(edd.cal_year, eqd.YearManuf) + 1, -4)/10000<=2003) OR pn.LBOXMATL = 'Cast Iron') AND (ceiling((pn.NOWAYS+1)/2)*2 < 4) then '1.1'
else ''
End
1.Name a variable with whatever name you want with int data type
2.Use execute sql task
3.copy all the complete query into that task and specify the result Set to single row
4.Switch to Result Set page, choose the variable you create, and set the result name to 0
5.Now every time you run the package the variable will be assigned either 1.3 or 1.1
That variable could be used in Derived Column transformation in data flow now

How can I hide specific duplicate rows in SSRS 2005?

Here's my problem: I have some some duplicate rows (rows with the same ID). What I want is to hide rows based on a specific's column value. The mentioned column is obviously not the ID column (it's the Description column).
I tried this on the Row Visibility (Expression):
=IIF (Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value)
AND
ReportItems!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
So, I want to exclude the rows in which Description is not equal to 'Incident Status Change to Work In Progress from Open'.
The message that I get is:
The Hidden expression for the table 'table 1' contains an error:
Object variable or With block variable not set.
Any ideas on this?
Thanks in advance
I think you are probably getting that error because there are one or more rows in your query result that have a NULL value in the Description field.
You could test this theory by simplifying your expression to this:
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value), true, false)
just to see if you can run the report without error.
If that works, then you need to add a test for a NULL value as part of the larger condition test, to get back to the logic you intended.
Try this:
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value) AND IsNothing(Fields!Description.Value) = false AND Fields!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
This should avoid the error by preventing the evaluation of the last part of the condition, when the Description has a NULL value.
I think you're on the right track just using ReportItems instead of Fields!
=IIF(Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value) AND Fields!Description.Value <> "Incident Status Change to Work In Progress from Open", true, false)
SSRS can be wonky sometimes. Try parenthesizing:
=IIF(((Fields!Incident_ID.Value = Previous(Fields!Incident_ID.Value)) AND (IsNothing(Fields!Description.Value) = false) AND (Fields!Description.Value <> "Incident Status Change to Work In Progress from Open")), true, false)

How to increase the user' error times? unknown whether the original value is null

Before operate Access database,in the SQL express,I can just use this statement and it works very well:
UPDATE Person
SET FErrorTimes = IsNull(FErrorTimes, 0) + 1
WHERE (FUserName = #name)
by now, it reports as error syntax
can someone help me please,and thanks very much.
Access' IsNull is different than SQL Server's IsNull. In Access, IsNull accepts only one argument, and returns True or False to indicate whether that argument evaluates as Null.
Use one of these instead.
UPDATE Person
SET FErrorTimes = Nz(FErrorTimes, 0) + 1
WHERE FUserName = #name
UPDATE Person
SET FErrorTimes = IIf(FErrorTimes Is Null, 0, FErrorTimes) + 1
WHERE FUserName = #name
Note Nz() is only available for a query run within an Access session. If you're running a query from external code which connects to the Access db, use the second example.

Alternative way to do FirstOrDefault in LINQ

I've got a membership table that records whether a user is a member of a list. When an update to a user's membership occurs a new record is written and the previous records are left as is which allows a history of their memberships to be maintained. To get a user's membership status involves selecting their most recent entry.
An example of some user list membership data is below. The aim is to find a LINQ expression that groups by list and user but only returns the row with most recently inserted record.
List Name, Username, Comment, ExpiresOn, Inserted
Test List, joeb, second update, 2012-03-13 16:55:03, 2012-01-31 22:28:40
Test List, joeb, first update, 2012-02-13 16:55:01, 2012-01-31 22:28:39
Test List, joeb, initial, 2012-01-13 16:55:02, 2012-01-31 22:28:38
An SQL query illustrates how the current list membership status can be extracted.
select ulm2.ID, ulm2.ExpiresOn, ulm2.Comment, ulm2.Inserted
from UserListMembership as ulm1
left outer join UserListMembership ulm2 on ulm1.id = ulm2.id
group by ulm1.userlistid, ulm1.userid;
The question is how to write a LINQ expression for the query that doesn't use a nested FirstOrDefault call which causes my MySQL entity framework provider to throw a "System.NotSupportedException: Specified method is not supported." exception?
Update:
Below is my failing LINQ expression, it throws the "Specified method is not supported" once the FirstOrDefault call is added.
var query = from mem in db.UserListMemberships
where
mem.User.UserUsernames.Any(y => y.Username.ToLower() == username.ToLower())
&& mem.UserList.Account.Subscriptions.Any(x => x.ID == subscriptionID)
&& mem.ExpiresOn > utcNow
group mem by new { mem.UserListID, mem.UserID } into g
select new { UserListMembership = (from mem2 in db.UserListMemberships where mem2.UserListID == g.Key.UserListID && mem2.UserID == g.Key.UserID orderby mem2.Inserted descending select mem2).FirstOrDefault() };
return query.Select(a => a.UserListMembership).ToList();
Without posting code it's hard for anyone to see what your issue is.
Are you not able to just order the results by your date field and then select the first record? Something like users.OrderByDescending(u => u.Inserted).FirstOrDefault()?