[C#] SQLite数据库

一、安装 sqlite 

前往 https://www.sqlite.org/download.html 下载 sqlite-tools-win32-*.zip 和 sqlite-dll-win32-*.zip 压缩文件

创建文件夹 C:\sqlite,并解压 上面两个压缩文件。

添加C:\sqlite 到 PATH 环境变量 ,使用 命令 C:/sqlite/sqlite3 看能否打开SQLite

二、基类代码

using UnityEngine;
using System.Data.SQLite;
using System;

public class DBBase
{
    private const string dbName = "MyDB";
    protected SQLiteConnection m_connection;
    protected SQLiteCommand m_command;
    protected SQLiteDataReader m_reader;
    public string DBPath
    {
        get
        {
            return Application.streamingAssetsPath + "/" + dbName + ".db";
        }
    }

    public void OpenDB()
    {
        try
        {
            m_connection = new SQLiteConnection(string.Format("Data Source = {0};", DBPath));
            m_connection.Open();
            Debug.Log(DBPath + "  打开数据库");
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
    }

    public void CloseDB()
    {

        if (m_command != null)
            m_command.Cancel();
        m_command = null;

        if (m_reader != null)
            m_reader.Close();
        m_reader = null;

        if (m_connection != null)
            m_connection.Close();
        m_connection = null;

        Debug.Log("关闭数据库");
    }

    public void ExecuteNonQuery(string CommandText)
    {
        try
        {
            m_command = new SQLiteCommand(m_connection);
            m_command.CommandText = CommandText;
            m_command.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }
    }


    public SQLiteDataReader ExecuteQuery(string CommandText)
    {
        try
        {
            m_command = new SQLiteCommand(m_connection);
            m_command.CommandText = CommandText;
            m_reader = m_command.ExecuteReader();
            return m_reader;
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
            throw;
        }
 
    }

}

三、使用


using System.Collections.Generic;
using System.Data.SQLite;

public class Good {
    public int Id;
    public string QrCode;
    public string Name;
    public double Price;
}

public class GoodTable : DBBase {

    public string TableName = "Goods";      //表名
    public string IDKey = "ID";             //键 
    public string QRCodeKey = "QR_CODE";
    public string NameKey = "NAME";
    public string PriceKey = "PRICE";
    List goodList = new List();


    private static GoodTable _instance;
    public static GoodTable Instance
    {
        get
        {
            if (_instance == null)
                _instance = new GoodTable();
            return _instance;
        }
    }

    public void CreateTable() {
        string commadnText = "CREATE TABLE " + TableName + " (" +
                            IDKey + " INTEGER NOT NULL PRIMARY KEY UNIQUE," +
                            QRCodeKey + " TEXT NOT NULL," +
                            NameKey + " TEXT NOT NULL," +
                            PriceKey + " FLOAT NOT NULL)";
        ExecuteNonQuery(commadnText);
    }

    public void DropTable()
    {
        string commadnText = "DROP TABLE " + TableName;
        ExecuteNonQuery(commadnText);
    }

    public bool HasTable() {
        return false;
    }

    public void Insert(string code, string name, float price)
    {
        string commadnText = string.Format("INSERT INTO {0} ({1},{2},{3}) VALUES ({4},'{5}','{6}')"
            , TableName, QRCodeKey, NameKey, PriceKey, code, name, price);
        ExecuteNonQuery(commadnText);
    }

    public void Update(float price ,int id)
    {
        string commadnText = string.Format(" UPDATE {0} SET {1} = {2} WHERE {3} = {4})"
            , TableName, PriceKey, price, IDKey, id);
        ExecuteNonQuery(commadnText);
    }

    public void Update(string name, int id)
    {
        string commadnText = string.Format(" UPDATE {0} SET {1} = '{2}' WHERE {3} = {4})"
            , TableName, NameKey, name, IDKey, id);
        ExecuteNonQuery(commadnText);
    }

    public void Delete(int id)
    {
        string commadnText = string.Format(" DELETE FROM {0} WHERE {1} = {2}"
            , TableName, IDKey, id);
        ExecuteNonQuery(commadnText);
    }

    public List QueryAll()
    {
        string commandText = "SELECT * FROM " + TableName;
        SQLiteDataReader reader = ExecuteQuery(commandText);
        int index = 0;
        while (reader.Read())
        {
            Good g = new Good();
            g.Id = (int)reader.GetValue(0);
            g.QrCode = (string)reader.GetValue(1);
            g.Name = (string)reader.GetValue(2);
            g.Price = (double)reader.GetValue(3);
            goodList.Add(g);
            index++;
        }
        return goodList;
    }
}

 

 

你可能感兴趣的:(C#)