On 02/04/2015 10:21 AM, Derek Frost wrote:
> I'm sorry if this is a stupid question, I'm a bit new to ms (but I
> know coffeescript) and I know next to nothing about Lua.
>
> So I've been falling in love with moonscript and having great fun
> playing with functions and objects, but now I want to interact with
> modules and try to get at least a basic db to get me started - even
> sqlite3 would be ok for now. I've had no success using 'require' with
> moonscript and I failed to get tapis installed and not sure it would
> help me anyway because I don't have postgres.
>
> What I really want to know is can I somehow connect to luasql and use
> moonscript with a db? I can't find any code examples at all for using
> moonscript with a db, except with lapis which I copied. I've tried to
> get tapis via luarocks but I'm not certain it's properly installed
> because I have a rocks folder but not much else - but it seems to
> install then says error unpacking.
You've said "tapis" a few times by this point, do you mean Lapis? I
think you're right, if you don't want to or can't setup PostgreSQL,
SQLite is likely going to be your option.
https://github.com/leafo/lapis/blob/master/lapis/db.moon
>
> I must say I'm stuck now. Not succeeding with sqlite3 is lame - I
> usually sort that out in seconds with languages I'm pathetic at - like
> lua! Any help would be great, many thanks,
I tend to write one-off database updates here at work in Moonscript &
LuaSQL (specifically with luasql.mysql) Below is an example with some
comments added describing most of the functionality:
-- This is a database change to convert a table from being stored in hours
-- to minutes
-- Require luasql.mysql, assert if it doesn't load, and store the result
-- into luasql
luasql = assert require 'luasql.mysql'
-- Just some variables to define the host, username, & password
host = ''
user = ''
pass = ''
-- A select to get the id & hours from the entries table where hours are
-- not 0
entries = [[
SELECT id, hours
FROM entries
WHERE hours <> 0
]]
-- Basic update SQL to be filled in with a string.format
entry_update = [[
UPDATE entries
SET hours=%d
WHERE id=%d
]]
-- Setup the env to be mysql
env = assert luasql.mysql!
-- Actually connect to the database with the "some_schema" database
conn = assert env\connect 'some_schema', user, pass, host
-- Execute the select
cur = assert conn\execute entries
-- Get the first entry from the select as an associative array, so you can
-- access database columns with entry.column
entry = cur\fetch {}, 'a'
-- Standard loop over results
while entry
-- Execute the update with the hours converted to minutes
update = string.format entry_update, math.ceil(entry.hours*60),
entry.id
assert conn\execute update
-- Fetch the next result and store it in entry
entry = cur\fetch {}, 'a'
-- Close the cursor
cur\close!
-- Alter the table column name
assert conn\execute [[
ALTER TABLE entries
CHANGE COLUMN hours minutes INTEGER UNSIGNED NOT NULL DEFAULT 0
]]
-- close the connection & environment
conn\close!
env\close!
IIRC in the LuaSQL documentation, the env, conn, & cur will look very
similar to what they used there. Let me know if you have any questions,
because I use LuaSQL with Moonscript semi-regularly for maintenance tasks.