对于简单的数据操作,无涯教程可以使用文件,但是有时,这些文件操作可能不是很方便。为此,可能经常切换到使用数据库。 LuaSQL是从Lua到许多数据库管理系统的简单接口。 LuaSQL是库,它提供对不同类型 SQL 的支持。这包括
在本教程中,将介绍Lua中MySQL和SQLite的MySQL数据库处理。这两者都使用通用接口,并且应该有可能将此实现移植到其他类型的数据库。首先一起看看如何在MySQL中进行操作。
为了使用以下示例按预期方式工作,需要初始数据库设置。这些假设在下面列出。
您已经安装并设置了MySQL,默认用户为root,密码为" 123456"。
您已经创建了数据库测试。
您已经遍历了MySQL教程,以了解 MySQL基础。
假设您的Lua实现正确完成,可以使用简单的 require 语句导入sqlite库。
mysql=require "luasql.mysql"
变量mysql将通过参考主mysql表提供对函数的访问。
无涯教程可以通过启动MySQL环境,然后为该环境创建连接来创建连接。如下所示。
local env =mysql.mysql() local conn=env:connect('test','root','123456')
上面的连接将连接到现有的MySQL文件,并与新创建的文件创建连接。
连接提供了一个简单的执行函数,该函数将帮助执行创建,插入,删除,更新等所有数据库操作。语法如下所示-
conn:execute([[ 'MySQLSTATEMENT' ]])
在以上语法中,需要确保conn已打开并且存在现有的MySQL连接,并用正确的语句替换" MySQLSTATEMENT"。
下面显示了一个简单的创建表示例。它创建一个带有两个参数id(类型为integer)和名称(类型为varchar)的表。
mysql=require "luasql.mysql" local env =mysql.mysql() local conn=env:connect('test','root','123456') print(env,conn) status,errorString=conn:execute([[CREATE TABLE sample2 (id INTEGER, name TEXT);]]) print(status,errorString )
当您运行上述程序时,将创建一个名为sample的表,其中包含两列,即id和name。
MySQL environment (004BB178) MySQL connection (004BE3C8) 0 nil
如果有任何错误,您将得到一条错误语句,而不是nil。下面显示了一个简单的错误声明。
LuaSQL: Error executing query. MySQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"id INTEGER, name TEXT)' at line 1
MySQL的插入语句如下所示。
conn:execute([[INSERT INTO sample values('11','Raj')]])
MySQL的更新语句如下所示。
conn:execute([[UPDATE sample3 SET name='John' where id ='12']])
MySQL的删除语句如下所示。
conn:execute([[DELETE from sample3 where id ='12']])
就select语句而言,无涯教程需要遍历每一行并提取所需的数据。一个简单的选择语句如下所示。
cursor,errorString=conn:execute([[select * from sample]]) row=cursor:fetch ({}, "a") 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
在上面的代码中,conn是一个开放的MySQL连接。借助execute语句返回的游标,您可以遍历表响应并获取所需的选择数据。
下面给出了一个完整的示例,其中包括上述所有语句。
mysql=require "luasql.mysql" local env =mysql.mysql() local conn=env:connect('test','root','123456') print(env,conn) status,errorString=conn:execute([[CREATE TABLE sample3 (id INTEGER, name TEXT)]]) print(status,errorString ) status,errorString=conn:execute([[INSERT INTO sample3 values('12','Raj')]]) print(status,errorString ) cursor,errorString=conn:execute([[select * from sample3]]) print(cursor,errorString) row=cursor:fetch ({}, "a") while row do print(string.format("Id: %s, Name: %s", row.id, row.name)) row=cursor:fetch (row, "a") end -- close everything cursor:close() conn:close() env:close()
当您运行上述程序时,您将获得以下输出。
MySQL environment (0037B178) MySQL connection (0037EBA8) 0 nil 1 nil MySQL cursor (003778A8) nil Id: 12, Name: Raj
事务是一种确保数据一致性的机制。事务应具有以下四个属性-
原子性 - 要么完成,要么什么都没有发生。
一致性 - 事务必须以一致的状态开始并使系统保持一致的状态。
隔离性 - 事务的中间输出在当前事务之外不可见。
耐久性 - 提交事务后,即使在系统出现故障后,影响仍然持久。
事务以START TRANSACTION开始;并以commit或rollback语句结束。
为了启动事务,需要在lua中执行以下语句,假设conn是一个开放的MySQL连接。
conn:execute([[START TRANSACTION;]])
需要执行以下语句来回滚执行start事务后所做的更改。
conn:execute([[ROLLBACK;]])
需要执行以下语句来提交开始事务执行后所做的更改。
conn:execute([[COMMIT;]])
上面已经了解MySQL,以下部分将介绍有关基本SQL操作的信息。记住事务,尽管不再对SQLite3进行解释,但是相同的语句也适用于SQLite3。
假设您的Lua实现正确完成,可以使用简单的require语句导入SQLite库。在安装过程中,一个包含数据库相关文件的libsql文件夹。
sqlite3=require "luasql.sqlite3"
变量sqlite3将通过引用主sqlite3表提供对函数的访问。
可以通过启动SQLite环境然后为该环境创建连接来创建连接。如下所示。
local env =sqlite3.sqlite3() local conn=env:connect('mydb.sqlite')
上面的连接将连接到现有的SQLite文件或创建一个新的SQLite文件,并与新创建的文件创建连接。
连接提供了一个简单的执行函数,该函数将帮助执行创建,插入,删除,更新等所有数据库操作。语法如下所示-
conn:execute([[ 'SQLite3STATEMENT' ]])
在上面的语法中,无涯教程需要确保conn已打开并且存在现有的sqlite3连接,并用正确的语句替换" SQLite3STATEMENT"。
下面显示了一个简单的创建表示例。它创建一个带有两个参数id(类型为integer)和名称(类型为varchar)的表。
sqlite3=require "luasql.sqlite3" local env =sqlite3.sqlite3() local conn=env:connect('mydb.sqlite') print(env,conn) status,errorString=conn:execute([[CREATE TABLE sample ('id' INTEGER, 'name' TEXT)]]) print(status,errorString )
当您运行上述程序时,将创建一个名为sample的表,其中包含两列,即id和name。
SQLite3 environment (003EC918) SQLite3 connection (00421F08) 0 nil
如果发生错误,将返回一条错误语句,而不是nil。下面显示了一个简单的错误声明。
LuaSQL: unrecognized token: ""'id' INTEGER, 'name' TEXT)"
SQLite的插入语句如下所示。
conn:execute([[INSERT INTO sample values('11','Raj')]])
就select语句而言,需要遍历每一行并提取所需的数据。一个简单的选择语句如下所示。
cursor,errorString=conn:execute([[select * from sample]]) row=cursor:fetch ({}, "a") 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
在上面的代码中,conn是一个开放的sqlite3连接。借助execute语句返回的游标,您可以遍历表响应并获取所需的选择数据。
下面给出了一个完整的示例,其中包括上述所有语句。
sqlite3=require "luasql.sqlite3" local env =sqlite3.sqlite3() local conn=env:connect('mydb.sqlite') print(env,conn) status,errorString=conn:execute([[CREATE TABLE sample ('id' INTEGER, 'name' TEXT)]]) print(status,errorString ) status,errorString=conn:execute([[INSERT INTO sample values('1','Raj')]]) print(status,errorString ) cursor,errorString=conn:execute([[select * from sample]]) print(cursor,errorString) row=cursor:fetch ({}, "a") while row do print(string.format("Id: %s, Name: %s", row.id, row.name)) row=cursor:fetch (row, "a") end -- close everything cursor:close() conn:close() env:close()
当您运行上述程序时,您将获得以下输出。
SQLite3 environment (005EC918) SQLite3 connection (005E77B0) 0 nil 1 nil SQLite3 cursor (005E9200) nil Id: 1, Name: Raj
无涯教程可以在此libsql库的帮助下执行所有可用的查询。因此,请不要仅仅停留在这些示例上。在Lua中的各个MySQL,SQLite3和其他受支持的数据库中试验各种查询语句。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