Changes between Version 1 and Version 2 of Docs/Prog/Manual/ApplicationLibraries/Database


Ignore:
Timestamp:
02/01/13 14:32:23 (12 years ago)
Author:
Don Wilson
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Docs/Prog/Manual/ApplicationLibraries/Database

    v1 v2  
    1 [[TOC(heading=Table of Contents, Docs/Prog/*)]] [[PageOutline]]
    2 
    31= Database =
    4 == About SQLite ==
    5 The Cardinal 825 comes fully equipped with an open source relational database known as SQLite. SQLite is a software library that implements a [http://sqlite.org/selfcontained.html self-contained], [http://sqlite.org/serverless.html serverless], [http://sqlite.org/zeroconf.html zero-configuration], [http://sqlite.org/transactional.html transactional] SQL database engine. SQLite is the [http://sqlite.org/mostdeployed.html most widely deployed] SQL database engine in the world. The source code for SQLite is in the [http://sqlite.org/copyright.html public domain]. SQLite is used by companies such as Adobe, Bloomberg, mozilla, symbian, Skype, and Google (Android OS).
    6 
    7 == SQLite Library (libsql) ==
    8 Cardinal supplies a library that allows usage of the SQLite database as well as a wrapper class to make generating queries and using the database as easy as possible. You can get the latest library from [wiki:/Libs/libsql this wiki] or by [wiki:/Docs/Prog/Manual/Subversion checking out the latest source] from the repository.
    9 
    10 == Using libsql ==
    11 Once you have the latest source you will need to include it in your project by modifying your project settings. Follow the steps listed below to get up and running.
    12 
    13 1. Right-click on your project and choose '''properties''' from the drop-down menu.
    14 
    15   [[Image(screenshot.1.jpeg)]]
    16 
    17 2. Choose '''C/C++ Build''' and then '''Settings''' from the next screen. Click on '''Directories''' under '''Sourcery G++ C++ Compiler'''.
    18 
    19   [[Image(screenshot.2.jpeg)]]
    20 
    21 3. Click on the add button and choose '''Workspace''' and the libsql project from the popup.
    22 
    23   [[Image(screenshot.3.jpeg)]]
    24 
    25 4. Click '''OK'', '''OK''' and then observed the Include paths. It should have '''${workspace_loc/libsql}'''.'''''
    26 
    27   [[Image(screenshot.4.jpeg)]]
    28 
    29 5. Next, click on '''Libraries''' under '''Sourcery G++ C++ Linker'''.
    30 
    31   [[Image(screenshot.5.jpeg)]]
    32 
    33 6. In the '''Libraries (-l)''' pane choose the add button and enter '''pthread'''. Click the add button again and enter '''sql'''. If you're using the lib825 (and you probably are) that pane should look like the screenshot below.
    34 
    35   [[Image(screenshot.6.jpeg)]]
    36 
    37 7. In the '''Library search path (-L)''' pane click the green add and choose '''Workspace'''. Pick libsql/Debug (or Release for release build) from the dropdown. If you do not have Debug you can either build the debug release of the libsql or manually enter /Debug after the libsql path. The final result should be identical to the image shown below.
    38 
    39   [[Image(screenshot.7.jpeg)]]
    40 
    41 8. The '''Library search path (-L)''' should be identical to the image below (Release may say Debug if you're building a Debug build).
    42 
    43   [[Image(screenshot.8.jpeg)]]
    44 
    45 9. Click '''OK''' to save your settings.
    46 
    47 == SqlBuilder Class ==
    48 A query builder class has been included with the SQLite library to facilitate generation of queries. Using the class is recommended to eliminate query bugs (and debugging time). Below you can find examples of using the class.
    49 
    50 {{{
    51 #!Lineno
    52 #!c
    53 
    54 // LibSQL Headers
    55 #include "SqlBuilder.h"
    56 
    57 int main(int ac, char **av) {
    58     SqlBuilder Sql;
    59        
    60     // Example of a simple query
    61     Sql.Select("name");
    62     Sql.From("customers");
    63     Sql.Print();
    64     Sql.Reset();
    65 
    66     // Example of using sorting
    67     Sql.Select("id");
    68     Sql.From("trucks");
    69     Sql.Sort(SQL_ORDER_ASC,"name");
    70     Sql.Print();
    71     Sql.Reset();
    72        
    73     // Example of using multiple arguments with a where clause
    74     Sql.Select("name","value");
    75     Sql.From("configuration");
    76     Sql.Where("name LIKE 'Thomas'", "id > 0");
    77     Sql.Print();
    78     Sql.Reset();
    79        
    80     // Most complicated example, multiple arguments, multiple sorts, and multiple tables with aliases
    81     // This also does a simple inner join and then sorts by customer name, truck name, in descending order.
    82     Sql.Select("c.id,t.id,c.name,t.name");
    83     Sql.From("trucks", "t"); // trucks table aliased as t, so the id field would be t.id
    84     Sql.From("customers", "c");
    85     Sql.Where("c.id > 0", "t.id > 0", "t.name LIKE 'temp'", "c.name LIKE 'kyle'");
    86     Sql.Sort(SQL_ORDER_DESC,"c.name","t.name");
    87     Sql.Print();
    88     Sql.Reset();
    89 
    90     // Using values to bind to the where clause.
    91     int nTmp = 5;
    92     Sql.Select("test");
    93     Sql.From("trucks");
    94     Sql.Where("id =", nTmp);
    95     Sql.Print();
    96        
    97     return 0;
    98 }
    99 }}}
    100 
    101 Output:
    102 
    103 {{{
    104 # ./sqltest
    105 SqlBuilder :: Query Generated = [SELECT name FROM customers]
    106 SqlBuilder :: Query Generated = [SELECT id FROM trucks ORDER BY name ASC]
    107 SqlBuilder :: Query Generated = [SELECT name, value FROM configuration WHERE name LIKE 'Thomas' id > 0]
    108 SqlBuilder :: Query Generated = [SELECT c.id,t.id,c.name,t.name FROM trucks AS t, customers AS c WHERE c.id > 0 AND t.id > 0 AND t.name LIKE 'temp' AND c.name LIKE 'kyle' ORDER BY c.name, t.name DESC]
    109 SqlBuilder :: Query Generated = [SELECT test FROM trucks WHERE id = 5]
    110 }}}
    111 
    112 == SQLite++ Class ==
    113 A C++ wrapper has been provided that sits on top of the SQLite C functions and aims to make implementation easier. See below for examples.
    114 
    115 {{{
    116 #!Lineno
    117 #!c
    118 database db;
    119 
    120 // Headers
    121 #include "sq3.hpp"
    122 
    123 int main(int ac,char **av) {
    124     database db;
    125        
    126     // Executing a simple query
    127     db.execute("SELECT * FROM trucks");
    128        
    129     // Iterating through rows of a result set
    130     statement st(db, "SELECT * FROM trucks");
    131     cursor cur(st.get_cursor());
    132    
    133     while(cur.step() == SQLITE_ROW) {
    134         string name;
    135         float accum;
    136        
    137         // Do stuff with data
    138         cur.get(0,name);
    139         cur.get(1,accum);
    140     }
    141    
    142     // Doing multiple inserts using bind and transactions for speed
    143     bool bError = false;
    144    
    145     transaction tr(db);
    146     st.prepare("INSERT INTO trucks (name,accum) VALUES (?,?);");
    147    
    148     for(int i = 0; i < 10; i++) {
    149         st.bind(1,"truck!");
    150         st.bind(2,i*1000.0);
    151        
    152         if (st.execute() == SQLITE_ERROR) {
    153                 bError = true;
    154         }
    155     }
    156    
    157     if (bError) {
    158         tr.rollback();
    159     } else {
    160         tr.commit();
    161     }
    162 }
    163 }}}
    164 
    165 == Using SQLite++ with SqlBuilder ==
    166 You can easily combine SQLite++ and SqlBuilder to make database access easy.
    167 
    168 {{{
    169 #!Lineno
    170 #!c
    171 
    172 // LibSQL Headers
    173 #include "sq3.hpp"
    174 #include "SqlBuilder.h"
    175 
    176 int main(int ac, char **av) {
    177     database db;
    178     SqlBuilder sql;
    179 
    180     // Identical to the select above
    181     sql.Select("*");
    182     sql.From("trucks");
    183     db.execute(sql.GetQuery());
    184 
    185     // Grab a specific row instead of all results
    186     // Gets the name of truck id 10
    187     sql.Select("name");
    188     sql.From("trucks");
    189     sql.Where("id =",10);
    190     db.execute(sql.GetQuery());
    191 }
    192 }}}