Accessing sql data?

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
Is this the correct way to retrieve information from the database?

iGameSpeedMultiplier = DB.Query( "SELECT GrowthPercent FROM GameSpeeds WHERE 'ID' = " .. PreGame.GetGameSpeed() ) / 100;
 
wiki

The short answer is, in a lua script, you can just use "GameInfo.GameSpeeds" and iterate through it.
 
Thank you. The wiki's so sparse of information right now I admit I haven't looked at it much yet. So in other words, simplify to this?

iGameSpeedMultiplier = GameInfo.GameSpeeds.(PreGame.GetGameSpeed()).GrowthPercent / 100;
 
Thank you. The wiki's so sparse of information right now I admit I haven't looked at it much yet.
No problem. I agree about the wiki. In fact, I just finished putting my first small contribution into it before I read this. :p
 
Hmm... if I try the DB.Query method it states I "cannot perform arithmetic on a function value", and if I use the second method it states '<name>' expected near '('

What's the proper way to retrieve just a single value, if I don't need to loop through them all?

Edit: Nevermind, figured it out, have to use array index [] notation instead of dot if passing the result of a function... even though I thought the two were identical in implementation, apparently there's some differences.

GAME_SPEED_MULTIPLIER = GameInfo.GameSpeeds[PreGame.GetGameSpeed()].GrowthPercent / 100;
 
It's one of those quirks of Lua, that everything is a table.
 
Hmm... if I try the DB.Query method it states I "cannot perform arithmetic on a function value", and if I use the second method it states '<name>' expected near '('

What's the proper way to retrieve just a single value, if I don't need to loop through them all?

Edit: Nevermind, figured it out, have to use array index [] notation instead of dot if passing the result of a function... even though I thought the two were identical in implementation, apparently there's some differences.

GAME_SPEED_MULTIPLIER = GameInfo.GameSpeeds[PreGame.GetGameSpeed()].GrowthPercent / 100;
Not quite identical... Tablename.foo is equivalent to Tablename["foo"]. Any query to the database might return multiple values (as far as SQL is concerned), so it would make some sense for all return values to be tables, presumably following array conventions.
 
What about setting data? I know the SQL command, but is there a way to do it with simply...

GameInfo.tablename[row][column] = x ?

Or does this only change the local Lua table...
 
Sseckman said:
Any changes you make to this structure will NOT modify the database and could cause potential problems with other scripts.
That's from the wiki.
 
Hmm... so gotta use the SQL version then, as I suspected. Somewhat more cumbersome code but still manageable... thanks.

Basically what I'm doing is just increasing the yield of an improvement (like some techs do).
 
Thalassicus, could you please put some sample code here showing how to change db (with Lua embedded SQL commands)?

I'd also be interested in knowing which of these changes "stick" (i.e., are retained in gamesave) and which you have to re-update when game reloaded. Some of these are non-intuitive -- e.g., it seems like trait changes get kept in gamesave, but many/most other db elements are not.
 
Well, what I'm actually doing is storing the values I want to change in the save file and retrieving them later. I haven't got it all working yet though, have some other work-related stuff to deal with first. I'll post the code when I complete it sometime in the next few days.
 
It is only safe to assume that all data you put into the database cache will be eliminated on the next reload. Trying to store anything there is useless if the user ends up changing mods.
 
Yes, I agree that it is not safe to depend on the db cache at all. But I'm pretty sure that some db contents are NOT reloaded from db cache, but rather retained in (and reloaded from) gamesave file. If so, it would be useful to know which db contents behave this way.

I may be way off base here. The above interpretation is my inference based on modifying the db and then observing effects (or lack of effects) in reloaded games vs. new games.
 
Back
Top Bottom