This directory contains comprehensive Python examples demonstrating how to use the AgentFlare AI SQLite Graph Database Extension from Python applications.
- Python 3.6+ with sqlite3 module (built-in)
- Compiled graph extension (libgraph.so)
- Optional: Jupyter notebook for interactive examples
Start with the basic example to verify everything works:
cd examples
python simple_graph_example.pyThis example shows:
- Loading the extension
- Creating nodes and edges
- Running basic graph algorithms
- Testing Cypher query parsing
Run the full feature demonstration:
python python_examples.pyThis covers:
- All SQL functions available
- Graph algorithms and analysis
- Cypher query operations
- Write operations (CREATE, MERGE, SET, DELETE)
- Performance testing
- Social network modeling
See a real-world use case with the recommendation system:
python recommendation_system.pyThis demonstrates:
- Building a movie recommendation graph
- User similarity analysis
- Movie recommendations based on graph algorithms
- Popularity analysis
Launch the Jupyter notebook for an interactive experience:
jupyter notebook graph_database_tutorial.ipynb| File | Description | Complexity |
|---|---|---|
simple_graph_example.py |
Basic operations and setup | Beginner |
python_examples.py |
Comprehensive feature demo | Intermediate |
recommendation_system.py |
Real-world application | Advanced |
graph_database_tutorial.ipynb |
Interactive tutorial | All levels |
The SQLite Graph Extension provides these SQL functions:
-- Node and edge management
SELECT graph_node_add(node_id, properties_json);
SELECT graph_edge_add(from_id, to_id, type, properties_json);
SELECT graph_count_nodes();
SELECT graph_count_edges();-- Path finding and analysis
SELECT graph_shortest_path(from_id, to_id);
SELECT graph_degree_centrality(node_id);
SELECT graph_is_connected();
SELECT graph_density();
SELECT graph_has_cycle();-- Query parsing and validation
SELECT cypher_parse(query_string);
SELECT cypher_validate(query_string);
SELECT cypher_execute(query_string);-- Transaction management
SELECT cypher_begin_write();
SELECT cypher_commit_write();
SELECT cypher_rollback_write();
-- Node and relationship operations
SELECT cypher_create_node(id, labels, properties);
SELECT cypher_merge_node(id, labels, match_props, create_props);
SELECT cypher_set_property(type, id, property, value);
SELECT cypher_delete_node(type, id, properties);import sqlite3
import json
import os
# Connect to database
conn = sqlite3.connect(":memory:") # or file path
conn.row_factory = sqlite3.Row
# Load extension
if os.path.exists("build/libgraph.so"):
conn.enable_load_extension(True)
conn.load_extension("build/libgraph.so")
cursor = conn.cursor()# Create a node with JSON properties
node_data = {"name": "Alice", "age": 30, "city": "NYC"}
cursor.execute("SELECT graph_node_add(?, ?) as result",
(1, json.dumps(node_data)))
result = cursor.fetchone()
print(f"Node created: {result['result']}")# Create relationship between nodes
edge_data = {"relationship": "FRIENDS", "since": "2020"}
cursor.execute("SELECT graph_edge_add(?, ?, ?, ?) as result",
(1, 2, "FRIENDS", json.dumps(edge_data)))
result = cursor.fetchone()
print(f"Edge created: {result['result']}")# Check if graph is connected
cursor.execute("SELECT graph_is_connected() as connected")
result = cursor.fetchone()
print(f"Graph connected: {bool(result['connected'])}")
# Find shortest path
cursor.execute("SELECT graph_shortest_path(?, ?) as path", (1, 2))
result = cursor.fetchone()
print(f"Shortest path: {result['path']}")
# Calculate centrality
cursor.execute("SELECT graph_degree_centrality(?) as centrality", (1,))
result = cursor.fetchone()
print(f"Node centrality: {result['centrality']}")try:
cursor.execute("SELECT graph_some_function() as result")
result = cursor.fetchone()
print(f"Success: {result['result']}")
except sqlite3.Error as e:
print(f"Error: {e}")- Model users, friendships, interactions
- Find mutual friends, influence analysis
- Recommend connections, detect communities
- User-item relationships with ratings
- Collaborative filtering using graph algorithms
- Content-based recommendations
- Entities and relationships modeling
- Semantic queries and inference
- Knowledge discovery
- Infrastructure modeling (servers, connections)
- Dependency analysis
- Failure impact assessment
- Transaction networks
- Pattern recognition
- Anomaly detection using graph metrics
- Batch Operations: Create multiple nodes/edges in transactions
- Index Usage: Leverage SQLite indexes for large graphs
- Memory Management: Use file-based databases for large datasets
- Algorithm Selection: Choose appropriate algorithms for your use case
# Check if extension file exists
import os
extension_path = "build/libgraph.so"
if not os.path.exists(extension_path):
print(f"Extension not found: {extension_path}")
print("Please compile the extension first")# Test if extension loaded correctly
try:
cursor.execute("SELECT graph_count_nodes()")
print("Extension loaded successfully")
except sqlite3.Error as e:
print(f"Extension error: {e}")# Use file database for large graphs
conn = sqlite3.connect("large_graph.db") # instead of ":memory:"- Experiment with the provided examples
- Modify the sample data for your use case
- Integrate with your existing applications
- Scale to larger datasets
- Contribute improvements to the project
- Check the main project documentation
- Review test files for additional examples
- Submit issues for bugs or feature requests
- Contribute improvements via pull requests
These examples are provided under the same license as the main AgentFlare AI SQLite Graph Extension project.
Part of the AgentFlare AI ecosystem