Page 1 of 1

Using Random number as a percentage

Posted: Fri Mar 25, 2011 7:30 am
by maverickalex
Firstly,

I am trying to generate a random number between two known number.

My script grabs an aircraft type from a database and i am trying to use the IF command like so.

Code: Select all

 --generate the passenger numbers for each aircraft type trying to generate random passenger count (approx 80% capacity) for each type of aircraft
put "SELECT name from phpvms_aircraft where id='"&tAircraft&"'"into tplane
   put revDataFromQuery(comma,return,gConnectionID,tplane) into tplanedata
   
   if tplanedata is"B737-300" then
      put  random(140) +60 into tpass -- max passengers = 180
   else if tplanedata is "FOKKER50" then
      put random (58) +10 into tpass --max passengers = 58
   else if tplanedata is "B777-200" then
      put random (440)+300 into tpass -- max passengers = 440
   else if tplanedata is "IL96" then
      put random(90000) +10000 into tpass -- this is lbs of cargo
   else if tplanedata is "MD81" then
      put random(140) +40 into tpass -- max passengers = 157
Obviously i am not using the Random function correctly as it spits out all sorts of numbers.


Secondly,

I have an active database table called pireps. it has a column "load"
my test table is called test (duh) and also has a column "load"
both have the same attributes ie type, length/values etc.

When i try to do an INSERT using a column "load" and variable "tload" i get a syntax error.
if instead of using load and tload i rename them pass and tpass (creating a column "pass" in my table with exactly the same attributes and renaming my field "load" to "pass") it works fine.

I need to update the column "load" as i cannot change it.
Anyone any ideas why this might be the case

here is the insert using "pass"

Code: Select all

INSERT INTO phpvms_test (pilotid,code,aircraft,flightnum,depicao,arricao,flighttime,flighttime_stamp,distance,submitdate,fuelused,fuelunitcost,fuelprice,flighttype,gross,pilotpay,revenue,pass,price,rawdata) VALUES ('081','AVA','8	','302A','KLAX','CYUL','03:36','3:36 AM','376.443','2011,3,25,6,0,0,6','2301','0.96','2209','P','254100','360.72','251530.32','605','420','first pirep')
heres the error message i get when changed to load
Image

message

Code: Select all

INSERT INTO phpvms_test (pilotid,code,aircraft,flightnum,depicao,arricao,flighttime,flighttime_stamp,distance,submitdate,fuelused,fuelunitcost,fuelprice,flighttype,gross,pilotpay,revenue,load,price,rawdata) VALUES ('081','AVA','8	','302A','KLAX','CYUL','03:36','3:36 AM','376.443','2011,3,25,6,21,0,6','2301','0.96','2209','P','186060','360.72','183490.32','443','420','first pirep')
It has me baffled!!!

Re: Using Random number as a percentage

Posted: Fri Mar 25, 2011 9:04 am
by bn
Hi Alex,

as for your first problem with random

from the dictionary
To generate a random number between two integers, use a handler like this:

function randomInRange lowerLimit,upperLimit
return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
No idea about the database problem.

Kind regards

Bernd

Re: Using Random number as a percentage

Posted: Fri Mar 25, 2011 9:37 am
by maverickalex
Thanks Bernd

I had seen that in the dictionary but couldn't figure out where or how to implement it.
I noticed an example using random elsewhere which is where i got the current script from.

could you show an example on how to use that function.

Thanks

Re: Using Random number as a percentage

Posted: Fri Mar 25, 2011 9:52 am
by bn
Hi Alex,

if you want to use it inline then:

Code: Select all

   --suppose you want a lower limit of 60 and an upper limit of 180
   putĀ  random(180-60+1) +60-1  into pass -- max passengers = 180
if you want to use it as a function then:

Code: Select all

 on MyHandler
 --suppose you want a lower limit of 60 and an upper limit of 180
   put randomInRange (60, 180) into pass
end MyHandler
-- 
function randomInRange lowerLimit,upperLimit
   return random(upperLimit - lowerLimit + 1) + lowerLimit - 1
end randomInRange
Kind regards

Bernd

Re: Using Random number as a percentage

Posted: Fri Mar 25, 2011 9:14 pm
by maverickalex
Bernd,

Thank-You ever so much for that. really made my day getting that part to work.

Now to try and trace why field "load" anf variable tload wont work.

Cheers
Alex

Re: Using Random number as a percentage

Posted: Sat Mar 26, 2011 9:04 am
by maverickalex
ok,

i have tried to simplify things to identify why load wont work as a variable tload

i created a new stack which just puts a single field into the variable tload
then inserts it into the db

Code: Select all

put field "load" into tload
   
   put "INSERT INTO phpvms_load (load) VALUES ('"&tload&"')"  into tdata
   revExecuteSQL gConnectionID,tdata
   put tdata
i get the syntax error

message box says
INSERT INTO phpvms_load (load) VALUES ('111')

if i change the variable tload to tpass

like this

Code: Select all

 put field "load" into tpass
   
   put "INSERT INTO phpvms_load (pass) VALUES ('"&tpass&"')"  into tdata
   revExecuteSQL gConnectionID,tdata
   put tdata
message box says
INSERT INTO phpvms_load (pass) VALUES ('111')

and the database is updated succesfully

both columns load and pass have exactly the same attributes.
Why would this be, can anyone shed a light on this? perhaps mySQL does not allow a field load?

Re: Using Random number as a percentage

Posted: Sun Mar 27, 2011 5:04 am
by maverickalex
I found the cause and it was as i suspected. Load is a reserved word in MYSQL. In order to use it in a table or column name it needs to be surrounded by back ticks `load`.

Alex