Lua数据库/MySQL操作

来源:互联网 发布:arcgis mac 编辑:程序博客网 时间:2024/06/03 06:15

0x00 封装前提

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

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

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

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

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

CloseConnect()使用。完美解决。

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


0x01 代码实现

----预留日志记录function Log(msg)--日志文件写到这里 /var/logprint(msg);end--创建环境句柄function CreateEnv()luasql = require "luasql.mysql"env_handle = luasql.mysql();return env_handle;endfunction ConnectMySqlDb(env_handle, database_name, user_name, user_pwd, ip, port)--如果没有环境句柄直接报错if(nil == env_handle) thenLog("没有创建环境句柄,无法调用ConnectMysqlDb!");return nil;endif(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 thenprint("ConnectMysqlDb->connect 连接默认数据库有问题!");endreturn conn;else----自定义连接数据库conn = env_handle:connect(database_name, user_name, user_pwd, ip, port);if nil == conn thenprint("ConnectMysqlDb->connect 连接自定义数据库有问题!");endreturn conn;endendfunction  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)) thenLog("SelectData 参数有问题");return -1;endcursor,errorString  = connc_handle:execute(sql_string);if  (nil ~=  errorString) thenLog(errorString);return -1;endrow = cursor:fetch ({}, "a");if( -1 == row) thenLog("no get row!");return 1;elsewhile row doprint(string.format("Id: %s, Name: %s", row.id, row.name))-- reusing the table of resultsrow = cursor:fetch (row, "a")endendreturn 1;end--执行增删改sql语句function ExecuteSqlString(connc_handle,  sql_string) if((nil == connc_handle) or (nil == sql_string)) thenLog("ExecuteSqlString 参数有问题");return -1;endcursor,errorString  = connc_handle:execute(sql_string);if  (nil ~=  errorString) thenLog(errorString);return -1;endreturn 1;end

dofile("test.lua");--int main()--{func_execute_status = 0;--连接数据库env_handle = CreateEnv();if(nil == env_handle) thenLog("env handle create fail!");os.exit(1);end--conn_mysql_handle = ConnectMySqlDb(env_handle);if nil == conn_mysql_handle  thenprint("no connect mysql db!");elseprint("connect mysql db!");end--执行自定义语句--ExecuteSqlString(conn_handle,SqlString)--conn_mysql_handle:execute 执行成功是0func_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) thenLog("SelectData no execute !");endinsert_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) thenLog("ExecuteSqlString no execute !");end--关闭环境变量句柄 和 连接句柄CloseConnect(env_handle, conn_mysql_handle);--return 0;--}


0x02经验总结

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

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

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

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

三、完成后与领导进行沟通,我完成了,大家都是一个团队,不要做孤胆英雄。





原创粉丝点击