Flex SQL Select Max - apache-flex

I am using the following code to get the ID of the latest form application started in my flex app, In the table the column is named id. However the following updates nothing to g.appID. The correct application ID makes it's way to the row Object but not to the g.appID variable. The only property that row appears to have is Max(id) which is the correct value that I need. but when I try g.appID = row.Max(id) I get a error saying Max is not a function, so I cant get either way to work currently. Anyone see what I could be doing wrong? Thanks for any help in advance!
// Get the ID
sqlStatement.text =
"SELECT Max(id) FROM applications";
sqlStatement.execute();
var result:SQLResult;
result = sqlStatement.getResult();
var row:Object = result.data[0];
g.appID = row.id;

Change your SQL to: SELECT Max(id) AS maxID FROM applications This will get you out of doing that wierd subselect your doing. This may also save you some processing depending on how often that query is running.
now change g.appID = row.id; to g.appID = row.maxID; and you should be good to go.

Related

Is it possible to retrieve the row count impacted by Dapper query?

I am trying to insert a record in SQL Server table using Dapper. I am able to insert records using ExecuteScalarAsync but it is not returning the impacted row count which is 1 in this case. I tried Execute, ExecuteScalar and query methods also.
sqlCommand = #"Insert into PAYMENTS (TRANSACTION_NO, MEMBER_ID, PAID_AMOUNT, PAID_DATE) VALUES ( #TransactionNo, #MemberId,#PaidAmount,#PaidDate);
var status = await con.ExecuteScalarAsync<int>(sqlCommand, details);
or
`var status = con.Execute<string>(sqlCommand, details);`
I just want to know if there is a way to retrieve this.
Just in case somebody is looking for this, I could get the count of rows impacted with ExecuteAsync

The multi-part identifier could not be bound error won't fix

