使用 write() 和 read() 创建一个简单的 Lua .txt 数据库

我正在尝试用 LUA 创建一个简单的两个函数的文本“数据库”。我只需要两个函数。

我的数据库应该像这样:

    varName;varValue
    JohnAge;18
    JohnCity;Munich
    LarissaAge;21
    LarissaCity;Berlin

实际上我不受任何格式的限制!我只是没有一种长期保存数据的方式在我的lua环境中,我需要找到一个解决办法。如果你已经有了一个类似的解决方案,请随时向我提供。非常感谢

Function WriteToDB(varName, varValue)
If database.text contains a line that starts with varName
replace whatever comes after seperator ";" with varValue (but dont go into the next line)

Function ReadFromDB(varName)
If database.text contains a line that starts with varName
take whatever comes after the seperator ";" and return that (but dont go into the next line)
elseif not found print("error")
点赞
用户107090
用户107090

将数据保存为 Lua 代码以构建表格:

return {
JohnAge = 18,
JohnCity = "Munich",
LarissaAge = 21,
LarissaCity = "Berlin",
}

更好的是

return {
["John"] = {Age = 18, City = "Munich"},
["Larissa"] = {Age = 21, City = "Berlin"},
}

使用以下代码加载数据

db = dofile"db.lua"

使用以下代码访问数据

print(db["Larissa"].Age)

或者

print(db[name].Age)
2016-11-20 12:29:02