sqlite的安全性没用mySql高,因为sqlite不用创建用户,以及权限设置,sqlite是单机数据库,功能简约,小型化,MySql试用于服务器数据量大功能多需要安装,例如网站访问量比较大的数据管理
其实MySQL与Sqlite中的数据库操作函数基本上都一样,没什么区别,主要区别就是权限和库大小
注意:在使用这个sqlite数据管理时,也要把,几个需要的动态库放到Plugins里面(Mono.Data.Sqlite, sqlite3, System.Data)这几个动态库百度都能搜到
1.先新建一个数据库封装类DbAccess
using System;
using System.Collections;
using Mono.Data.Sqlite;
using UnityEngine;
public class DbAccess
{
private SqliteConnection dbConnection;
private SqliteCommand dbCommand;
private SqliteDataReader reader;
public DbAccess (string connectionString)
{
OpenDB (connectionString);
}
//打开数据库
public void OpenDB (string connectionString)
{
try
{
dbConnection = new SqliteConnection (connectionString);
dbConnection.Open ();
Debug.Log ("Connected to db");
}
catch(Exception e)
{
string temp1 = e.ToString();
Debug.Log(temp1);
}
}
//关闭数据库
public void CloseSqlConnection ()
{
if (dbCommand != null)
{
dbCommand.Dispose ();
}
dbCommand = null;
if (reader != null)
{
reader.Dispose ();
}
reader = null;
if (dbConnection != null)
{
dbConnection.Close ();
}
dbConnection = null;
Debug.Log ("Disconnected from db.");
}
//执行SQL语句
public SqliteDataReader ExecuteQuery (string sqlQuery)
{
dbCommand = dbConnection.CreateCommand ();
dbCommand.CommandText = sqlQuery;
reader = dbCommand.ExecuteReader ();
return reader;
}
//查询整个表格
public SqliteDataReader ReadFullTable (string tableName)
{
string query = "SELECT * FROM " + tableName;
return ExecuteQuery (query);
}
//插入函数
public SqliteDataReader InsertInto (string tableName, string[] values)
{
string query = "INSERT INTO " + tableName + " VALUES (" + values[0];
for (int i = 1; i < values.Length; ++i)
{
query += ", " + values[i];
}
query += ")";
return ExecuteQuery (query);
}
//修改表格
public SqliteDataReader UpdateInto (string tableName, string []cols,string []colsvalues,string selectkey,string selectvalue)
{
string query = "UPDATE "+tableName+" SET "+cols[0]+" = "+colsvalues[0];
for (int i = 1; i < colsvalues.Length; ++i)
{
query += ", " +cols[i]+" ="+ colsvalues[i];
}
query += " WHERE "+selectkey+" = "+selectvalue+" ";
return ExecuteQuery (query);
}
//删除表格元组
public SqliteDataReader Delete(string tableName,string []cols,string []colsvalues)
{
string query = "DELETE FROM "+tableName + " WHERE " +cols[0] +" = " + colsvalues[0];
for (int i = 1; i < colsvalues.Length; ++i) {
query += " or " +cols[i]+" = "+ colsvalues[i];
}
Debug.Log(query);
return ExecuteQuery (query);
}
//特定插入元组
public SqliteDataReader InsertIntoSpecific (string tableName, string[] cols, string[] values)
{
if (cols.Length != values.Length) {
throw new SqliteException ("columns.Length != values.Length");
}
string query = "INSERT INTO " + tableName + "(" + cols[0];
for (int i = 1; i < cols.Length; ++i) {
query += ", " + cols[i];
}
query += ") VALUES (" + values[0];
for (int i = 1; i < values.Length; ++i) {
query += ", " + values[i];
}
query += ")";
return ExecuteQuery (query);
}
//整个表格内容删除
public SqliteDataReader DeleteContents (string tableName)
{
string query = "DELETE FROM " + tableName;
return ExecuteQuery (query);
}
//创建表格
public SqliteDataReader CreateTable (string name, string[] col, string[] colType)
{
if (col.Length != colType.Length) {
throw new SqliteException ("columns.Length != colType.Length");
}
string query = "CREATE TABLE " + name + " (" + col[0] + " " + colType[0];
for (int i = 1; i < col.Length; ++i) {
query += ", " + col[i] + " " + colType[i];
}
query += ")";
return ExecuteQuery (query);
}
//根据条件查询
public SqliteDataReader SelectWhere (string tableName, string[] items, string[] col, string[] operation, string[] values)
{
if (col.Length != operation.Length || operation.Length != values.Length) {
throw new SqliteException ("col.Length != operation.Length != values.Length");
}
string query = "SELECT " + items[0];
for (int i = 1; i < items.Length; ++i) {
query += ", " + items[i];
}
query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";
for (int i = 1; i < col.Length; ++i) {
query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";
}
return ExecuteQuery (query);
}
}
2.下面新建一个类用来对数据哭进行操作,这个脚本挂在一个激活的gameobject上就可以了
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
public class Test : MonoBehaviour
{
void Start()
{
//数据库文件存储地址
string appDBPath = Application.dataPath + "/MZ.db";
DbAccess db = new DbAccess(@"Data Source=" + appDBPath);
path = appDBPath;
if(!System.IO.File.Exists(@"Data Source=" + appDBPath))
{
//创建表格
db.CreateTable("CQ", new string[] { "name", "qq", "email", "blog" }, new string[] { "text", "text", "text", "text" });
}
//插入数据
db.InsertInto("CQ",new string[] {"'XQ'", "'520520'", "'520@gmail.com'", "'www.blog1.com'"});
db.InsertInto("CQ", new string[] { "'XX'", "'552200'", "'555@gmail.com'", "'www.blog2.com'" });
db.InsertInto("CQ", new string[] { "'XQ'", "'520520'", "'222@gmail.com'", "'www.blog3.com'" });
//删除数据
db.Delete("CQ", new string[] { "email", "email" }, new string[] { "'520@gmail.com'", "'555@gmail.com'" });
using (SqliteDataReader sqReader = db.SelectWhere("CQ", new string[] { "name", "email" }, new string[] { "qq" }, new string[] { "=" }, new string[] { "520520" }))
{
while (sqReader.Read())
{
//目前中文无法显示
Debug.Log("CQ" + sqReader.GetString(sqReader.GetOrdinal("name")));
Debug.Log("CQ" + sqReader.GetString(sqReader.GetOrdinal("email")));
}
sqReader.Close();
}
db.CloseSqlConnection();
}
}