Database Support

Hi Nila ,
Please download the Source code present
https://sqlite.org/download.html
i.e sqlite-amalgamation-3190300.zip

  1. Make a folder structure for Sqlite3 as
    mkdir third-party
    cd third-party
    mkdir inc
    mkdir src
    mkdir lib

  2. Copy sqlite3.h in third-party/inc/
    sqlite3.c in third-party/src/

  3. Create a Makefile in path third-party/src/
    vi Makefile
    add following lines

    export TOOLCHAIN_DIR:=/opt/swi/y17-ext/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi
    GCC = ${TOOLCHAIN_DIR}/arm-poky-linux-gnueabi-gcc
    all:
    ${GCC} -c -Wall -Werror -fpic -lpthread -ldl sqlite3.c -o …/lib/sqlite3.so

  4. Check for sqlite3.so file in the path third-party/lib/

Now you can use the library in your App Bundles.


CREATING LEGATO APPLICATION USING SQLITE3

In Path apps/sample
mkdir testsql

The directory structure should contain

  1. third-party folder containing sqlite3 lib inc and src.
    2.Application Defination file testsql.adef

    executables:
    {
    testsql = (testsqlComp)
    }
    bundles:
    {
    file:
    {
    [r] third-party/sqlite3/lib/sqlite3.so /lib/
    }
    }
    processes:
    {
    run:
    {
    (testsql)
    }
    }

  2. Component directory testsqlComp

4.cd testsqlComp
a) Create a Component.cdef file here as shown below
vi Component.cdef
sources:
{
testsql.c
}

b) vi testsql.c

Dont forget to include
legato.h and sqlite3.h
5.
/**************************************************************************
*Function:COMPONENT_INIT
****************************************************************************/
COMPONENT_INIT
{
LE_INFO(“Sample to use Sqlite3 in Legato”);
//Implement functionalities
}
Snippets to read/Write data using sqlite3
make sure you define macro with path where you want to create database
NEW_DATABASE “/data/sql.db”

void createDb()
{
		int rc ;
		rc = sqlite3_open(NEW_DATABASE, &db);
		if(!rc){
           		LE_INFO("Successfully Created ");
       		}
		else {
          		LE_INFO("Error in Creating/Opening Db file");
     		}
}

//This function is for creating Table
void initOperation()
{
char *sql;
char *zErrMsg = 0;
createDb();
if(db)
{
LE_INFO(“InitOperations Command For Table Creation”);
sql = “create table if not exists test_sqlite(ID int Primary\ key, valuename text , value int)”;
int rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if(!rc){
LE_INFO(“Executed Command For Table Creation”);
}
}
}

//Inserting Data into sql.db

	void insertData(sqlite3 *db, int farg, const char *name, int val)
	{
			if (!db)
			return;
			LE_INFO("Insert Command");
			const char *zErrMsg ;
			sqlite3_stmt *stmt;
			char *SQL;
		// Insert data item into myTable
			SQL = "insert into test_sqlite(ID,valuename,value)\
           		values (?,?,?)";
			int rc = sqlite3_prepare(db, SQL, strlen(SQL), &stmt, &zErrMsg);
			if( rc == SQLITE_OK ) 
		{
				// bind the value
  				sqlite3_bind_int(stmt, 1, farg);
  				sqlite3_bind_text(stmt, 2, name, strlen(name), 0);
  				sqlite3_bind_int(stmt, 3, val);
  				// commit
  				sqlite3_step(stmt);
  				sqlite3_finalize(stmt);
 			}   
	}

//display data

	void selectAll()
	{
		char *sql;
		char *zErrMsg = 0;
  			int rc ;
  			createDb();
  			LE_INFO("Display All **************************");
  			sql = "select * from test_sqlite";
  			rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
  			if(rc != SQLITE_OK)
   			 {
      			LE_INFO("Error Executing Command--No Record to show");
    		}
    		else
    		{
            		LE_INFO("Select is success");
    		}
    		closeDB();
	}

//Callback required by sqlite3_exec in selectAll

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
LE_INFO(“Insert Command”);
int i;
for(i=0; i<argc; i++)
{
fprintf(stdout,"%s = %s\n", azColName[i], argv[i] ? argv[i] : “NULL”);
}
return 0;
}

building application is explained in the previous reply.

Regards
Ankita

1 Like