How does PAWN deal with NULL returns from a MySQL database?
#1

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.
Reply
#2

In a string it will be NULL and in an integer it will be 0
Reply
#3

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?
Reply
#4

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.
Reply
#5

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
Reply
#6

If you are using blueg plugin, there's "ismysqlnull" macro/function - it's a wrapper around strcmp.
Reply
#7

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?
Reply
#8

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.
Reply
#9

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
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)