reading data from the database basics
This commit is contained in:
Binary file not shown.
53
src/main.c
53
src/main.c
@@ -1,4 +1,5 @@
|
|||||||
#include <duckdb.h>
|
#include <duckdb.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@@ -9,20 +10,66 @@ int main(int argc, char** argv){
|
|||||||
printf("You need to specify a duckdb file path! Terminating!\n");
|
printf("You need to specify a duckdb file path! Terminating!\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
printf("First argument: %s", argv[1]);
|
printf("First argument: %s\n", argv[1]);
|
||||||
|
|
||||||
duckdb_database db;
|
duckdb_database db;
|
||||||
if(duckdb_open(argv[1], &db) == DuckDBError){
|
if(duckdb_open(argv[1], &db) == DuckDBError){
|
||||||
printf("Error opening duckdb file, terminating!");
|
printf("Error opening duckdb file, terminating!\n");
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
duckdb_connection conn;
|
duckdb_connection conn;
|
||||||
if(duckdb_connect(db, &conn) == DuckDBError){
|
if(duckdb_connect(db, &conn) == DuckDBError){
|
||||||
printf("Error connecting to the duckdb database! Terminating!");
|
printf("Error connecting to the duckdb database! Terminating!\n");
|
||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Test query
|
||||||
|
duckdb_result result;
|
||||||
|
duckdb_query(conn, "SELECT * from \"Engine RPM\";\n", &result);
|
||||||
|
int32_t iterations = 0;
|
||||||
|
int32_t number_of_data_points = 0;
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
iterations++;
|
||||||
|
|
||||||
|
duckdb_data_chunk data_chunk = duckdb_fetch_chunk(result);
|
||||||
|
|
||||||
|
if(!data_chunk){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
idx_t row_count = duckdb_data_chunk_get_size(data_chunk); //number of rows from the data chunk
|
||||||
|
|
||||||
|
//first column
|
||||||
|
duckdb_vector col1 = duckdb_data_chunk_get_vector(data_chunk, 0);
|
||||||
|
int32_t *col1_data = (int32_t *) duckdb_vector_get_data(col1);
|
||||||
|
uint64_t *col1_validity = duckdb_vector_get_validity(col1);
|
||||||
|
|
||||||
|
for (idx_t row = 0; row < row_count; row++) {
|
||||||
|
number_of_data_points++;
|
||||||
|
if(duckdb_validity_row_is_valid(col1_validity, row)){
|
||||||
|
printf("%d, ", col1_data[row]);
|
||||||
|
}else{
|
||||||
|
printf("NULL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
duckdb_destroy_data_chunk(&data_chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nNumber of iterations: %d, number of data points: %d\n", iterations, number_of_data_points);
|
||||||
|
|
||||||
|
printf("Shutting down HardCompound!\n");
|
||||||
|
|
||||||
|
printf("Destroying result!\n");
|
||||||
|
duckdb_destroy_result(&result);
|
||||||
|
|
||||||
|
printf("Closing database and any connection.\n");
|
||||||
|
duckdb_disconnect(&conn);
|
||||||
|
duckdb_close(&db);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user