
mfsql Library - Version: 1.0.6
mfsql is a simple way to access and use SQLite/MariaDB/PostgreSQL databases via c++ interface
mfsql is distributed under the BSD 3-Clause License, see copying for details
git:
git clone https://gitlab.com/m8618/mfsql
To use
Create an object for the type of database you need
e.g.
#include <mfsql.h>
mfsql::mfsqlite DBobject;
mfsql::mfmariadb DBobject;
mfsql::mfpostgre DBobject;
use the appropriate open function
query the database
Interface
// the open functions throw a mfsql::mfdberror on failure
void mfsqlite::open(const std::string& path_dbname);
void mfmariadb::open(const std::string& dbname_server, const std::string& DBname, const std::string& DBuser, const std::string& DBpassw, short int port = 3306 /* 3306 --> mysql standard port number */);
void mfpostgre::open(const std::string& dbname_server, const std::string& DBname, const std::string& DBuser, const std::string& DBpassw, short int port = 5432 /* 5432 --> postgre standard port number */);
// close database connection and free internal library memory - object can be re-used with further open()'s
int close();
// run a sql statement
bool query(const std::string& sql_statement, std::string& errmsg, int& errorcode);
// get the column names for a table
int tablefieldnames(const std::string& tablename, std::vector<std::string>& fieldnames, std::string& errmsg);
// get last error info
int error(std::string& errmsg);
// get underlying database pointer
// each database has its own pointer type
// SQLite --> sqlite3*
// MariaDB --> MYSQL*
// PostgreSQL --> PGconn*
sqlite3* || MYSQL* || PGconn*
getdb() const;
EXAMPLES
connect to a database and make a query
#include <iostream>
#include <mfsql.h>
int main() {
mfsql::mfsqlite DB;
try {
DB.open("sqlite_testdb.db");
}
catch(mfsql::mfdberror err) {
std::cerr << err.what() << std::endl;
return 0;
}
int rc(0);
std::vector<std::vector<std::string> > data;
std::string err_msg;
// just display all asset table data
std::string sql("select * from asset;");
if(DB.query(sql, err_msg, rc)) {
DB.getquerydata(data);
for(std::vector<std::vector<std::string> >::iterator it = data.begin(); it != data.end(); ++it) {
for(std::vector<std::string>::iterator yt = it->begin(); yt != it->end(); ++yt) {
std::cout << "'" << *yt << "' ";
}
std::cout << "\n";
}
}
else
std::cerr << "SQL error:" << rc << " error string:" << err_msg << "\n";
return 0;
}
compilation of above code example if this was a file named codeexample.cpp
g++ codeexample.cpp `pkg-config --libs --cflags mfsql-1.0.6` -o theprogram
static compilation example
g++ codeexample.cpp -I/usr/local/include/mfsql-1.0.6 /usr/local/lib/libmfsql.a -lsqlite3 -o theprogram