Unity接入SQLite :C#封装SQL命令
using System.Data.SQLite;
public class SQLiteHelper
{
private string connectionString;
public SQLiteHelper(string dbPath)
{
connectionString = $"Data Source={dbPath}";
}
public void ExecuteNonQuery(string query)
{
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(query, connection))
{
command.ExecuteNonQuery();
}
}
}
public SQLiteDataReader ExecuteReader(string query)
{
var connection = new SQLiteConnection(connectionString);
connection.Open();
var command = new SQLiteCommand(query, connection);
return command.ExecuteReader();
}
public object ExecuteScalar(string query)
{
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (var command = new SQLiteCommand(query, connection))
{
return command.ExecuteScalar();
}
}
}
}
这个代码示例展示了如何封装SQLite数据库操作的基本命令,包括执行非查询命令(如INSERT、UPDATE、DELETE)、执行查询命令并返回结果集以及执行查询命令并返回结果集中的第一行的第一列。这些封装方法可以简化数据库操作,并减少代码中的重复和错误。
评论已关闭