Skip to content

I have some questions since I'm a noobie #119

Closed

Description

@JeysonFlores

I have 2 pieces of code, the one like the example of the README.md and the other the code that is on #113 . I have these questions:

  • How do I define the tables in order to make them compatible with the sqlite_tbl? Initially I thought that when I connect the DB and define the tables these tables would be created (like an ORM) but it seems like doesn't work like that. So, when I declare a "luatable" type in a column what type should I declare in the SQL definition? text, varchar or something else?

I have this code by the way

local sqlite = require 'sqlite'

local db = sqlite {
    uri = "db.sqlite",
    projects = {
        objectives = "luatable",
        title = { "text", unique = true, primary = true},
        created = { "timestamp", default = sqlite.lib.strftime("%s", "now") },
        done = "boolean"
    },
    todos = {
        id = true,
        title = "text",
        project = { "text", reference = "projects.title", on_delete = "cascade" }
    },
}

if db:exists("projects") == false then
    db:execute("create table projects")
end

and it gives me the following error:

luajit: /usr/local/share/lua/5.1/sqlite/stmt.lua:24: Invalid connection passed to sqlstmt:parse
stack traceback:
	[C]: in function 'assert'
	/usr/local/share/lua/5.1/sqlite/stmt.lua:24: in function 'parse'
	/usr/local/share/lua/5.1/sqlite/db.lua:249: in function 'eval'
	/usr/local/share/lua/5.1/sqlite/db.lua:324: in function 'exists'
	sq.lua:18: in main chunk
	[C]: at 0x56455f2641d0

Activity

  1. kkharji commented on Sep 21, 2021

    @kkharji
    Owner

    Okay there are few points to take here.

    1. sqlite {} creates a logical object, which by the way is now changed because of reasons mentioned in this commit 8e47074.
    2. sqlite {} is an abstraction over sqlite.new which only create sqlite object without opening the connection, unlike sqlite:open, however, opts.keep_open should ensure that it keeps connection open

    How do I define the tables in order to make them compatible with the sqlite_tbl? Initially I thought that when I connect the DB and define the tables these tables would be created (like an ORM) but it seems like doesn't work like that.

    You can call sqlite.tbl() to create new table object and later pass it to sqlite {} or sqlite:extend {}, and what sqlite.tbl expect is the table shape compatible to create sqlite_tbl object

    So, when I declare a "luatable" type in a column what type should I declare in the SQL definition? text, varchar or something else?

    I don't understand what you meant here

  2. Repository owner locked and limited conversation to collaborators on Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions