# Database Support

**URL:** https://mangoh.discourse.group/t/database-support/93
**Category:** Uncategorized
**Created:** [June 2, 2016, 6:29am UTC](https://mangoh.discourse.group/t/database-support/93 "2016-06-02T06:29:02Z")
**Posts on this page:** 1
**Showing post:** 15

<div class="post-metadata">

### Author: ![Ankita](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Ankita](https://mangoh.discourse.group/u/Ankita)
#### Post date: [June 20, 2017, 1:19pm UTC](https://mangoh.discourse.group/t/database-support/93/15 "2017-06-20T13:19:23Z")

</div>

Hi Nila ,  
Please download the Source code present  
[https://sqlite.org/download.html](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

4. Check for [sqlite3.so](http://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

2. Component directory testsqlComp

[4.cd](http://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

---

_[View the full topic](https://mangoh.discourse.group/t/database-support/93)._
