Sqlite3 table have INT column with possible NULL values.
I add the data to the table with this code:
if ( ptr == NULL )
sqlite3_bind_null(stmt, 5);
else
sqlite3_bind_int64(stmt, 5, ptr->key_session );
then I get back my values with the following code:
unsigned int key_session = (unsigned int)sqlite3_column_int64(stmt, 0);
So, what is about NULL? What is the best practice with NULLs in the table? how to check for NULL? or what will occur in my GET code with NULL ?
You can do this check:
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
...
If the check succeeds, the value was NULL; otherwise, it was a non-null int64.
You can check whether sqlite_column_type() returns SQLITE_NULL.
If 0 is a valid return for nulls, you don't need to do anything special - nulls will convert to zero if an integer is requested.
Related
I am trying to understand a data structure, hash table with open addressing.
I am currently reading on the source code provided by geekforgeeks, but I have a few questions on the code.
Below, is the pasted function for inserting Node from geekforgeeks.
//Function to add key value pair
void insertNode(K key, V value)
{
HashNode<K,V> *temp = new HashNode<K,V>(key, value);
// Apply hash function to find index for given key
int hashIndex = hashCode(key);
//find next free space
while(arr[hashIndex] != NULL && arr[hashIndex]->key != key //// LINE 9 //////
&& arr[hashIndex]->key != -1)
{
hashIndex++;
hashIndex %= capacity;
}
//if new node to be inserted increase the current size
if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1) //// LINE 17 //////
size++;
arr[hashIndex] = temp;
}
Questions
In line 9, why would you check three conditionals, being,
if slot inside the hash table is null ===> arr[hashIndex] != NULL
AND if slot has the same key with the node that is going to be inserted ===> arr[hashIndex]->key != key
AND if slot has the key of -1, which indicates the slot where node was deleted before ===> arr[hashIndex]->key != -1
If I were to optimize this code, I believe checking whether the slot is NULL or not is already enough.
In line 17, why would you increment the size property of HashMap before assigning the node to the slot? ===> if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1)
size++;
To me, this logic seems to be messy.
I would rather do, arr[hashIndex] = temp; size++;
With the assumption of geekforgeeks's logic is well written, could you explain to me why the logic for inserting the new node to a hash table with open addressing is implemented as above specifically on the two points I have raised?
The three conditions to have a valid index are:
The object at the index is NULL
OR the object is not NULL, but its key is the same of the one we're inserting
OR the object is not NULL, but its key value is -1
Since the negation of all three conditions occurs, we don't have a valid index, and the loop rolls on.
In line 17: size is incremented only if the insertion doesn't reuse an existing index, so the node is new (which means either condition 1 or 3 applies).
I am reading data from excel cell via ADO
while (!pRec->adoEOF)
{
_variant_t a = pRec->Fields->GetItem(long(0))->Value;
//todo
}
how to check if a equal NULL
a.bstrVal==NULL
it didn't work.
I suspect you want:
a.vt == VT_NULL // Represents a NULL received from a database
Or
a.vt == VT_EMPTY // Represents an uninitialized object
Database Nulls have a specific representation in VARIANT.
However if it is from Excel it's also possible you are getting an empty string rather than a null.
a.vt == VT_BSTR && SysStringLen(a.bstrVal) == 0
I have this kind of code
two->height = max(two->right->height, two->left->height);
One of the two->right or two->left can be a pointer to null so the program will seg fault . I am looking for , if the two->left is null it will get transformed into zero so the two->right will be automatically true .
Is there any trick that can overcome this issue ?
This can also work:
two->height = max(
( two->right != nullptr ? two->right->height : 0 ),
( two->left != nullptr ? two->left->height : 0 )
);
You are first going to want to perform a check on the left and right pointers and see if they are null. Something along the lines of:
if(two->right == NULL) {
...
}
else if(two->left == NULL) {
...
}
else {
two->height = max(two->right->height, two->left->height);
}
There are many ways to deal with pointers being NULL. I just picked a simple one for an example.
I would like to to convert string to double. I do that this way:
bool String2ValueType(const std::string & a_str_value_type, double & a_result)
{
if(a_str_value_type.empty())
return false;
char * end;
double result = strtod(a_str_value_type.c_str(), &end);
if(*end!=NULL)
return false;
a_result = result;
return true;
}
How can I check if result is fine, or not, if it's value is 0?
for example i will get 0 if I will send string("0"), and this is not error.
But I can also send some other string- that is number, but it will not be converted and I will get also 0 (error), ( I'm talking about case when *end == 0 ).
If no conversion is performed, zero is returned and the value of nptr
is stored in the location referenced by endptr.
At least, that's what man page says.
I have spotted strange behavior with lua_getfield() function. Please look at this code fragment, which should just get a field from table located at the stack top:
if(lua_istable(L,-1)){
lua_getfield(L,-1,"field_name");
int type = lua_type(L,-1); // returns LUA_TNIL
int field_value = lua_tointeger(L,-1); // returns 0
lua_pop(L,1);
// and now let's try iterating all table's fields:
lua_pushnil(L); // first key
while(lua_next(L, -2) != 0){
// uses 'key' (at index -2) and 'value' (at index -1)
CString key = lua_tostring(L,-2);
int type = lua_type(L,-1);
if(key == "field_name"){ //
int value = lua_tointeger(L,-1); // returns correct value!!!! (type == LUA_TNUMBER)
// ????? what the heck ????
}
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
The question is, why lua_getfield() doesn't work, while lua_next() works perfectly?
I have used lua_getfield() tens of times with no problems and now I'm bumping my head into my keyboard...
reagards
Marcin
Problem solved. There was a problem with adjusting the number of results returned by previous call to lua_pcall. What confuses me though, is why lua_next worked correctly when lua_getfield() miserabely failed...