📚 World/SQLite

SQLite类是开发阶段常用功能,可进行本地数据库(SQLite)的相关操作。

数据库操作需要掌握相关知识,适合复杂数据存储或有明确目的使用,如果只是希望实现简单游戏存档等功能,请考虑使用Json功能。

注意:数据库操作语句将直接影响数据安全,因此应该确保严格的SQL指令输入,以及字符串的安全过滤。

📒 Static Function

📘 SQLite.Create()

创建指定路径的数据库文件(目录:Mod\Host)。

补充:数据库文件仍然属于主机文件,因此可以通过World.Core.DeleteHostFile()进行删除(不可恢复)。

function SQLite.Create( path: string, sql: string = null, isReplace: bool = false )
SQLite.Create("Database.db"); //Path: LocalMod\Host\Database.db
  • path文件路径
  • sql创建数据的执行命令
  • isReplace如果文件已经存在,是否进行覆盖(不可恢复)

📘 SQLite.Connect()

连接指定路径的数据库文件(目录:Mod\Host)。

想要操作一个数据库,连接数据库是必须的。

function SQLite.Connect( path: string ): SQLiteSource
let gameDB = SQLite.Connect("Database.db"); //Path: LocalMod\Host\Database.db
if(gameDB != null)
{
	gameDB.Execute("sql"); //Todo
}

📘 SQLite.EscapeString()

格式化数据库字符串,降低目标字符串SQL注入等风险。

function SQLite.EscapeString( text: string ): string
let sql = "SELECT * FROM Accounts WHERE Name = '" + SQLite.EscapeString("playerName") + "'";

📘 SQLite.IsSafeString()

检查指定数据库字符串,判断是否为安全字符串(不包含非安全字符)。

function SQLite.IsSafeString( text: string ): bool

📒 Function

📘 SQLiteSource.Disconnect()

关闭当前数据库连接,停止使用(场景切换时会自动执行)。

注意:因为SQLite是基于文件流操作的,所以当数据库中途不再使用时,应该执行Disconnect()释放文件权限。

function SQLiteSource.Disconnect()

📘 SQLiteSource.Begin()

开始数据库操作事务。

function SQLiteSource.Begin()

📘 SQLiteSource.Commit()

提交数据库操作事务。

function SQLiteSource.Commit()

📘 SQLiteSource.Rollback()

回退数据库操作事务。

function SQLiteSource.Rollback()

📘 SQLiteSource.Query()

执行一段SQL指令,并返回可能的结果。

function SQLiteSource.Query( sql: string ): SQLiteQueryResult

📘 SQLiteSource.Execute()

执行一段SQL指令,不返回任何结果。

📘 SQLiteSource.ExistTable()

检查指定表是否存在。

function SQLiteSource.ExistTable( table: string ): bool

📘 SQLiteSource.DropTable()

删除指定表(不可逆)。

function SQLiteSource.DropTable( table: string )

📘 SQLiteSource.RenameTable()

重命名指定表(需自行做好前提判断)。

function SQLiteSource.RenameTable( table: string, newTable: string )

📘 SQLiteSource.LastInsertRowId()

获取上次插入的最新RowID

function SQLiteSource.LastInsertRowId(): int64

📒 SQLiteQueryResult

📘 SQLiteQueryResult.Get()

根据列\行获取数值,如果目标\数值不存在,默认返回null

注意:如果指定列\行存在表内数据,但数值内容为空,同样会返回null,准确判断应该使用ExistColumn() | ExistRow()

function SQLiteQueryResult.Get( column: string, row: int = 0, bigType: bool = false ): any
function SQLiteQueryResult.Get( column: int, row: int = 0, bigType: bool = false ): any
  • bigType是否转换为大类型数据(默认高精度格式),通常不需要开启,返回常规类型数据即可。

📘 SQLiteQueryResult.ColumnCount()

获取结果列数。

function SQLiteQueryResult.ColumnCount(): int

📘 SQLiteQueryResult.RowCount()

获取结果行数。

function SQLiteQueryResult.RowCount(): int

📘 SQLiteQueryResult.ExistColumn()

获取指定列是否存在。

function SQLiteQueryResult.ExistColumn( column: string ): bool
function SQLiteQueryResult.ExistColumn( column: int ): bool

📘 SQLiteQueryResult.ExistRow()

获取指定行是否存在。

function SQLiteQueryResult.ExistRow( row: int ): bool

📘 SQLiteQueryResult.HasData()

获取当前结果是否有效且存在数据

注意:有时此选项会返回false,但IsValid()返回true,表示查到了对应的有效表,但是没有找到匹配的行数据

function SQLiteQueryResult.HasData(): bool

📘 SQLiteQueryResult.IsValid()

获取当前结果是否有效(查询流程正常,即使没有找到匹配的行数据)。

注意:如果此值返回false,则代表没有找到任何内容,查询没有意义。

function SQLiteQueryResult.IsValid(): bool