您的位置:首页 > 编程语言 > Lua

Lua数据库/MySQL操作

2017-09-04 08:41 302 查看
0x00 封装前提

    这次只封装到函数接口这一层,本来想封装到业务类调用的,还有好多知识点要复习,就放弃了。

现在就来说说封装接口的指导思想:

1.接口单一职责,这是一直我封装接口所提倡和遵守的,做一件事就好,多了除了作者自己‘嗨’,别人看不懂,这样的接口没劲。

2.每个语言都有语法特性,一定要前期了解好,这样方便做非法操作判断和处理。

3.编写接口前先实现功能,之后根据功能代码进行切割,这样封装起来心里有个数,对切割功能函数粒度有个把控。比如我封装ConnectMySqlDb()的时候,把环境变量也封装进去了,后来在释放环境句柄和连接句柄的时候,环境句柄找不到了。这样我很轻松的把环境变量提取出来封装成单独一个接口提供给ConnectMySqlDb()和

CloseConnect()使用。完美解决。

4.里程碑的确立,确定好任务完成的边界,不做无底洞的脑洞。这个真的很重要,无休无止的开脑洞,只会拖累自己和让工程遥遥无期,踏踏实实的做里程碑就好了。就好比我这个接口,完成简单的增删改查的功能,事务之类的留在第二版进行编写。

0x01 代码实现

----预留日志记录
function Log(msg)
--日志文件写到这里 /var/log
print(msg);

end

--创建环境句柄
function CreateEnv()

luasql = require "luasql.mysql"
env_handle = luasql.mysql();

return env_handle;
end

function ConnectMySqlDb(env_handle, database_name, user_name, user_pwd, ip, port)

--如果没有环境句柄直接报错
if(nil == env_handle) then
Log("没有创建环境句柄,无法调用ConnectMysqlDb!");

return nil;
end

if(database_name == nil) and (user_name == nil) and
(user_pwd == nil) and (ip == nil) and (port == nil)
then
----连接默认数据库
conn = env_handle:connect("reacher", "root", "admin", "127.0.0.1", 3306);
if nil == conn then
print("ConnectMysqlDb->connect 连接默认数据库有问题!");
end
return conn;
else
----自定义连接数据库
conn = env_handle:connect(database_name, user_name, user_pwd, ip, port);
if nil == conn then
print("ConnectMysqlDb->connect 连接自定义数据库有问题!");
end

return conn;
end

end

function  CloseConnect(env_handle, conn_handle)

env_handle:close();  --关闭数据库连接
conn_handle:close()   --关闭数据库环境

end

--查询数据表中的数据
function  ExecuteSelectSqlString(connc_handle,  sql_string)
if((nil == connc_handle) or
(nil == sql_string)) then
Log("SelectData 参数有问题");

return -1;
end

cursor,errorString  = connc_handle:execute(sql_string);
if  (nil ~=  errorString) then
Log(errorString);
return -1;
end

row = cursor:fetch ({}, "a");
if( -1 == row) then
Log("no get row!");
return 1;
else
while row do
print(string.format("Id: %s, Name: %s", row.id, row.name))
-- reusing the table of results
row = cursor:fetch (row, "a")
end
end

return 1;
end

--执行增删改sql语句
function ExecuteSqlString(connc_handle,  sql_string)

if((nil == connc_handle) or (nil == sql_string)) then
Log("ExecuteSqlString 参数有问题");
return -1;
end

cursor,errorString  = connc_handle:execute(sql_string);
if  (nil ~=  errorString) then
Log(errorString);
return -1;
end

return 1;

end

dofile("test.lua");

--int main()
--{
func_execute_status = 0;

--连接数据库
env_handle = CreateEnv();
if(nil == env_handle) then
Log("env handle create fail!");

os.exit(1);
end

--
conn_mysql_handle = ConnectMySqlDb(env_handle);
if nil == conn_mysql_handle  then
print("no connect mysql db!");
else
print("connect mysql db!");
end

--执行自定义语句
--ExecuteSqlString(conn_handle,SqlString)
--conn_mysql_handle:execute 执行成功是0
func_execute_status = conn_mysql_handle:execute"SET NAMES UTF8";

sql_string = "select * from student";
func_execute_status  =  ExecuteSelectSqlString(conn_mysql_handle, sql_string);
if(1 ~= func_execute_status) then
Log("SelectData no execute !");
end

insert_string  =  "insert into student(id, name)values(4, 'messi')";
func_execute_status =  ExecuteSqlString(conn_mysql_handle,insert_string);
print(func_execute_status);
if(1 ~= func_execute_status) then
Log("ExecuteSqlString no execute !");
end

--关闭环境变量句柄 和 连接句柄
CloseConnect(env_handle, conn_mysql_handle);

--	return 0;
--}


0x02经验总结

1.不要害怕报错,基础语法有些忘记了,自己挖坑自己跳,幸好心态够好。

2.debug的经验之谈:这就要大吹特吹一下接口的好处,单独调试。你想想吧,你封装的接口不过分耦合别的接口,调试只需要在关键的地方打上日志函数,还原报错原因基本是十拿九稳,之后整整逻辑,轻轻松松就修好了。

3.善于写记录文档,一、管理自己的任务清单,完成未完成一目了然。

二、了解自己的编码障碍在那里,完成任务后进行技能点提升。

三、完成后与领导进行沟通,我完成了,大家都是一个团队,不要做孤胆英雄。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: