mwild1 / luadbi

Multi-backend SQL database library for Lua
MIT License
41 stars 14 forks source link

Can't assert a DBI #73

Closed vomus closed 7 months ago

vomus commented 7 months ago

Hi! I am somewhat new to Lua but I need to run a few scripts from inside a luatex. I have a luadbi installed from a package for my distribution but does not seem to be able to run it even following test example:

[serge@veles latex]$ lua5.3 
Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> DBI = require "DBI"
> assert.is_not_nil(DBI)
stdin:1: attempt to index a function value (global 'assert')
stack traceback:
        stdin:1: in main chunk
        [C]: in ?
>

I went to look at the code and tried the following:

> local name_to_module = {
    MySQL = 'dbd.mysql',
    PostgreSQL = 'dbd.postgresql',
    SQLite3 = 'dbd.sqlite3',
    DB2 = 'dbd.db2',
    Oracle = 'dbd.oracle',
    ODBC = 'dbd.odbc'
}
> name_to_module[MySQL]
stdin:1: attempt to index a nil value (global 'name_to_module')
stack traceback:
        stdin:1: in main chunk
        [C]: in ?
> name_to_module = {
    MySQL = 'dbd.mysql',
    PostgreSQL = 'dbd.postgresql',
    SQLite3 = 'dbd.sqlite3',
    DB2 = 'dbd.db2',
    Oracle = 'dbd.oracle',
    ODBC = 'dbd.odbc'
}
> name_to_module[MySQL]
nil
> 

How come this does not work if it is taken from DBI.lua? Is there anything I am doing incorrectly?

sparked435 commented 7 months ago

Your first example, 'assert' is normally a built in Lua function. Try:

DBI = require "DBI" assert(DBI)

The code you are see calling assert.is_not_nil is likely part of the test suite, which overrides assert with the luassert library to simplify test cases. If you are new to Lua and/or LuaDBI I recommend staying with the builtin assert() function.

Second example, you are missing either single or double quotes, try:

name_to_module['MySQL']

Also make sure your database driver modules are installed as well (dbd.mysql, etc. The exact package name varies with your distribution.)

vomus commented 7 months ago

Thanks, it seems to work now...