
A fast and futuristic database for your projects.
We have already written an initial article aboutDuckDB that explains initial concepts, installation and first steps. For more information, access the link:
In this article, we will see how to connect to DuckDB using the API withC++
01. Download the lib and create a basic code
TheDuckDB API for C++ is not yet stable, on the API page itself there is this warning:
DuckDB's C++ API is internal. It is not guaranteed to be stable and can change without notice. If you would like to build an application on DuckDB, we recommend using the C API.
That's why they recommend that you use theCAPI which works perfectly. To do this, just access the page:https://duckdb.org/docs/installation/ and choose the data according to your system. In my case, I chose:
Then just click on the.zip
that will be made available and download it, or download it withwget:
wget-q https://github.com/duckdb/duckdb/releases/download/v1.3.1/libduckdb-linux-amd64.zip
Unzip:
unzip libduckdb-linux-amd64.zip-d libduckdb-linux-amd64
Create a database and table, example:duckdb terminalroot.db
CREATETABLEterminalroot(idINTEGER,nameSTRING);INSERTINTOterminalrootVALUES(1,'Marcos Oliveira');INSERTINTOterminalrootVALUES(2,'Mark Raasveldt');INSERTINTOterminalrootVALUES(3,'Hannes Muhleisen');SELECT*FROMterminalroot;.exit
Go into the directory you unzipped and create some basic code:cd libduckdb-linux-amd64/ && vim main.cpp
#include"duckdb.h"#include<iostream>intmain(){duckdb_databasedb;duckdb_connectioncon;duckdb_resultresult;if(duckdb_open("terminalroot.db",&db)==DuckDBError){std::cerr<<"Error opening database\n";return1;}if(duckdb_connect(db,&con)==DuckDBError){std::cerr<<"Error connecting to database\n";return1;}if(duckdb_query(con,"SELECT * FROM terminalroot;",&result)==DuckDBError){std::cerr<<"Error executing SELECT\n";duckdb_disconnect(&con);duckdb_close(&db);return1;}for(idx_trow=0;row<=result.deprecated_column_count;row++){intid=duckdb_value_int32(&result,0,row);constchar*name=duckdb_value_varchar(&result,1,row);std::cout<<id<<" | "<<name<<'\n';duckdb_free((void*)name);// free varchar memory}duckdb_destroy_result(&result);duckdb_disconnect(&con);duckdb_close(&db);}
Compile and run:
g++ main.cpp libduckdb.soLD_LIBRARY_PATH=. ./a.out
Probable output:
1 | Marcos Oliveira2 | Mark Raasveldt3 | Hannes Muhleisen
Install on the system
If you want to install on your system and compile with the-lduckdb
flag, run the commands below:
- 01. Move
.h
toinclude
:
sudo mkdir-p /usr/local/include/duckdbsudo cpduckdb.h /usr/local/include/duckdb/
- 02. Move thedynamic library to
lib
> And update the system's shared library cache.
sudo cplibduckdb.so /usr/local/lib/sudoldconfig
After that, just test with the flag and you don't even need to be in the folder with the downloaded files or use the environment variable:LD_LIBRARY_PATH
But you need
terminalroot.db
, if you put the absolute path, e.g.:"/home/$USER/.db/terminalroot.db"
your binary will run regardless of where you are in the system!
g++ main.cpp-lduckdb./a.out
If you want to create.pc
forpkg-config
, expand the procedure below:
Create the file/usr/local/lib/pkgconfig/duckdb.pc
with this content:
prefix=/usr/localexec_prefix=${prefix}libdir=${exec_prefix}/libincludedir=${prefix}/includeName: DuckDBDescription: DuckDB embedded databaseVersion: 1.2.1Libs: -L${libdir} -lduckdbCflags: -I${includedir}/duckdb
Save with:
sudo mkdir-p /usr/local/lib/pkgconfigsudovim /usr/local/lib/pkgconfig/duckdb.pc# paste the above content
Refresh the cache:
exportPKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
I tried the static librarylibduckdb_static.a
, but had problems, but if you prefer, try:
g++ main.cpp libduckdb_static.a-I.-ldl-pthread-lm-lz-static-libstdc++-static-libgcc
If you also have problems, check for missing dependencies:
nm libduckdb_static.a |grep" U "
(
"U"
=undefined symbol)
Or useldd ./a.out
to see if there are still dynamic libs hanging.
In my case none of these steps worked, but see if they apply to you too.
Useful links
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse