1. 程式人生 > 實用技巧 >SQlite常用操作封裝

SQlite常用操作封裝

using UnityEngine;
using System;
using System.Collections.Generic;

public abstract class ISQLOperation 
{
    public abstract void CreateDateBase(string path);
    public abstract void DeletDateBase(string name);
    public abstract void Instert(System.Object obj);
    public abstract List<T> Select<T>(string
condition); public abstract void Update(System.Object sql); public abstract void DeletTable(string tableName, string condition); public abstract void CreatTable<T>(); public abstract void Execute(string sql); }
using System.Collections;
using System.Collections.Generic;
using
UnityEngine; using SQLite4Unity3d; using System; using System.IO; using System.Linq.Expressions; using System.Linq; public class SqlliteOperations : ISQLOperation { public SqlliteOperations() { CreateDateBase(DefaultName.dateBasePath); CreatTable<User>(); }
public SQLiteConnection connection; /// <summary> /// 建立庫 /// </summary> /// <param name="path"></param> /// <param name="name"></param> public override void CreateDateBase(string path) { try { connection = new SQLiteConnection(path, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create); } catch (Exception e) { Debug.LogError(e.Message+",建立資料庫失敗。"); } } /// <summary> /// 刪資料不刪表 /// </summary> /// <param name="tableName"></param> public override void DeletTable(string tableName,string condition) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { SQLiteCommand liteCommand = connection.CreateCommand("delete from " + tableName + " where " + connection); liteCommand.ExecuteNonQuery(); } catch (Exception e) { Debug.LogError(e.Message+",刪除資料庫資料錯誤。"); } } } /// <summary> /// 刪庫 /// </summary> /// <param name="name"></param> public override void DeletDateBase(string name) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { SQLiteCommand liteCommand = connection.CreateCommand("drop datebase " + name); liteCommand.ExecuteNonQuery(); } catch (Exception e) { Debug.LogError(e.Message + ",刪除資料庫錯誤。"); } } } //"UPDATE User SET Password = \"222222\" WHERE UserName = \"admin\"" /// <summary> /// 修改表 /// </summary> /// <param name="obj"></param> public override void Update(object obj) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { connection.Update(obj); } catch (Exception e) { Debug.LogError(e.Message + ",修改資料庫錯誤:"+ obj.ToString()); } } } /// <summary> /// 建立表 /// </summary> /// <typeparam name="T"></typeparam> public override void CreatTable<T>() { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { connection.CreateTable<T>(); } catch (Exception e) { Debug.LogError(e.Message + ",建立表錯誤:"+typeof(T).Name); } } } /// <summary> /// 插入 /// </summary> /// <param name="obj"></param> public override void Instert(System.Object obj) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { connection.Insert(obj); } catch (Exception e) { Debug.LogError(e.Message + ",插入表錯誤:" + obj.ToString()); } } } /// <summary> /// 查詢表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <returns></returns> public override List<T> Select<T>(string sql) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { SQLiteCommand command = connection.CreateCommand(sql); List<T> list = command.ExecuteQuery<T>(); return list; } catch (Exception e) { Debug.LogError(e.Message + ",查詢表錯誤:" + typeof(T).Name); return null; } } } /// <summary> /// 執行任意sql語句 /// </summary> /// <param name="sql"></param> public override void Execute(string sql) { using (SQLiteConnection connection = new SQLiteConnection(DefaultName.dateBasePath)) { try { SQLiteCommand liteCommand = connection.CreateCommand(sql); liteCommand.ExecuteNonQuery(); } catch (Exception e) { Debug.LogError(e.Message + ",執行sql語句錯誤:"+ sql); } } } }