Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to Connect to DuckDB with C++
Marcos Oliveira
Marcos Oliveira

Posted on • Edited on

     

How to Connect to DuckDB with C++

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
Enter fullscreen modeExit fullscreen mode

Unzip:

unzip libduckdb-linux-amd64.zip-d libduckdb-linux-amd64
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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);}
Enter fullscreen modeExit fullscreen mode

Compile and run:

g++ main.cpp libduckdb.soLD_LIBRARY_PATH=. ./a.out
Enter fullscreen modeExit fullscreen mode

Probable output:

1 | Marcos Oliveira2 | Mark Raasveldt3 | Hannes Muhleisen
Enter fullscreen modeExit fullscreen mode

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/
Enter fullscreen modeExit fullscreen mode
  • 02. Move thedynamic library tolib> And update the system's shared library cache.
sudo cplibduckdb.so /usr/local/lib/sudoldconfig
Enter fullscreen modeExit fullscreen mode

After that, just test with the flag and you don't even need to be in the folder with the downloaded files or use theLD_LIBRARY_PATH environment variable:

But you needterminalroot.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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

Save with:

sudo mkdir-p /usr/local/lib/pkgconfigsudovim /usr/local/lib/pkgconfig/duckdb.pc# paste the above content
Enter fullscreen modeExit fullscreen mode

Refresh the cache:

exportPKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

If you also have problems, check for missing dependencies:

nm libduckdb_static.a |grep" U "
Enter fullscreen modeExit fullscreen mode

("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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

EN: https://terminalroot.com/BR: https://terminalroot.com.br/
  • Location
    Brasil, Paraná
  • Joined

More fromMarcos Oliveira

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp