Author Topic: Simple SQL Query Returns No Data  (Read 313 times)

Legacy_BelowTheBelt

  • Hero Member
  • *****
  • Posts: 699
  • Karma: +0/-0
Simple SQL Query Returns No Data
« on: November 18, 2014, 07:56:39 pm »


               

I must be missing something very basic, but it's stymied me for a couple of days and I need to figure this out.


 


I have a general table that holds module-level data, called mod_main.  One  of the columns there is "InGameDate" and is a varchar type (50 chars wide).


 


OnModuleLoad, I make two function calls regarding this field:


 


1)  GetDate to retrieve the string from the InGameDate field with:




SQLExecDirect("SELECT InGameDate FROM mod_main");
string sDate = SQLGetData(1);
WriteTimestampedLogEntry ("GetDate = "+sDate);


 

and

 

2)  SaveDate to save a new date into the InGameDate field with a date iValue:



SQLExecDirect("UPDATE mod_main SET InGameDate ='"+IntToString(iValue)+"'");
WriteTimestampedLogEntry ("SetDate = "+IntToString(iValue));


Seems easy enough.


 


However, when I look at my logfile here's what I get:


 


[Tue Nov 18 19:48:19] GetDate = 

[Tue Nov 18 19:48:19] SetDate = 11072527

 

As you can see, the SetDate is accurate and it is writing/updating the database (This is a mathmatically-arrived value of the year, month, day, and hour).  I've verified that it is writing correctly to the db.  But, the GetDate string is empty.  Even when I set this on a looping delay timer pseudo hb, the GetDate continues to be blank, while the SetDate continues to update correctly.

 

I've tried modifying the InGameDate column to text format (from varchar) to no avail.

 

Additionally, when I run the GetDate query directly in SQL, it properly returns the value of the field.

 

Any thoughts?


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Simple SQL Query Returns No Data
« Reply #1 on: November 18, 2014, 09:52:29 pm »


               

I believe you need to call SQLFetch() before SQLGetData().



               
               

               
            

Legacy_WhiteTiger

  • Hero Member
  • *****
  • Posts: 889
  • Karma: +0/-0
Simple SQL Query Returns No Data
« Reply #2 on: November 21, 2014, 05:58:59 am »


               

I recommend you first check if query returned no result.

 

this way should work 



string sSQL = "SELECT InGameDate FROM mod_main";
PrintString("Executing query: " + sSQL);
SQLExecDirect(sSQL);

string sDate = "";

if (SQLFetch() != SQL_SUCCESS)
{
PrintString("Could not execute the query or query returned no result.");
return; //stops the current script, you may delete here if you want
}
else
{
sDate = SQLGetData(1);
PrintString("GetDate = "+sDate);
}


               
               

               
            

Legacy_BelowTheBelt

  • Hero Member
  • *****
  • Posts: 699
  • Karma: +0/-0
Simple SQL Query Returns No Data
« Reply #3 on: November 21, 2014, 06:40:40 pm »


               

Thanks.  I was presuming the SQLFetch was optional to validate that the data found (or not) or to return the next row.  Didn't realize it was required.  Put it in and it works.



               
               

               
            

Legacy_WhiteTiger

  • Hero Member
  • *****
  • Posts: 889
  • Karma: +0/-0
Simple SQL Query Returns No Data
« Reply #4 on: November 21, 2014, 09:14:19 pm »


               

Good you got it. (:


Oh, what are you trying to do with game time?