• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Sqlite 常用函数封装提高Codeeer的效率
    以下是频繁用到的Sqlite函数,内容格式相对固定,封装一下有助于提高开发效率(^_^至少提高Codeeer的效率了)

    而且,我发现Sqlite中文资料比较少,起码相对其他找起来要复杂些,服务一下大众~
    我没有封装读取部分,因为数据库读取灵活性太大,封装起来难度也大,而且就算封装好了,也难以应付所有情况,还是建议根据实际情况设计代码逻辑。

    创建
    复制代码 代码如下:

    /// summary>
    /// Creat New Sqlite File
    /// /summary>
    /// param name="NewTable">New Table Name/param>
    /// param name="NewWords">Words list of the New Table/param>
    /// returns>IsSuccessful/returns>
    public static bool Creat(string DataSource, string NewTable, Liststring> NewWords)
    {
    try
    {
    //Creat Data File
    SQLiteConnection.CreateFile(DataSource);
    //Creat Table
    using (DbConnection conn = SQLiteFactory.Instance.CreateConnection())
    {
    //Connect
    conn.ConnectionString = "Data Source=" + DataSource;
    conn.Open();
    //Creat
    string Bazinga = "create table [" + NewTable + "] (";
    foreach (string Words in NewWords)
    {
    Bazinga += "[" + Words + "] BLOB COLLATE NOCASE,";
    }
    //Set Primary Key
    //The Top item from the "NewWords"
    Bazinga += @"PRIMARY KEY ([" + NewWords[0] + "]))";
    DbCommand cmd = conn.CreateCommand();
    cmd.Connection = conn;
    cmd.CommandText = Bazinga;
    cmd.ExecuteNonQuery();
    }
    return true;
    }
    catch (Exception E)
    {
    MessageBox.Show(E.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return false;
    }
    }

    删除
    复制代码 代码如下:

    /// summary>
    /// Delete Date
    /// /summary>
    /// param name="DataSource">/param>
    /// param name="TargetTable">/param>
    /// param name="Word">/param>
    /// param name="Value">/param>
    /// returns>/returns>
    public static bool Delete(string DataSource, string TargetTable, string Word, string Value)
    {
    try
    {
    //Connect
    using (DbConnection conn = SQLiteFactory.Instance.CreateConnection())
    {
    conn.ConnectionString = "Data Source=" + DataSource;
    conn.Open();
    DbCommand cmd = conn.CreateCommand();
    cmd.Connection = conn;
    //Delete
    cmd.CommandText = "Delete From " + TargetTable + " where [" + Word + "] = '" + Value + "'";
    cmd.ExecuteNonQuery();
    }
    return true;
    }
    catch (Exception E)
    {
    MessageBox.Show(E.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return false;
    }
    }

    插入
    这里要说明下,因为存在多字段同时插入的情况(何止存在,很普遍- -。没见过谁的数据库像意大利面条一样)

    在这里设计了Insert结构用以储存字段和值的关系(曾考虑过用数组的办法实现,可是那玩意不太方便调用,瞅着挺抽象的,不太好用,如果有更好的建议,欢迎留言~)
    复制代码 代码如下:

    /// summary>
    /// Use to format Insert column's value
    /// /summary>
    public struct InsertBag
    {
    public string ColumnName;
    public string Value;
    public InsertBag(string Column, string value)
    {
    ColumnName = Column;
    Value = value;
    }
    }

    以下为插入模块的主函数
    复制代码 代码如下:

    /// summary>
    /// Insert Data
    /// /summary>
    /// param name="DataSource">/param>
    /// param name="TargetTable">/param>
    /// param name="InsertBags">struck of InsertBag/param>
    /// returns>/returns>
    public static bool Insert(string DataSource, string TargetTable, ListInsertBag> InsertBags)
    {
    try
    {
    using (DbConnection conn = SQLiteFactory.Instance.CreateConnection())
    {
    //Connect Database
    conn.ConnectionString = "Data Source=" + DataSource;
    conn.Open();
    //Deal InsertBags
    StringBuilder ColumnS = new StringBuilder();
    StringBuilder ValueS = new StringBuilder();
    for (int i = 0; i InsertBags.Count; i++)
    {
    ColumnS.Append(InsertBags[i].ColumnName + ",");
    ValueS.Append("'" + InsertBags[i].Value + "',");
    }
    if (InsertBags.Count == 0)
    {
    throw new Exception("InsertBag 数据包为空,睁大你的狗眼……");
    }
    else
    {
    //Drop the last "," from the ColumnS and ValueS
    ColumnS = ColumnS.Remove(ColumnS.Length - 1, 1);
    ValueS = ValueS.Remove(ValueS.Length - 1, 1);
    }
    //Insert
    DbCommand cmd = conn.CreateCommand();
    cmd.CommandText = "insert into [" + TargetTable + "] (" + ColumnS.ToString() + ") values (" + ValueS.ToString() + ")";
    cmd.ExecuteNonQuery();
    return true;
    }
    }
    catch (Exception E)
    {
    MessageBox.Show(E.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    return false;
    }
    }

    目测有点复杂呢,来个Demo,有必要说下,“W2”和“W44”是已经设计好的字段,而“TableTest”是已经添加好的表段
    复制代码 代码如下:

    ListSqlite.InsertBag> Lst = new ListSqlite.InsertBag>();
    Lst.Add(new Sqlite.InsertBag("W2", "222222222"));
    Lst.Add(new Sqlite.InsertBag("W44", "4444444"));
    Sqlite.Insert(@"D:\1.Sql3", "TableTest", Lst);

    表段获取
    复制代码 代码如下:

    /// summary>
    /// Get Tables From Sqlite
    /// /summary>
    /// returns>list of Tables/returns>
    public static Liststring> GetTables(string DataSource)
    {
    Liststring> ResultLst = new Liststring>();
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + DataSource))
    {
    conn.Open();
    using (SQLiteCommand tablesGet = new SQLiteCommand("SELECT name from sqlite_master where type='table'", conn))
    {
    using (SQLiteDataReader tables = tablesGet.ExecuteReader())
    {
    while (tables.Read())
    {
    try
    {
    ResultLst.Add(tables[0].ToString());
    }
    catch (Exception E)
    {
    MessageBox.Show(E.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    }
    }
    }
    return ResultLst;
    }

    字段获取
    复制代码 代码如下:

    /// summary>
    /// Get Words From Table->Sqlite
    /// /summary>
    /// param name="TargetTable">Target Table/param>
    /// returns>list of Words/returns>
    public static Liststring> GetWords(string DataSource,string TargetTable)
    {
    Liststring> WordsLst = new Liststring>();
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + DataSource))
    {
    conn.Open();
    using (SQLiteCommand tablesGet = new SQLiteCommand(@"SELECT * FROM " + TargetTable, conn))
    {
    using (SQLiteDataReader Words = tablesGet.ExecuteReader())
    {
    try
    {
    for (int i = 0; i Words.FieldCount; i++)
    {
    WordsLst.Add(Words.GetName(i));
    }
    }
    catch (Exception E)
    {
    MessageBox.Show(E.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    }
    }
    return WordsLst;
    }

    解释下,为啥代码中的注释基本都用英文写了,因为这段时间在学双拼- -。可是还不太熟悉,打字超慢,而且Code的时候容易打断思路,好在~英文不多,而且这些都看不懂的话你……你要向我解释一下你是怎么一路学到数据库的 0。
    您可能感兴趣的文章:
    • 让Sqlite脱离VC++ Runtime独立运行的方法
    • C++操作SQLite简明教程
    • SQLite 入门教程三 好多约束 Constraints
    • C#中使用SQLite数据库的方法介绍
    • ASP.NET(C#)中操作SQLite数据库实例
    • VC++基于Dx实现的截图程序示例代码
    • VC++实现输出GIF到窗体并显示GIF动画的方法
    • VC++开发中完美解决头文件相互包含问题的方法解析
    • 浅析VC++中的头文件包含问题
    • VC++操作SQLite简单实例
    上一篇:扩展 Entity Framework支持复杂的过滤条件(多个关键字模糊匹配)
    下一篇:ADO.NET中的五个主要对象的详细介绍与应用
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    Sqlite 常用函数封装提高Codeeer的效率 Sqlite,常用,函数,封装,提高,