Flutter图数据库交互插件neo4j_http_client的使用

Flutter图数据库交互插件neo4j_http_client的使用

特性

  • 发现API
  • 查询执行
  • 事务
  • 认证和授权

安装

Flutter

flutter pub add neo4j_http_client

Dart

dart pub add neo4j_http_client

使用

连接并发送单个查询

import 'package:neo4j_http_client/neo4j_http_client.dart';

final client = Client(
  url: Uri.parse('http://localhost:7474/db/data'), // Neo4j服务器地址
  database: 'test', // 数据库名称
  username: 'username', // 用户名
  password: 'password', // 密码
);

final query = Query('MATCH (n) RETURN n LIMIT 10'); // 查询语句
final response = await client.execute([query]); // 执行查询
client.close(); // 关闭客户端连接

print(response.results); // 打印查询结果

发送多个查询

import 'package:neo4j_http_client/neo4j_http_client.dart';

final client = Client(
  url: Uri.parse('http://localhost:7474/db/data'),
  database: 'test',
  username: 'username',
  password: 'password',
);

final query = Query.fromMap({
  'MATCH (n) RETURN n LIMIT $limit': {'limit': 5}, // 第一个查询
  'MATCH (n) RETURN n SKIP $skip LIMIT $limit': {'skip': 5, 'limit': 5}, // 第二个查询
});
final response = await client.execute([query]);
client.close();

print(response.results);

事务

import 'package:neo4j_http_client/neo4j_http_client.dart';

final client = Client(
  url: Uri.parse('http://localhost:7474/db/data'),
  database: 'test',
  username: 'username',
  password: 'password',
);

final transaction = await client.beginTransaction(); // 开始事务
final query = Query('CREATE (n:Person {name: "John"}) RETURN n'); // 创建节点
await transaction.execute([query]);

// 提交事务
await transaction.commit();

// 或回滚事务
// await transaction.rollback();

client.close();

获取数据并将其转换为记录列表

final query = Query('MATCH (n:Person {name: "Alice"}) RETURN n');
final response = await client.execute([query]);
client.close();
final result = response.results.first;

// 将结果转换为记录列表
final records = result.toRecords();
for (final record in records) {
  final node = record['n'];
  print('Name: ${node['name']}, Age: ${node['age']}');
}

records 变量将包含以下格式的数据:

[
  {'n': {'name': 'Alice', 'age': 30}},
  // ...
]

更多关于Flutter图数据库交互插件neo4j_http_client的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图数据库交互插件neo4j_http_client的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


neo4j_http_client 是一个用于在 Flutter 应用中与 Neo4j 图数据库进行交互的插件。它通过 HTTP 或 HTTPS 协议与 Neo4j 数据库通信,允许你执行 Cypher 查询并处理返回的结果。

以下是如何在 Flutter 项目中使用 neo4j_http_client 的基本步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 neo4j_http_client 依赖:

dependencies:
  flutter:
    sdk: flutter
  neo4j_http_client: ^0.1.0 # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入库

在你的 Dart 文件中导入 neo4j_http_client

import 'package:neo4j_http_client/neo4j_http_client.dart';

3. 初始化客户端

创建一个 Neo4jClient 实例,并配置与 Neo4j 数据库的连接:

final client = Neo4jClient(
  baseUrl: 'https://your-neo4j-server:7473', // Neo4j 服务器的 URL
  username: 'your-username', // Neo4j 用户名
  password: 'your-password', // Neo4j 密码
);

4. 执行 Cypher 查询

使用 executeCypher 方法执行 Cypher 查询,并处理返回的结果:

void fetchData() async {
  final cypherQuery = '''
    MATCH (n:Person) 
    RETURN n.name AS name, n.age AS age
    LIMIT 10
  ''';

  try {
    final response = await client.executeCypher(cypherQuery);
    if (response.isSuccess) {
      for (var row in response.rows) {
        print('Name: ${row['name']}, Age: ${row['age']}');
      }
    } else {
      print('Error: ${response.error}');
    }
  } catch (e) {
    print('Exception: $e');
  }
}

5. 处理返回结果

executeCypher 方法返回一个 Neo4jResponse 对象,你可以通过 isSuccess 属性检查查询是否成功,并通过 rows 属性获取查询结果的每一行数据。

6. 关闭客户端

在不需要使用客户端时,关闭它以释放资源:

client.close();
回到顶部