The Variable fileName contains the name of two columns to fetch value of it, which is in the type of "email,address"
The output of the fileName variable is sam#gmail.com102streetN, which is what I need
but when I try to insert that value into another column table then it throws me an error
The multi-part identifier "sam#gmail.com102streetN" could not be bound.
I don't know how to fix it
for each a in idParase
strSQL = "Select Award, Year, ("&fileName&") as dynamicColumns FROM dbo.Awards_TABLE Where awardID = "&a&""
cmdCRProc.CommandText = strSQL
Set rsCR = cmdCRProc.Execute
nameParase = rsCR("dynamicColumns")
sql2 = "Insert Into dbo.Queue (Award,Year,awardID,FileName) Values ("&rsCR(0)&", "&rsCR(1)&", "&a&", "&nameParase&") "
cmdCRProc.CommandText = sql2
Set rsCR = cmdCRProc.Execute
next
I think this isn't going wrong in the INSERT, but in the SELECT.
If I write out your SELECT it would be something this I think (I don't know the value of "a", but let's say it's 2):
Select Award, Year, (sam#gmail.com102streetN) as dynamicColumns FROM dbo.Awards_TABLE Where awardID = 2
I very much doubt you have a column named 'sam#gmail.com102streetN', right?
I don't know what your goal is, but it seems like you're building a query with a dynamic column name that doesn't exist.
What you can do is Response.Write() your SQL statements and see what the result is. Then maybe run them in your database management tool to check what's going on:
Response.write(strSQL)
And
Response.write(sql2)

How can I find out if an update cause a change in SQLITE with Xamarin?

I am using this code;
sql = "UPDATE SCORE SET A = 1 WHERE ID = 12 AND COUNT = 25";
db2.Execute(sql);
Is there a way that I can execute this kind of statement and find out if the update actually found and updated a row?
The execute method returns the number of rows modified. So
var rowsModified = db2.Execute(sql);
if(rowsModified > 0)
{
// the statement found at least one row to update
}
Modified means in this case that a row to update was found. So, it is counted as a modification even when the statement results in assigning the same values already present in the row.

EntityFramework using with database foreign key

Actually I spend whole day on the EntityFramework for foreign key.
assume we have two table.
Process(app_id,process_id)
LookupProcessId(process_id, process_description)
you can understand two tables with names, first table ,use process_id to indicate every application, and description is in the seoncd table.
Actually i try many times and figure out how to do inquery: it was like
Dim result = (from x in db.Processes where x.LookupProcess is (from m in db.LookupProcessIds where descr = "example" select m).FirstOrDefault() select x).FirstOrDefault()
First I want to ask is there easier way to do it.
Second i want to ask question is about insert
p As New AmpApplication.CUEngData.Process
p.app_id=100
p.LookupProcess = (from m in db.LookupProcessIds where descr = "example" select m).FirstOrDefault()
db.AddToProcesses(p)
db.SaveChanges()
from appearance it looks fine, but it give me error says
Entities in 'AmpCUEngEntities.Processes' participate in the 'FK_Process_LookupProcess' relationship. 0 related 'LookupProcess' were found. 1 'LookupProcess' is expected.
can i ask is that insert wrong? and is that my query correct?
For your first question:
Dim result = (from x in db.Processes
where x.LookupProcess.descr = "example"
select x).FirstOrDefault()
Actually, you missed some concepts from DataEntityModel, and its Framework. To manipulate data, you have to call object from contextual point of view. Those allow you to specify to the ObjectStateManager the state of an DataObject. In your case, if you have depending data from FK, you will have to add/update any linked data from leaf to root.
This example demonstrate simple (no dependances) data manipulation. A select if existing and an insert or update.
If you want more info about ObjectStateManager manipulation go to http://msdn.microsoft.com/en-us/library/bb156104.aspx
Dim context As New Processing_context 'deseign your context (this one is linked to a DB)
Dim pro = (From r In context.PROCESS
Where r.LOOKUPPROCESS.descr = LookupProcess.descr
Select r).FirstOrDefault()
If pro Is Nothing Then 'add a new one
pro = New context.PROCESS With {.AP_ID = "id", .PROCESS_ID = "p_id"}
context.PROCESS.Attach(pro)
context.ObjectStateManager.ChangeObjectState(pro, System.Data.EntityState.Added)
Else
'update data attibutes
pro.AP_ID = "id"
pro.PROCESS_ID = "p_id"
context.ObjectStateManager.ChangeObjectState(pro, System.Data.EntityState.Modified)
'context.PROCESS.Attach(pro)
End If
context.SaveChanges()
I hope this will help. Have a nice day!
For your first question, to expand on what #jeroenh suggested:
Dim result = (from x in db.Processes.Include("LookupProcess")
where x.LookupProcess.descr = "example"
select x).FirstOrDefault()
The addition of the Include statement will hydrate the LookupProcess entities so that you can query them. Without the Include, x.LookupProcess will be null which would likely explain why you got the error you did.
If using the literal string as an argument to Include is not ideal, see Returning from a DbSet 3 tables without the error "cannot be inferred from the query" for an example of doing this using nested entities.
For your second question, this line
p.LookupProcess = (from m in db.LookupProcessIds
where descr = "example" select m).FirstOrDefault()
Could cause you problems later on because if there is no LookupProcessId with a process_description of "example", you are going to get null. From MSDN:
The default value for reference and nullable types is null.
Because of this, if p.LookupProcess is null when you insert the entity, you will get the exception:
Entities in 'AmpCUEngEntities.Processes' participate in the 'FK_Process_LookupProcess' relationship. 0 related 'LookupProcess' were found. 1 'LookupProcess' is expected.
To avoid this kind of problem, you will need to check that p.LookupProcess is not null before it goes in the database.
If Not p.LookupProcess Is Nothing Then
db.AddToProcesses(p)
db.SaveChanges()
End If

AIR SQLite IN expression not working

I'm having a problem with an expression in my sql statement in SQLite for Adobe AIR
basically I have this
sql = "UPDATE uniforms SET status=#status WHERE customerId IN(19,20)";
updateStmt.parameters["#status"] = args[1];
updateStmt.execute();
if I run the above code it works, updating the status when the id are 19 and 20
but if I pass the ids list as a parameter like this
sql = "UPDATE uniforms SET status=#status WHERE customerId IN(#ids)";
updateStmt.parameters["#status"] = args[1];
updateStmt.parameters["#ids"] = "19,20";
updateStmt.execute();
it gives me and error, saying could not convert text value to numeric value, which make sense because I'm passing and string but the IN expression should convert it accordingly, like it does when I pass directly the list values, why is not working the other way, thanks for any help!
Not sure but I think the problem is that your #ids parameter should be an array when using it like this. Try
updateStmt.parameters["#ids"] = new Array(19,20);

Resources