just_database 0.0.1 copy "just_database: ^0.0.1" to clipboard
just_database: ^0.0.1 copied to clipboard

A pure-Dart SQL database engine with in-memory storage and optional file persistence.

example/example.dart

import 'package:just_database/just_database.dart';

/// Example demonstrating basic usage of just_database
Future<void> main() async {
  print('=== just_database Example ===\n');

  // Create a database
  print('Creating database...');
  final db = await JustDatabase.open('example_db', mode: DatabaseMode.standard);

  // Create a table
  print('Creating users table...');
  await db.execute('''
    CREATE TABLE users (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      email TEXT UNIQUE,
      age INTEGER,
      active BOOLEAN DEFAULT true
    )
  ''');

  // Insert some data
  print('Inserting users...');
  await db.execute(
    "INSERT INTO users (name, email, age) VALUES ('Alice Johnson', 'alice@example.com', 28)",
  );
  await db.execute(
    "INSERT INTO users (name, email, age) VALUES ('Bob Smith', 'bob@example.com', 34)",
  );
  await db.execute(
    "INSERT INTO users (name, email, age, active) VALUES ('Carol White', 'carol@example.com', 25, false)",
  );

  // Query all users
  print('\nQuerying all users:');
  var result = await db.execute('SELECT * FROM users');
  for (var row in result.rows) {
    print('  ${row['name']} (${row['age']}) - ${row['email']}');
  }

  // Query with WHERE clause
  print('\nActive users over 25:');
  result = await db.execute(
    'SELECT * FROM users WHERE age > 25 AND active = true',
  );
  for (var row in result.rows) {
    print('  ${row['name']} - Age: ${row['age']}');
  }

  // Update a record
  print('\nUpdating Carol to active...');
  await db.execute("UPDATE users SET active = true WHERE name = 'Carol White'");

  // Create products table with foreign key
  print('\nCreating products and orders tables...');
  await db.execute('''
    CREATE TABLE products (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      price REAL NOT NULL,
      stock INTEGER DEFAULT 0
    )
  ''');

  await db.execute('''
    CREATE TABLE orders (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      user_id INTEGER NOT NULL,
      product_id INTEGER NOT NULL,
      quantity INTEGER NOT NULL,
      FOREIGN KEY (user_id) REFERENCES users(id),
      FOREIGN KEY (product_id) REFERENCES products(id)
    )
  ''');

  // Insert products
  await db.execute(
    "INSERT INTO products (name, price, stock) VALUES ('Laptop', 999.99, 10)",
  );
  await db.execute(
    "INSERT INTO products (name, price, stock) VALUES ('Mouse', 29.99, 50)",
  );

  // Insert orders
  await db.execute(
    "INSERT INTO orders (user_id, product_id, quantity) VALUES (1, 1, 1)",
  );
  await db.execute(
    "INSERT INTO orders (user_id, product_id, quantity) VALUES (2, 2, 2)",
  );

  // Check schema
  print('\nDatabase tables:');
  for (var tableName in db.tableNames) {
    final schema = db.getTableSchema(tableName);
    print('  $tableName (${schema!.columns.length} columns)');
  }

  // Note: Direct table access for composite indexes is internal
  // In this example, foreign key indexes are automatically created
  print('\nAutomatic indexes created for foreign keys');

  // Delete a record
  print('\nDeleting a user...');
  await db.execute("DELETE FROM users WHERE name = 'Carol White'");

  // Final count
  result = await db.execute('SELECT * FROM users');
  print('\nFinal user count: ${result.rows.length}');

  print('\n=== Example Complete ===');
}
4
likes
150
points
190
downloads

Publisher

verified publisherjustunknown.com

Weekly Downloads

A pure-Dart SQL database engine with in-memory storage and optional file persistence.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter, path_provider, provider

More

Packages that depend on just_database