• 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);
                }
                
            }
        }
    }
  • 相关阅读:
    关于Windows 2000,IIS 5/4下面,Form内容超过200K解决办法!
    获取客户端网卡MAC地址和IP地址的几种方法
    关于Java的异常捕获
    redis的swap
    Linux下C++程序获取运行时间的一种方式
    CentOS 修改IP地址, DNS, 网关[内容来源网络]
    [转]ubuntu12.04搭建ruby on rails 环境
    rails环境 bundle 时无法安装nokogiri 完美解决
    SQL Server – 冷知识 (新手)
    Typescript – 学习笔记 tsconfig.json
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14269317.html
Copyright © 2020-2023  润新知