MySQL for the Internet of Things

(Steven Felgate) #1

Chapter 4 ■ Data transformation


■Note this code is meant to be an example of how to encode and decode. if you want to use this in your


own solution, you may need to modify it to fit your code.


Notice one method uses a loop to iterate over the bytes while the other uses the more common
approach of using a conditional statement. I include both, but you can use whichever is easiest to
understand. Notice that both methods require the size of the integer. This is necessary because the integer
could be 1, 2, or 4 bytes in length.
Another concept of data type usage concerns values that never change. In this case, you can declare the
variable as a constant, which tells the compiler to substitute the variable with the constant value, thus saving
a small bit of memory. Do this with larger data types and you could save a lot of memory. Consider the
following code snippet. The first line of code uses 2 bytes, while the second forces the compiler to substitute
10 everywhere the variable appears.


int multiplier = 10;
const int multiplier = 10;


There are quite a number of tricks and techniques for working with data types—so much so that entire
multivolume books have been written to explore the nuances and pros and cons of every technique possible.
If you are using an Arduino in your IOT solution and want to ensure you are programming it with as much care
and efficiency as possible, see Dr. Simon Monk’s book Programming Arduino Next Steps (McGraw Hill, 2014).
If you are programming your low-cost computer board with Python, many of these techniques are the
same or have similar application. An excellent book on efficient Python programming on the Raspberry
Pi is Wolfram Donat’s book Learn Raspberry Pi Programming with Python (Apress, 2014). If you want to
go beyond the basics of Python programming, check out J. Burton Browning and Marty Alchin’s book Pro
Python (Apress, 2014).


Database Considerations


Fortunately, you are not likely to encounter the same memory limitations when storing data in a database
that you would with a microcontroller or similar device with small memory sizes. This is because databases
are good at storing data in specific formats and in some cases can optimize the storage used (especially for
text). Perhaps more importantly, database servers have a lot more storage space than a microcontroller.
Thus, there really isn’t an issue storing data in its proper type for a database with some minor
exceptions. That is, there are some possible border cases where complex data types may not be available on
the database server, but there are certainly enough primitives that you can save whatever type you need.


■Note mysQL supports many data types. see the section entitled “Data types” in the mysQL online


reference manual for more details (http://dev.mysql.com/doc/refman/5.7/en/).


Furthermore, the issue of dealing with scale is not an issue for storing data. This is because, once
again, the database is really good at that. Thus, rounding or limiting decimals for floating-point numbers is
more about presentation than anything else. However, if you need to do something like this, you can in the
database server.

Free download pdf