/sqlcpp

A multithreaded sqlite implementation that injects data to the DB in a detached thread and waits for it to finish before the class exits

Primary LanguageC++

sqlcpp

SETUP for VISUAL STUDIO 2017

Making it Run with CHAI-3D

  1. Copy DBManager.cpp / DBManager.h to the project folder from https://github.com/sandeepzgk/sqlcpp
  2. Right Click the application > Click on Add > Add Existing Items and select the DBManager.cpp and DBManager.h files
  3. Download SQLITE DLL and def from the link https://sqlite.org/download.html "Precompiled Binaries for Windows" get both x86 and win64 versions.
  4. Create SQLITE import library from the downloaded sqlite3.def file. for both 32 and 64 bits (needed for different compiler options for chai3d / vs applications) Use "DEVELOPER COMMAND PROMPT FOR VS"
  5. Ideally the the dll's should be extracted to a folder say "c:\sqlite" and then run the command lib /DEF:"C:\sqlite\sqlite-dll-win64\sqlite3.def" /OUT:"c:\sqlite\sqlite3-32.lib" in the DEVELOPER COMMAND PROMPT FOR VS This will generate the 32 bit lib files.
  6. Make the 64 bit libs files using /MACHINE toggle like this lib /DEF:"C:\sqlite\sqlite-dll-win64\sqlite3.def" /OUT:"c:\sqlite\sqlite3.lib" /MACHINE:X64, Ensure the names are different for both the 32 bit and 64 bit.
  7. To include sqlite.h in the Vs project download source code for sqlite3 from https://sqlite.org/download.html "sqlite-amalgamation-xxxxxxx.zip" Extract the zip file and copy sqlite3.h to a directory say for eg:"c:\sqlite" also copy generated sqlite3.lib (in step-2) and sqlite3.dll to the same directory.
  8. In visual studio in your code add the appropriate folders
  9. Set additional include directory to sqlite.h file directory. for this ,in the Vs solution explorer take project properties and go to. Configuration Properties–>C++–>General–>set additional directories set it as the directory in which sqlite3.h copied for eg: "c:\sqlite"
  10. Now set sqlite.lib as linker input. for this ,in the Vs solution explorer take project properties and go to. Configuration Properties–>Linker–>Input and add sqlite3.lib to it. Ensure you have either 32 or 64 bit as you created and named.
  11. Set sqlite3.lib library directory as additional library for this in the Vs solution explorer take project properties and go to. Configuration Properties–>Linker–>General–>Additional Library Directories set it , for eg:"c:\sqlite"

HOW TO USE THIS

  1. In your main file, include the header file #include "DBManager.h"
  2. Get an instance of the class by DBManager *dbmanager = DBManager::GetInstance();
  3. There are two writes possible with this class, 3a. dbmanager->writeMainData(5.5, "P1", "A", 3, 2, "Y"); 3b. dbmanager->writeSRData("P1",1,12); 3c. dbmanager->writeForceData("Og81",12,1.24,15.55,12.44)
  4. These writes will add values to the three different tables in the database.

A working example of this code is available at https://github.com/sandeepzgk/chai3d-experiments/tree/SQLIntegration_Test SQLIntegration_Test as a part of the chai3d-experiements repository.

Instructions adapted from : https://cppcodetips.wordpress.com/tag/including-sqlite-with-visual-studio/

ADDITIONAL IMPORTANT NOTES

  • The code now implements singleton pattern and you can get the instance anywhere
  • The filename for the db is test.db which can be changed to anything else
  • There is no delay in startup of the code
  • this uses detachable threads to write data to the db and only ONE write thread is available
  • Before exiting the application just check if isBusy function is returning true or false, if its busy please DO NOT EXIT
  • the write call should immediately return control to the main thread, as it has already spawn a write thread to the DB which will kill itself after execution.

EXPLORING THE DB

  • Use https://sqlitebrowser.org/ this is a pretty good tool to explore the sqlite database
  • You will see two tables MAINDATA and SRDATA tables that contains the data entered by you.

SETUP for UBUNTU

  • sudo apt-get install libsqlite3-dev
  • sudo apt-get install sqlite3
  • use cmake to build the makefile and make that should get you the executable. https://github.com/sandeepzgk/sqlcpp