SQL select

Post Reply
Prescott
Newbie
Posts: 6
Joined: Fri Aug 12, 2022 1:03 pm

SQL select

Post 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);
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: SQL select

Post 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?
Prescott
Newbie
Posts: 6
Joined: Fri Aug 12, 2022 1:03 pm

Re: SQL select

Post 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
Post Reply