博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Unity】封装SQLite管理类
阅读量:4085 次
发布时间:2019-05-25

本文共 2942 字,大约阅读时间需要 9 分钟。

using UnityEngine;using System.Collections;using Mono.Data.Sqlite;using System.Data.SqlClient;public class SQLiteManager{	private SqliteConnection con;	private SqliteCommand command;	// 构造函数重载	public SQLiteManager (string dataBasePath)	{		// 链接数据库		con = new SqliteConnection (dataBasePath);		// 打开数据库		con.Open ();		// 实例化命令		command = con.CreateCommand ();	}	// 增--方式1	// insert into 表名  values(值1,值2...)	public SqliteDataReader InsertData (string tableName, object[] values)	{		command.CommandText = "insert into " + tableName + " values (" + values [0];		for (int i = 1; i < values.Length; i++) {			command.CommandText += "," + values [i];		}		command.CommandText += ")";		return command.ExecuteReader ();	}	// 增--方式2	public SqliteDataReader InsertData (string tableName, string[] cols, object[] values)	{		if (values.Length != cols.Length) {			Debug.Log ("参数个数与列数不匹配,无法进行数据的写入!");			return null;		}		// insert into 表名(列0,列1) values(值1,值2)		// into后面有空格!!!		command.CommandText = "insert into " + tableName + "(" + cols [0];		// 把每个列的名字,都加入到  表名(列0,列1		for (int i = 1; i < cols.Length; i++) {			command.CommandText += "," + cols [i];		}		command.CommandText += ")";		command.CommandText += " values(" + values [0];		for (int i = 1; i < values.Length; i++) {			command.CommandText += "," + values [i];		}		command.CommandText += ")";		return command.ExecuteReader ();	}	//-----------------------------------------	// 删	public SqliteDataReader DeleteData (string tableName)	{		// 清空当前表中所有的内容		command.CommandText = "delete from " + tableName;		return command.ExecuteReader ();	}	// 删除满足条件的指定数据	public  SqliteDataReader DeleteData (string tableName, string condition)	{		command.CommandText = "delete from " + tableName + " where " + condition;		return command.ExecuteReader ();	}	//-----------------------------------------	// 改	public SqliteDataReader UpdateData (string tableName, string[] cols, object[] values, string condition)	{		if (values.Length != cols.Length) {			Debug.Log ("输入的值的个数有误!");			return null;		}		command.CommandText = "update " + tableName + " set " + cols [0] + "=" + values [0];		for (int i = 1; i < cols.Length; i++) {			command.CommandText += "," + cols [i] + "=" + values [i];		}		command.CommandText += " where " + condition;		return command.ExecuteReader ();	}	//-----------------------------------------	// 查	// string condition = null:设置参数的默认值为null	// 调用该方法的时候,可以选择传递3个参数,或者2个参数,如果传递2个参数,第三个参数的值将使用默认值	public SqliteDataReader SelectData (string tableName, string[] cols, string condition = null)	{		command.CommandText = "select " + cols [0];		for (int i = 1; i < cols.Length; i++) {			command.CommandText += "," + cols [i];		}		command.CommandText += " from " + tableName;		if (condition != null) {			command.CommandText += " where " + condition;		}		return command.ExecuteReader ();	}	//-----------------------------------------	// 关闭数据库	public void CloseDataBase ()	{		con.Close ();		command.Cancel ();	}}

转载地址:http://bbkii.baihongyu.com/

你可能感兴趣的文章
platform_driver平台驱动注册和注销过程(下)
查看>>
.net强制退出主窗口的方法——Application.Exit()方法和Environment.Exit(0)方法
查看>>
c# 如何调用win8自带的屏幕键盘(非osk.exe)
查看>>
build/envsetup.sh 简介
查看>>
linux怎么切换到root里面?
查看>>
编译Android4.0源码时常见错误及解决办法
查看>>
Android 源码编译make的错误处理
查看>>
启用SELinux时遇到的问题
查看>>
virbr0 虚拟网卡卸载方法
查看>>
No devices detected. Fatal server error: no screens found
查看>>
新版本的linux如何生成xorg.conf
查看>>
Centos 6.0_x86-64 终于成功安装官方显卡驱动
查看>>
Linux基础教程:CentOS卸载KDE桌面
查看>>
read humor_campus
查看>>
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>