沙盘引擎 (SEngine)

创意诞生沙盘世界,不止如此想象!

用户工具

站点工具


侧边栏

scripting:native:sqlite

📚 Native/SQLite

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

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

SQLite类仅支持增删查改以及部分扩展操作,实际上若选用SQLite作为存储方式,建议使用Browser等相关工具自行可视化建库,然后使用引擎API进行操作,而不是100%通过引擎API方式。

出于安全考虑,SQLite类目前仅支持World端使用。

📒 Static Function

📘 SQLite.Connect()

连接指定路径的数据库文件(仅支持在Mod\Space目录下)。

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

数据库的操作仅在当前场景生效。

function SQLite.Connect( path: string ): SQLiteSource
let gameDB = SQLite.Connect("Database.db"); //Path: LocalMod/Space/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") + "'";

📒 Function

📘 SQLiteSource.Disconnect()

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

function SQLiteSource.Disconnect()

📘 SQLiteSource.Begin()

开始数据库操作事务。

function SQLiteSource.Begin()

📘 SQLiteSource.Commit()

提交数据库操作事务。

function SQLiteSource.Commit()

📘 SQLiteSource.Rollback()

回退数据库操作事务。

function SQLiteSource.Rollback()

📘 SQLiteSource.Execute()

执行一段SQL指令,并可能返回目标结果(如果存在查询\执行结果,将返回SQLiteSourceData)。

此指令扩展性很高,可进行执行、查询等多种用途,属于万能指令。

function SQLiteSource.Execute( sql: string ): SQLiteSourceData
let gameDB = SQLite.Connect("Database.db");
 
//Only Execute
gameDB.Execute("sqlxxx");
 
//QuerySQL
let sqlData = gameDB.Execute("sql");
DLog(sqlData.GetData("ColumnName", 0));

📘 SQLiteSource.ExecuteScalar()

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

SQLiteSource.Execute方法不同的是,此方法会直接返回(如有)得到的第一个结果(默认:列1行1,可通过参数2+3扩展)。

function SQLiteSource.ExecuteScalar( sql: string, columnName: string = "", row: int = -1 ): Any

📘 SQLiteSource.GetTableRowCount()

获取指定表的行数。

function SQLiteSource.GetTableRowCount( table: string ): int

📘 SQLiteSource.GetTableColumnCount()

获取指定表的列数。

function SQLiteSource.GetTableColumnCount( table: string ): int

📘 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

📘 SQLiteSourceData.GetCount()

获取当前查询结果数量。

function SQLiteSourceData.GetCount(): int

📘 SQLiteSourceData.GetData()

获取当前查询的指定索引结果。

function SQLiteSourceData.GetData( columnName: string, index: int = 0 ): Any
let gameDB = SQLite.Connect("Database.db");
 
//QuerySQL
let sqlData = gameDB.Execute("SELECT * FROM Accounts WHERE Name = '" + SQLite.EscapeString("playerName") + "'");
if(sqlData.GetCount() > 0) DLog(sqlData.GetData("Level", 0));

scripting/native/sqlite.txt · 最后更改: 2024/03/11 20:41 由 bibiboxs