Parameter index out of range MySQL PreparedStatement? - mysql

I try to do a select:
but I receive an Exception:

Try it like that:
preparedStatement = connection.prepareStatement(sqlCompetition);
preparedStatement.setInt(...);
I think you have still the old statement in the variable

You are creating PrepareStatement object at line number 212 but you are not assigning to the prepareStatement variable, which means prepareStatement variable still holds old object.
At line number 213 you are using setInt() method, it'll calls the old object. It seems your old object contains any place holders, because of the this reason you got Parameter index out of range exception.
Assign the newly created PreparedStatement object to prepareStatement variable at line number 212 to resolve your problem.

Related

MS SSIS : Data Type conflict for variables (String vs Object) working with Access MEMO type

I made this package to make my sql task more dynamic:
Step1. I select SQLtext to run from my AccessTable and store in SQLStr var.
Step2. I run that Select statement stored in var SQLStr from Step1.
However I can run Step1 only if I used DataType = Object, and I can run Step2 only if i use DataTYpe = String, I played with it and verified this 100$.
How I can solve this problem, can I do some convert? of datatypes, my query stored in Access DB type = Memo, as it quite long, and might have line breaks.
What is the trick ?? Please refer to pic below for all details.
** Please note that this problem only occurs for Access Memo Type field !!!!**
In other setup it should work with String type OK in Step1
In your first query, when you select SQLText, Cast it as a string (in the select statement) so that you can store it in a string variable.
If it is not possible to CAST the memo field to a string in the SELECT (I'm not familiar with Access), then you can iterate through the object variable in a foreach loop and store the value of the SQLText column in a string variable, and then proceed to step 2 using that.
IF neither of those work, as a brute force method, you can follow the solution in this thread to import the Access table to a Staging table on SQL Server that uses an nvarchar(max) field, which you should be able to select into a string variable with no problem.

ActiveRecord::Result returned from mysql backed `.select_all` not having type info

I'm trying to type cast the result from ActiveRecord::Base.connection.select_all, but type info seems to be empty. According to this Stackoverflow Post, the type info can be derived by result.column_types at least when using postgres adapter. But when I run the same code with mysql adapter, I get nothing returned.
result = ActiveRecord::Base.connection.select_all(User.all.to_sql)
result.column_types #=> returns {}
result.cast_value #=> returns uncasted values
I know there's something like this that casts the thing,
User.type_for_attribute('id').deserialize('100')
# => 100
But this won't work well when working with ActiveRecord::Result, because we don't know which column belongs to which ActiveRecord class (at least from the result).
**Question 1. Are there any easy ways to achieve type casting on ActiveRecord::Result?
I followed the source code a bit, and seems like mysql adapter isn't passing the type info when initialising the Result object. Below is how ActiveRecord::Result is initialised (gh code),
module ActiveRecord
class Result
def initialize(columns, rows, column_types = {})
#columns = columns
#rows = rows
#hash_rows = nil
#column_types = column_types
end
...
And here's ActiveRecord's mysql adapter code and you can see it doesn't pass anything as the third argument, whereas, postgres code does.
So I'm guessing type casting for mysql is done somewhere else.
Question 2. Anybody knows where the magic for mysql is happening along with the normal ActiveRecord usage like User.first.id?

Using a variable retrived from a file as column value in a SQL Query in SilkTest Script

I am creating a SilkTest script in which am storing a string in a List of String variable
List of STRING FaultDn
This variable reads the string from a file.
FaultDn = ReadFile("C:\FaultDn.txt")
Then I try to connect to SQL database to retrieve a value by providing this string
HSQL hstmnt = DB_ExecuteSql (hdbc, "select Id from openview.dbo.OV_MS_Message where OriginalServiceId like #FaultDn")
But it returns an error
Error: (42000) [Microsoft][SQL Server Native Client 10.0][SQL Server]Must declare the scalar variable "#FaultDn".
Can you please help in correcting this SQL Query in SilkTest Script?
I think you need to change your query to
HSQL hstmnt = DB_ExecuteSql (hdbc, "select Id from openview.dbo.OV_MS_Message where OriginalServiceId like '{FaultDn}'")
The difference being at '{FaultDn}'. You need to add the curly braces so Silk Test interprets it as a variable name, and the quotes because it is a string literal within the SQL query.
What I'm not exactly sure about it whether you're intentionally passing the whole list into the query, if you just want to pass the first line, you should change it to '{FaultDn[1]}'.

Getting Error message from linq to entity query.

I keep receiving the error
"LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression"
on the following line in my code
var Reviewer = repository.reviewers.FirstOrDefault(t => t.ReviewerName == formCollection[3]);
formCollection[3] is a string returned from a drop down I have contained within a form. The query seems to work O.K. until it returns the value from the database. What can I do to fix this?
OK, I was trying to do too much at once, when I finally thought about it and put formCollection[3] into a string variable and then used the string variable in the linq query everything worked out ok.

SSIS SQL Task - "Parameter name is unrecognized"

I have a SQL Task that needs to run a simple update to update a single row.
I have set the SQLStatement to:
update agency set AgencyLastBatchSeqNo = ? where agencyID = ?
On the Parameter Mapping page I gave set Parameter 0 and Parameter 1 to variables that I know contain the right values. I have also set the Parameter Name values correctly.
In the database, the column AgencyLastBatchSeqNo is an int, AgencyID is a big int. Does anyone have a reference to find what the data types map to in SSIS? I have guessed at SHORT for the int and LONG for the big int.
When I run the task I get the following error:
[Execute SQL Task] Error: Executing the query "update agency set AgencyLastBatchSeqNo = ? where AgencyID = ?" failed with the following
error: "Parameter name is unrecognized.". Possible failure reasons:
Problems with the query, "ResultSet" property not set correctly,
parameters not set correctly, or connection not established
correctly.
Could anyone please suggest what may be wrong?
Thanks
Rob.
The answer to this is to change the Parameter Name value in the Parameter Mapping screen.
Given the following query
SELECT Id, AnimalName FROM dbo.Farm WHERE Farm_id = ?
Assuming my Parameter is an integer Variable named User::Farm_id
Choose the following values on the Parameter Mapping Screen
Variable Name - User::Farm_id
Direction - Input
Data Type - LONG
Parameter Name - 0
Parameter Size - -1
Originally the Parameter Name will be "NewParameterName". Simply change this to the ordinal position of your variable marker ("?")
If you are using more than 1 parameter then in the execute sql task window go to parameter mapping and set the parameter name to 0,1,2,3....depending on the number of parameter and the parameter size to -1..
This must be helpful to resolve your issue.
One thing you don't mention is your connection type. I assume you are not using ADO.Net since the parameter marking in that case is not a ?. For the other types of connection, parameters are named as follows:
ADO (not ADO.Net) connection: parameter names are Param1, Param2...
ODBC connection: parameter names are 1,2,3...
OLEDB connection: parameter names are 0,1,2...
For the variable types (they are different in the parameter mapping section than in any other area of SSIS) I typically use Long for Int's and I typically leave the length set to -1. I believe that a Long will work for both Int's and Bigint's.
See SSIS data types.
int = DT_I4 (4 byte integer) = Int32 variable
bigint = DT_I8 (8 byte integer) = Int64 variable
Make sure you're quoting your values, and that you don't have typos in your column names.
When defining the parameter mappings any trailing blanks after the parameter name can cause this message too.
Go to Parameter Mapping and change Mapped Parameter_Name = 0 instead of using default name.
and for next parameter it should be 1. and so on...
Note:- you have to change the "Data Type" as per the value.
it will just take place on the occurrence of "?"