Page 1 of 1

SQL select

Posted: Tue Aug 16, 2022 1:36 pm
by Prescott
Hello, I´m trying run SQL select in javascript. (in Switch scripter)

I can do:
1. connect to database
2. update sql command
3. read log if the sql command was succesfull

But I can not get a result from simple "SELECT * from table" sql command (value from table) :roll:

For example this script:

Code: Select all

	var SQL = new DataSource();
	var dbo = SQL.connect('SQL19','user','pass');
	s.log (1, "OK DB: "+ dbo);
	
	var FStatement =  new Statement( SQL);
	var SQLcommand = "SELECT * FROM table WHERE job='111111'";
	isSucces = FStatement.execute(SQLcommand);
	
	s.log (1, "Command status: "+ isSucces);

Re: SQL select

Posted: Tue Aug 16, 2022 5:51 pm
by freddyp
This is just a snippet, not tested. What you are missing is the while construction.

Code: Select all

var queryResult = sqlStatement.execute( SQLcommand);

if (queryResult == false) {
	dsnConnection.disconnect();
	throw "SQL query failed: "+ SQLcommand; //or some other error handling
}

var returnResult = new Array();
while (sqlStatement.isRowAvailable()) {
	sqlStatement.fetchRow();
	var field = sqlStatement.getString( column);
	returnResult.push( field);
}
SQL.disconnect();
What database are you connecting to? I strongly advise against doing this in legacy! Or why not simply use the "Database connect" element?

Re: SQL select

Posted: Wed Aug 17, 2022 3:03 pm
by Prescott
Yess.. thank you very much it is working 8-)

Im working in legacy. I can not use DB connect element because I have a other issue with this: viewtopic.php?p=14106#p14106