我如何将一个表序列化以便插入到数据库中?

我想将一个表序列化,以便将其插入到数据库中,并在以后检索它。我该如何做?

点赞
用户1212870
用户1212870

Lua表格打印函数

local szStr = ""
function print_lua_table (lua_table, indent)
    indent = indent or 0
    for k, v in pairs(lua_table) do
        if type(k) == "string" then
            k = string.format("%q", k)
        end
        local szSuffix = ""
        if type(v) == "table" then
            szSuffix = "{"
        end
        local szPrefix = string.rep("    ", indent)
        formatting = szPrefix.."["..k.."]".." = "..szSuffix
        if type(v) == "table" then
            szStr = szStr..formatting
            print_lua_table(v, indent + 1)
            szStr = szStr..szPrefix.."},"
        else
            local szValue = ""
            if type(v) == "string" then
                szValue = string.format("%q", v)
            else
                szValue = tostring(v)
            end
            szStr = szStr..formatting..szValue..","
        end
    end
end

这是一个Lua表格打印函数,可以将一个表格格式化输出成字符串。该函数接收两个参数:要打印的表格,以及缩进的层数(可选,默认值为0)。

2013-06-24 05:23:07
用户1847592
用户1847592

如何使 Lua 值保持良好的序列化?

local serialize
do
    local num_fmt = '%.17g'
    local NaN_serialized = {  -- 这个思路是从 lua-nucleo 偷来的
        [num_fmt:format(1/0)] = '1/0',
        [num_fmt:format(-1/0)] = '-1/0',
        [num_fmt:format(0/0)] = '0/0'
    }
    local function serialize_table(t, indent)
        indent = indent or ''
        local new_indent = indent..'\t'
        if next(t) == nil then
            return '{}'
        else
            local lines = {}
            local function add_line(key)
                local ser_key = key
                if type(key) ~= 'string' or not key:find'^[%a_][%w_]*$' then
                    ser_key = '['..serialize(key, new_indent)..']'
                end
                table.insert(lines,
                    ser_key..' = '..serialize(t[key], new_indent))
            end
            local other_keys = {}
            local keys = setmetatable({number = {}, string = {}},
                {__index = function() return other_keys end})
            for k in pairs(t) do
                table.insert(keys[type(k)], k)
            end
            table.sort(keys.number)
            table.sort(keys.string)
            for _, k in ipairs(keys.number) do
                add_line(k)
            end
            for _, k in ipairs(keys.string) do
                add_line(k)
            end
            for _, k in ipairs(other_keys) do
                add_line(k)
            end
            return '{\n'..new_indent..table.concat(lines, ',\n'..new_indent)
                ..'\n'..indent..'}'
        end
    end

    function serialize(v, indent)
        if type(v) == 'string' then
            return ('%q'):format(v)
        elseif type(v) == 'boolean' then
            return tostring(v)
        elseif type(v) == 'nil' then
            return tostring(v)
        elseif type(v) == 'number' then
            return (num_fmt:format(v):gsub('^.*', NaN_serialized))
        elseif type(v) == 'table' then
            return serialize_table(v, indent)
        else
            error('Can not serialize '..type(v))
        end
    end
end

-- 它能做什么:
print(serialize(math.huge))
print(serialize'"What\'s up?"\n\t123')
print(serialize{{}, {{}}})

-- 它不能做什么:
local t = {}
local tt = {[t] = t}
print(serialize(tt)) -- tt 不是树,因此其序列化不正确
2013-06-25 18:21:15