Checking a field for NULL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subaruwrx88011@gmail.com

    #1

    Checking a field for NULL

    So I pressed tab-enter and posted an incomplete topic, sorry.

    I have imported information into a database using a file. It is tab
    delimited and for NULL fields I have NULL.

    example file.txt
    1<tab>Hello World<tab>NULL< tab>5.56

    1) Is this a correct way to make sure that this field will be NULL

    2) in C++ after I fetch the field can I say
    if(<field> != NULL)

    Thanks in advance

  • Thomas Bartkus

    #2
    Re: Checking a field for NULL

    <subaruwrx88011 @gmail.com> wrote in message
    news:1139861126 .665703.217110@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > So I pressed tab-enter and posted an incomplete topic, sorry.
    >
    > I have imported information into a database using a file. It is tab
    > delimited and for NULL fields I have NULL.
    >
    > example file.txt
    > 1<tab>Hello World<tab>NULL< tab>5.56
    >
    > 1) Is this a correct way to make sure that this field will be NULL[/color]

    Yes!
    NULL is a keyword in MySQL to be interpreted as Null.

    Although 2 successive tabs <tab><tab> in a tab delimited text import denotes
    a Null , it never hurts to be explicit. Both "<tab><tab> " and
    "<tab>NULL< tab> will cause a Null value between the tabs. If you can
    arrange to get an explicit "NULL" between the tabs - go for it. Do note,
    however, that MySQL doesn't care one way or the other. It just makes it
    easier for a human being to troubleshoot.
    [color=blue]
    > 2) in C++ after I fetch the field can I say
    > if(<field> != NULL)[/color]

    You could also test it in your SQL using ISNULL() before it hits your C++.
    Why not explicitly test for Null in your SQL?

    IsNull(field) will return 1 (True) if the field value is Null or 0 (False)
    if it contains a valid value.

    You can also use an SQL IF():
    IF(IsNull(<fiel d>, {What to show in case of Null}, {What to show if the
    field is valid})

    You could do:
    IF(NOT IsNull(<fiield> , ....
    but my brain has trouble wrapping around the double negative - even if MySQL
    has no problem with it.

    You can also use:
    IFNULL(<field>, {Whatever it is you want to see if the field turns out to
    be Null})
    That will put forth the normal value of <field> if it *has* a normal value,
    or if it is Null you will get whatever else it is you want to specifiy in
    that situation.

    In general - I would do as much field testing as possible up front in SQL
    code.
    It's just the kind of thing MySQL does best.
    Thomas Bartkus


    Comment

    • subaruwrx88011

      #3
      Re: Checking a field for NULL

      Ya that would be nice but we are trying to limit the calls to MySQL.
      After i do a query I fetch a row and store it into a local structure to
      limit calls to the database. The place the field value is set to is a
      char*. So if it is NULL in the database shouldn't this char* also be
      NULL.

      Comment

      • Thomas Bartkus

        #4
        Re: Checking a field for NULL

        "subaruwrx88011 " <subaruwrx88011 @gmail.com> wrote in message
        news:1139867039 .227753.281400@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > Ya that would be nice but we are trying to limit the calls to MySQL.
        > After i do a query I fetch a row and store it into a local structure to
        > limit calls to the database. The place the field value is set to is a
        > char*. So if it is NULL in the database shouldn't this char* also be
        > NULL.[/color]

        I neither know (nor care!) why you might want to limit the calls to MySQL
        But
        There is nothing in what I suggested that would increase the database
        traffic.
        Post processing for Nulls does nothing to limit calls to the database and
        frankly -

        - it would seem to be silly waste of coding effort.
        Thomas Bartkus


        Comment

        • Thomas Bartkus

          #5
          Re: Checking a field for NULL

          "subaruwrx88011 " <subaruwrx88011 @gmail.com> wrote in message
          news:1139867039 .227753.281400@ z14g2000cwz.goo glegroups.com.. .[color=blue]
          > Ya that would be nice but we are trying to limit the calls to MySQL.
          > After i do a query I fetch a row and store it into a local structure to
          > limit calls to the database. The place the field value is set to is a
          > char*. So if it is NULL in the database shouldn't this char* also be
          > NULL.[/color]

          I neither know (nor care!) why you might want to limit the calls to MySQL
          But
          There is nothing in what I suggested that would increase the database
          traffic. Post processing for Nulls does nothing to limit calls to the
          database and
          frankly -

          - it would seem to be silly waste of coding effort.
          Thomas Bartkus


          Comment

          Working...