lua-users home
lua-l archive

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


On 3/7/10, Luciano de Souza <luchyanus@predialnet.com.br> wrote:
> If I understood: to serialize is to convert tables into a string
> representation and, without some significant code, we cannot do it.

>
> I will study everything you sent me. But now I need the simplest solution. I
> am studying IUP. I am trying to save some data in a file to test more
> realistically the features I programed. Up to now, the simplest way to save
> tables, as I cold see, the way we write less lines, it seems to be using
> XML. Am I right?
>
> I only want to save a table. Firstly, I tried to use Sqlite3. I learnt the
> SQL sintax. I read something about normalized databases. I read... I read...
> But although Sqlite is really simple, something always is wrong. It seemed
> to be very simple. But I got errors without error messages.
>
> After I tried to use lots of coma separated files, but it became hard.
>
> I tried to use XML and the results were OK to write values. To read values
> semms to be a little bit harder. As I could see, the simplest way to store a
> table is to use Luaxml.
>
> Actually, I tried to create my tables in Pascal:
>
> type
>
> wolrd = record
>
> country: string;
>
> city: string;
>
> end;
>
> With Plua and the interface with Lua stack, I could add and remove values in
> a table stored in a file. But it's a awful way, I know.
>
> In Python, we have something like:
>
> import shelve
>
> file = shelve.open('countries.db')
>
> For this point on, "file" can be used as a table. After trying so many
> different things, only to save some data collected by a IUP interface,
> perhaps I am impacient. I am not lazy, believe.
>
> My options area:
>
> 1. to discover how to handle with the Sqlite erros;
>
> 2. to undestand better Luaxml. I found so few examples that I didn't mature
> the idea. Lua has good manuals, but I think Luaxml is not one of then.
>
> If I invest in this strategies of serialization, I will to learn more, and
> more, and more... I know to learn is one of the best things in the wolrd,
> but at this moment I need to conquer something.
>
> That's the greatest problem of beginners. You are right in everything you
> said. I think the problem is me.

If you you want serialization, save, and restore and xml is to
complex, then I'd go for one of those json libraries (for example
json4lua)

require("json")
t = {
-- data here
}

-- save to file
f = io.open(filename, "w")
f:write(json.encode(t))
f:close()

--read from file
f = io.open(filename)
t = json.decode(f:read("*all"))
f:close()