lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>> 
>>> So luapsql also has the build issue I posted about earlier, and also
>>> fails without giving any error message!
>>> 
>>> 
>>> #!/usr/bin/env lua5.1
>>> local sql = require('psql')
>>> 
>>> if not arg[1] then
>>>      print("Usage: " .. arg[0] .. " database-name")
>>>      os.exit(1)
>>> end
>>> 
>>> local name = arg[1]
>>> os.execute("createdb '" .. name .. "'")
>>> local db = assert(sql.connect(("host=localhost dbname='%s'"):
>>>      format(name, name)))
>>> 
>>> local function exec(query, ...)
>>>      return assert(db:exec(query, ...))
>>> end
>>> 
>>> exec [[
>>> CREATE TABLE channels (
>>>      id INTEGER PRIMARY KEY,
>>>      path VARCHAR(16384) NOT NULL
>>> ); ]]
>>> 
>>> 
>>> lua5.1: ./create-database.lua:26: assertion failed!
>>> stack traceback:
>>>      [C]: in function 'assert'
>>>      ./create-database.lua:26: in function 'exec'
>>>      ./create-database.lua:33: in main chunk
>>>      [C]: ?
>>> 
>>> sure enough db:exec() returns absolutely nothing. Oi.
>> 
>> 
>> The connectdb() function always returns a connection object, unless there is no more memory.  You have to check the connection status using the status() method:
>> 
>> local conn = pgsql.connectdb('')
>> if conn:status ~= pgsql.CONNECTION_OK then
>>        print('no connection made')
>>        os.exit(1)
>> end
>> 
>> 
>> 
> 
> I'm not sure where connectdb comes from... my example was using the
> connect() method. I'm also really not understanding why connect() or
> connectdb() would return anything other than nil and an error message
> if connecting fails, or why the resulting object would then throw
> useless errors instead of giving some hint as to the problem...
> 

Oh, now I see, you are using psql, not pgsql, which is a different PostgreSQL interface...

Well, in pgsql there are more than one methods to connect to a database, most notably you can connect asynchronously.  All these functions return a connection object.  In the case of an asynchronous connection, you must query the status before sending queries.

connectdb() just behaves like all other connection functions.

But then, this does not apply to your code, since you are using psql, not pgsql... Sorry for the confusion.