How does PAWN deal with NULL returns from a MySQL database? -
Prostilov - 18.03.2015
Well, basically what the title says.
How are NULL values retrieved from a MySQL database interpreted by PAWN?
Will the value be NULL as well or will it be 0?
My apologies if this was not the right section to post this question.
AW: How does PAWN deal with NULL returns from a MySQL database? -
Kaliber - 18.03.2015
In a string it will be NULL and in an integer it will be 0
Re: AW: How does PAWN deal with NULL returns from a MySQL database? -
Prostilov - 18.03.2015
Quote:
Originally Posted by Kaliber
In a string it will be NULL and in an integer it will be 0
|
So if I understand what you're saying: if the destination variable of the database field in question is a string, the string will contain NULL, and if it's an integer it will be 0?
If so, is there a way to differentiate 0 and NULL if we're talking about integers?
AW: Re: AW: How does PAWN deal with NULL returns from a MySQL database? -
Kaliber - 18.03.2015
Quote:
Originally Posted by Prostilov
if the destination variable of the database field in question is a string, the string will contain NULL, and if it's an integer it will be 0?
|
If in the Database NULL is written and not '' then yes, then the string contains NULL
Quote:
Originally Posted by Prostilov
If so, is there a way to differentiate 0 and NULL if we're talking about integers?
|
No, because it's the same.
Re: AW: Re: AW: How does PAWN deal with NULL returns from a MySQL database? -
Prostilov - 18.03.2015
Quote:
Originally Posted by Kaliber
If in the Database NULL is written and not '' then yes, then the string contains NULL
No, because it's the same.
|
My bad, I was doubting if PAWN used NULL to indicate an empty variable or used it as an alias for 0.
Anyways, you answered my question, thanks.
+REP
Re: How does PAWN deal with NULL returns from a MySQL database? -
Misiur - 18.03.2015
If you are using blueg plugin, there's "ismysqlnull" macro/function - it's a wrapper around strcmp.
Re: How does PAWN deal with NULL returns from a MySQL database? -
Prostilov - 18.03.2015
Quote:
Originally Posted by Misiur
If you are using blueg plugin, there's "ismysqlnull" macro/function - it's a wrapper around strcmp.
|
I am indeed using BlueG's MySQL plugin, could you show me an example of how "ismysqlnull" works?
Re: How does PAWN deal with NULL returns from a MySQL database? -
Misiur - 18.03.2015
Included in a_mysql:
Quote:
#define ismysqlnull(%0) (strcmp(%0,"NULL",false)==0)
|
Example:
pawn Код:
new
probablyNullField[64]
;
cache_get_row(0, 0, probablyNullField, 1, 64);
if(ismysqlnull(probablyNullField)) {
print("Oh my, that field was null!");
} else {
printf("That field wasn't null, and its contents are: %s", probablyNullField);
}
Cache_get_*_int/float probably automagically convert null to 0, so if you have nullable field with distinct reason for null usage, use the former version.
Re: How does PAWN deal with NULL returns from a MySQL database? -
Prostilov - 18.03.2015
Quote:
Originally Posted by Misiur
Included in a_mysql:
Example:
pawn Код:
new probablyNullField[64] ; cache_get_row(0, 0, probablyNullField, 1, 64); if(ismysqlnull(probablyNullField)) { print("Oh my, that field was null!"); } else { printf("That field wasn't null, and its contents are: %s", probablyNullField); }
Cache_get_*_int/float probably automagically convert null to 0, so if you have nullable field with distinct reason for null usage, use the former version.
|
Alright, thank you for the detailed explanation.
+REP
Re: How does PAWN deal with NULL returns from a MySQL database? -
Vince - 18.03.2015
Quote:
Originally Posted by Misiur
Cache_get_*_int/float probably automagically convert null to 0
|
No, they don't. You get an error.
For the time being, I would recommend wrapping fields in the IFNULL() function if they can possibly be null.