SL-Pirate / dart_odbc

A Dart package for interacting with ODBC databases. It allows you to connect to ODBC data sources and execute SQL queries directly from your Dart applications.
MIT License
5 stars 1 forks source link
dart dart-library dart-package dartlang odbc odbc-api

dart_odbc

A Dart package for interacting with ODBC databases. It allows you to connect to ODBC data sources and execute SQL queries directly from your Dart applications.

This package is inspired by the obsolete odbc package by Juan Mellado.

style: very good analysis

Usage

  final odbc = DartOdbc(
    '/path/to/the/odbc/driver',
    version=SQL_OV_ODBC3_80 // optional
  );

Path to ODBC Driver

Path to the ODBC driver can be found in the ODBC driver manager. In windows this is a .dll file that is there in the installation folder of the ODBC driver. In linux this has an extension of .so. In macos this should have an extension of .dylib.

version

The ODBC version can be specified using the version parameter. Definitions for these values can be found in the LibODBC class. Please note that some drivers may not work properly with manually setting version.

  await odbc.connect(
    dsn: '<your_dsn>',
    username: 'db_username',
    password: 'db_password',
  );
  await odbc.connectWithConnectionString(
    "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\Users\Computer\AppData\Local\Temp\test.xlsx;"
  );

DSN (Data Source Name)

This is the name you gave when setting up the driver manager. For more information, visit this page from the MySQL Documentation

  final result = await odbc.execute("SELECT 10");

Executing prepared statements

  final List<Map<String, dynamic>> result = await odbc.execute(
    'SELECT * FROM USERS WHERE UID = ?',
    params: [1],
  );

Providing configuration for result set columns


  // Assume a table like this
  // +-----+-------+-------------+
  // | UID | NAME  | DESCRIPTION |
  // +-----+-------+-------------+
  // | 1   | Alice |             |
  // | 2   | Bob   |             |
  // +-----+-------+-------------+
  // The name is a column of size 150
  // The description is a column of size 500

  result = await odbc.execute(
    'SELECT * FROM USERS WHERE UID = ?',
    params: [1],

    /// The column config can be provided as this.
    /// But for most cases this config is not necessary
    /// This is only needed when the data fetching is not working as expected
    /// Only the columns with issues need to be provided
    columnConfig: {
      'NAME': ColumnType(size: 150),
      'DESCRIPTION': ColumnType(type: SQL_C_WCHAR, size: 500),
    },
  );

Get Tables

final List<Map<String, String>> tables = await odbc.getTables();

Disconnecting from the database

  await odbc.disconnect();

Accessing ODBC diver bindings directly

Tested On

This package has been tested to be working on the following Database Servers

Support for other Database Servers