lua 中如何读取本机IP和ODBC的设置
发布网友
发布时间:2022-05-20 13:22
我来回答
共1个回答
热心网友
时间:2023-10-25 02:19
-- odbc_reader.lua,读取odbc.ini文件并解析
require "pl.pretty"
odbc = {}
for line in io.lines('odbc.ini') do
temp = line:match('%[(.+)%]')
if temp then
if temp ~= 'ODBC Data Sources' then
source = temp
odbc[source] = {}
end
elseif source and line ~= '' then
key, value = line:match('(%w+)%s*=%s*(.*)')
odbc[source][key] = value
end
end
print(pl.pretty.write(odbc))
======================================================================
-- odbc.ini文件内容
[ODBC Data Sources]
myodbc = MyODBC 2.50 Driver DSN
myodbc3 = MyODBC 3.51 Driver DSN
[myodbc]
Driver = /usr/local/lib/libmyodbc.so
Description = MyODBC 2.50 Driver DSN
SERVER = localhost
PORT =
USER = root
Password =
Database = test
OPTION = 3
SOCKET =
[myodbc3]
Driver = /usr/local/lib/libmyodbc3.so
Description = MyODBC 3.51 Driver DSN
SERVER = localhost
PORT =
USER = root
Password =
Database = test
OPTION = 3
SOCKET =
[Default]
Driver = /usr/local/lib/libmyodbc3.so
Description = MyODBC 3.51 Driver DSN
SERVER = localhost
PORT =
USER = root
Password =
Database = test
OPTION = 3
SOCKET =
=====================================================================
-- 打印结果
{
myodbc = {
Database = "test",
SERVER = "localhost",
Description = "MyODBC 2.50 Driver DSN",
SOCKET = "",
PORT = "",
Password = "",
Driver = "/usr/local/lib/libmyodbc.so",
USER = "root",
OPTION = "3"
},
Default = {
Database = "test",
SERVER = "localhost",
Description = "MyODBC 3.51 Driver DSN",
SOCKET = "",
PORT = "",
Password = "",
Driver = "/usr/local/lib/libmyodbc3.so",
USER = "root",
OPTION = "3"
},
myodbc3 = {
Database = "test",
SERVER = "localhost",
Description = "MyODBC 3.51 Driver DSN",
SOCKET = "",
PORT = "",
Password = "",
Driver = "/usr/local/lib/libmyodbc3.so",
USER = "root",
OPTION = "3"
}
}