-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1485 from kuzudb/example
Add c, cpp examples
- Loading branch information
Showing
13 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Examples | ||
This directory contains examples of how to use the library. | ||
|
||
To use with CLion, add `add_subdirectory()` to the CMakeLists.txt file in the root directory of the project. | ||
E.g. to use cpp example | ||
``` | ||
add_subdirectory(examples/cpp) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cmake_minimum_required(VERSION 3.11) | ||
project(example-c) | ||
|
||
include_directories(../../src/include) | ||
link_directories(../../build/release/src) | ||
|
||
add_executable(example-c main.c) | ||
target_link_libraries(example-c kuzu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
|
||
#include "c_api/kuzu.h" | ||
|
||
int main() { | ||
kuzu_database* db = kuzu_database_init("" /* fill db path */, 0); | ||
kuzu_connection* conn = kuzu_connection_init(db); | ||
|
||
kuzu_query_result* result; | ||
// Create schema. | ||
result = kuzu_connection_query( | ||
conn, "CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));"); | ||
kuzu_query_result_destroy(result); | ||
// Create nodes. | ||
result = kuzu_connection_query(conn, "CREATE (:Person {name: 'Alice', age: 25});"); | ||
kuzu_query_result_destroy(result); | ||
result = kuzu_connection_query(conn, "CREATE (:Person {name: 'Bob', age: 30});"); | ||
kuzu_query_result_destroy(result); | ||
|
||
// Execute a simple query. | ||
result = kuzu_connection_query( | ||
conn, "MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;"); | ||
|
||
// Fetch each value. | ||
while (kuzu_query_result_has_next(result)) { | ||
kuzu_flat_tuple* tuple = kuzu_query_result_get_next(result); | ||
|
||
kuzu_value* value = kuzu_flat_tuple_get_value(tuple, 0); | ||
char* name = kuzu_value_get_string(value); | ||
kuzu_value_destroy(value); | ||
|
||
value = kuzu_flat_tuple_get_value(tuple, 1); | ||
int64_t age = kuzu_value_get_int64(value); | ||
kuzu_value_destroy(value); | ||
|
||
printf("name: %s, age: %lld \n", name, age); | ||
free(name); | ||
kuzu_flat_tuple_destroy(tuple); | ||
} | ||
|
||
// Print query result. | ||
char* result_string = kuzu_query_result_to_string(result); | ||
printf("%s", kuzu_query_result_to_string(result)); | ||
free(result_string); | ||
|
||
kuzu_query_result_destroy(result); | ||
kuzu_connection_destroy(conn); | ||
kuzu_database_destroy(db); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
cmake_minimum_required(VERSION 3.11) | ||
project(example-cpp) | ||
|
||
set(CMAKE_CXX_STANDARD 20) | ||
|
||
include_directories(../../src/include) | ||
link_directories(../../build/release/src) | ||
|
||
add_executable(example-cpp main.cpp) | ||
target_link_libraries(example-cpp kuzu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
|
||
#include "main/kuzu.h" | ||
using namespace kuzu::main; | ||
|
||
int main() { | ||
auto database = std::make_unique<Database>("" /* fill db path */); | ||
auto connection = std::make_unique<Connection>(database.get()); | ||
|
||
// Create schema. | ||
connection->query("CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));"); | ||
// Create nodes. | ||
connection->query("CREATE (:Person {name: 'Alice', age: 25});"); | ||
connection->query("CREATE (:Person {name: 'Bob', age: 30});"); | ||
|
||
// Execute a simple query. | ||
auto result = connection->query("MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE;"); | ||
// Print query result. | ||
std::cout << result->toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters