class DuckDB::Database
The Database
class encapsulates a DuckDB
database.
The usage is as follows:
require 'duckdb' db = DuckDB::Database.open # database in memory con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))') con.query("INSERT into users VALUES(1, 'Alice')") con.query("INSERT into users VALUES(2, 'Bob')") con.query("INSERT into users VALUES(3, 'Cathy')") result = con.query('SELECT * from users') result.each do |row| p row end
Public Class Methods
Source
# File lib/duckdb/database.rb, line 41 def open(dbpath = nil, config = nil) db = _db_open(dbpath, config) return db unless block_given? begin yield db ensure db.close end end
Opens database. The first argument is DuckDB
database file path to open. If there is no argument, the method opens DuckDB
database in memory. The method yields block if block is given.
DuckDB::Database.open('duckdb_database.db') #=> DuckDB::Database DuckDB::Database.open #=> opens DuckDB::Database in memory. DuckDB::Database.open do |db| con = db.connect con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))') end
Public Instance Methods
Source
static VALUE duckdb_database_close(VALUE self) { rubyDuckDB *ctx; TypedData_Get_Struct(self, rubyDuckDB, &database_data_type, ctx); close_database(ctx); return self; }
closes DuckDB
database.
Source
# File lib/duckdb/database.rb, line 74 def connect conn = _connect return conn unless block_given? begin yield conn ensure conn.disconnect end end
connects database.
The method yields block and disconnects the database if block given
db = DuckDB::Database.open con = db.connect # => DuckDB::Connection db.connect do |con| con.query('CREATE TABLE users (id INTEGER, name VARCHAR(30))') end