본문 바로가기
WIKI 기술 지식 베이스

Dart로 Turso 연결하기

원문 보기 위키 갱신

Dart로 Turso 연결하기 (Connect to Turso using Dart)

Dart에서도 Turso 데이터베이스를 바로 쓸 수 있어요. 설치부터 테이블 생성, 데이터 삽입, 쿼리까지 차근차근 따라 해 보세요.

출처: 문서

본문

1. 설치

turso_dart 패키지를 pubspec.yaml에 추가하세요:

dependencies:
  turso_dart:

그리고 실행하세요:

dart pub get

2. 연결

클라이언트 연결을 만들어요. 인메모리 데이터베이스나 로컬 파일에 연결할 수 있어요:

import 'package:turso_dart/turso_dart.dart';

// 인메모리
final client = TursoClient.memory();

// 또는 로컬 파일
final client = TursoClient.local('/path/to/local.db');

await client.connect();

3. 테이블 생성

고객(customers) 테이블을 만들어 보세요:

await client.execute(
  "CREATE TABLE IF NOT EXISTS customers (id INTEGER PRIMARY KEY, name TEXT)"
);

4. 데이터 삽입

customers 테이블에 데이터를 넣어 보세요:

await client.query("INSERT INTO customers(name) VALUES ('John Doe')");
await client.query("INSERT INTO customers(name) VALUES ('Jane Smith')");

5. 데이터 조회

테이블의 모든 고객을 조회해요:

final result = await client.query("SELECT * FROM customers");
print(result);

6. Prepared statements

더 나은 성능과 보안을 위해 prepared statements를 사용하세요:

final statement = await client.prepare("SELECT * FROM customers WHERE id = ?");
final result = await statement.query(positional: [1]);
print(result);

더 알아보기 (Learn more)