Tuesday, January 31, 2012

get ForwardingTable size in tinyos


entry = call ForwardingTable.getTable(&n);
while(entry[cur_entry].valid)
cur_entry++;
    printf("\t\tthe length of ForwardingTable is %i\n", cur_entry);

Monday, January 30, 2012

Printf in tinyos


To see the output generated by the Printf tutorial application you need to start the PrintfClient by running
the following command:


java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSBXXX:telosb


A few points are worthy of note before jumping in and writing your own applications that use the functionality
provided by the printf library.
1. In order to use the printf library, the tos/lib/printf directory must be in your include path. The
easiest way to include it is by adding the following line directly within the Makefile of your top level
application:
CFLAGS += -I$(TOSDIR)/lib/printf
2. Remember that changing the printf buffer size is done similarly:
CFLAGS += -DPRINTF_BUFFER_SIZE=XXX3. You MUST be sure to #include "printf.h" header file in every component in which you would like to
call the printf() command. Failure to do so will result in obscure error messages making it difficult to
identify the problem

Monday, January 16, 2012

Allow user to access mysql database from anywhere

GRANT ALL ON *.* TO USERNAME@'%' IDENTIFIED BY 'PASSWORD';

Thursday, January 5, 2012

Connect to MySQL using C++ on Ubuntu

Source:  http://blog.trilabs.co.tz/2011/09/connect-to-mysql-using-c-on-ubuntu.html


Someone asked on Facebook how to connect to a MySQL Database Using C++. So I thought it will be cool to just share it here for everyone.

Ground Work

Here is how things look like on my computer.
  • OS:  Ubuntu 11.04  ( Linux legacy 2.6.38-11-generic #48-Ubuntu SMP Fri Jul 29 19:05:14 UTC 2011 i686 i686 i386 GNU/Linux)
  • DB: MySQL 5.1 ( mysql Ver 14.14 Distrib 5.1.54, for debian-linux-gnu (i686) using readline 6.2)
  • Compiler:  GCC 4.5 (g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2)
I had to install some C++ MySQL Connector and other helpers, actually after trying the textbook way and it did not work, I did a bit of googling and asking dev here and there.
?View Code SHELL
sudo apt-get install libmysqlcppconn4 libmysqlcppconn-dev libmysqlclient-dev

Code

To illustrate this, I am going to connect to the development database of one of the project I am working on show a list of products against store they can be bought. I am assuming your database knowledge in order. I have also added as much details as I could on the code itself.
?View Code CPP
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
 
/**
 * This structure will be used to store all cessary inforamtion to connect to the database
 */
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};
 
/**
 * Sets up connection to the database, this depends on the functions from mysql.h.
 * This function will connect and return a MYSQL pointer to the new connection using the
 * structure above connection_details.
 */
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
     // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);
 
    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
      printf("Conection error : %s\n", mysql_error(connection));
      exit(1);
    }
    return connection;
}
 
/**
 * Performs the query.
 * This function perform the SQL quert and returns a pointer of MYSQL_RES
 */
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
   // send the query to the database
   if (mysql_query(connection, sql_query))
   {
      printf("MySQL query error : %s\n", mysql_error(connection));
      exit(1);
   }
 
   return mysql_use_result(connection);
}
 
/**
 * Main function
 */
int main()
{
  MYSQL *conn;  // the connection
  MYSQL_RES *res; // the results
  MYSQL_ROW row; // the results row (line by line)
 
  struct connection_details mysqlD;
  mysqlD.server = "localhost";  // where the mysql database is
  mysqlD.user = "jikoni";  // the root user of mysql
  mysqlD.password = "ugalinakuku"; // the password of the root user in mysql
  mysqlD.database = "discounts"; // the databse to pick
 
  // connect to the mysql database
  conn = mysql_connection_setup(mysqlD);
 
  // assign the results return to the MYSQL_RES pointer
  res = mysql_perform_query(conn, "SELECT products.product,stores.name  FROM products, stores WHERE products.sto_id = stores.id");
 
  printf("\n\t Product -> Store:\n\n\n");
 
  //To traverse the results we use a while loop in C++ and use the mysql_fetch_row function from within the mysql library set
  //where a row is a type of MYSQL_ROW.
 
  while ((row = mysql_fetch_row(res)) !=NULL)
      printf("\t-> %s -> %s \n", row[0],row[1]);
 
  /* clean up the database result set */
  mysql_free_result(res);
  /* clean up the database link */
  mysql_close(conn);
 
  return 0;
}

Compiling the code

Compile this program is a bit tricky as we need to link to the mysql libraries and headers that we have  used within the program, in our case the  mysql.h.  Luckly for us we can make use of the mysql_config, you may need to install it via your package manager system if you do not have it already. I already had it installed.
mysql_config provides you with useful information for compiling your MySQL client and connecting it to MySQL.
See what information it has by simply running it.
?View Code SHELL
# mysql_config
Usage: /usr/bin/mysql_config [OPTIONS]
Options:
        --cflags         [-I/usr/include/mysql  -DBIG_JOINS=1  -fno-strict-aliasing   -DUNIV_LINUX -DUNIV_LINUX]
        --include        [-I/usr/include/mysql]
        --libs           [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient]
        --libs_r         [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient_r]
        --plugindir      [/usr/lib/mysql/plugin]
        --socket         [/var/run/mysqld/mysqld.sock]
        --port           [0]
        --version        [5.1.54]
        --libmysqld-libs [-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqld -ldl -lwrap -lrt]
So to compile it run
?View Code SHELL
# g++ -o cmysql $(mysql_config --cflags) cmysql.cpp $(mysql_config --libs)
cmysql.cpp: In function ‘int main()’:
cmysql.cpp:60:19: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:61:17: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:62:21: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:63:21: warning: deprecated conversion from string constant to ‘char*’
cmysql.cpp:69:129: warning: deprecated conversion from string constant to ‘char*’
Ignore the warring for now,  if there errors then fix them and run the executable, cmysql, that would have been generated. I will look into the warnings in time.
?View Code SHELL
# ./cmysql 
 
  Product -> Store:
 
 -> USB Flash Disk -> Brand Computers Ltd
 -> Dell 65-Watt 3-Prong Slim AC Adapter -> Brand Computers Ltd
 -> Dell Streak 7 Android Tablet -> Brand Computers Ltd
 -> Nokia 701 -> Brand Computers Ltd
 -> Dell Inspron 1240 -> Just Computers
 -> HP Photosmart D110A -> Just Computers
 -> Web Hosting -> Tri Labs Limited
 -> Bulk SMS -> Tri Labs Limited
 -> EVDO USB Modem -> Tri Labs Limited
 -> EVDO USB Modem -> Tri Labs Limited
 -> Joomla We Template -> Tri Labs Limited
 -> Night Gear -> Viatuzi
 -> Tech Sports Bra -> Viatuzi
 -> Women's Fiona Bra -> Viatuzi
 -> VRV Color Tank -> Viatuzi
I hope this help someone out there.