<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        Sqlite 常用函數封裝提高Codeeer的效率

        來源:懂視網 責編:小采 時間:2020-11-27 22:42:07
        文檔

        Sqlite 常用函數封裝提高Codeeer的效率

        Sqlite 常用函數封裝提高Codeeer的效率:以下是頻繁用到的Sqlite函數,內容格式相對固定,封裝一下有助于提高開發效率(^_^至少提高Codeeer的效率了) 而且,我發現Sqlite中文資料比較少,起碼相對其他找起來要復雜些,服務一下大眾~ 我沒有封裝讀取部分,因為數據庫讀取靈活性太大,封裝起來難度也
        推薦度:
        導讀Sqlite 常用函數封裝提高Codeeer的效率:以下是頻繁用到的Sqlite函數,內容格式相對固定,封裝一下有助于提高開發效率(^_^至少提高Codeeer的效率了) 而且,我發現Sqlite中文資料比較少,起碼相對其他找起來要復雜些,服務一下大眾~ 我沒有封裝讀取部分,因為數據庫讀取靈活性太大,封裝起來難度也

        以下是頻繁用到的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, List<string> 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, List<InsertBag> 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”是已經添加好的表段
        代碼如下:

        List<Sqlite.InsertBag> Lst = new List<Sqlite.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 List<string> GetTables(string DataSource)
        {
        List<string> ResultLst = new List<string>();
        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 List<string> GetWords(string DataSource,string TargetTable)
        {
        List<string> WordsLst = new List<string>();
        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。

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Sqlite 常用函數封裝提高Codeeer的效率

        Sqlite 常用函數封裝提高Codeeer的效率:以下是頻繁用到的Sqlite函數,內容格式相對固定,封裝一下有助于提高開發效率(^_^至少提高Codeeer的效率了) 而且,我發現Sqlite中文資料比較少,起碼相對其他找起來要復雜些,服務一下大眾~ 我沒有封裝讀取部分,因為數據庫讀取靈活性太大,封裝起來難度也
        推薦度:
        標簽: 效率 封裝 SQLite
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 午夜不卡久久精品无码免费| 毛片免费观看网站| 亚洲无圣光一区二区| 精品国产麻豆免费网站| 一个人看www免费高清字幕| 亚洲电影唐人社一区二区| 国产精品无码免费视频二三区| 国产免费人成视频尤勿视频| 亚洲视频在线观看网址| 又粗又大又长又爽免费视频| 国产午夜精品免费一区二区三区| 亚洲性色AV日韩在线观看| 亚洲码国产精品高潮在线| 中文字幕无码免费久久99| 一级特黄色毛片免费看| 亚洲AV无码乱码麻豆精品国产| 亚洲精品tv久久久久久久久久| 久久精品无码专区免费青青| 国产成人综合亚洲| 亚洲国色天香视频| 国产成人综合亚洲AV第一页| 妞干网免费视频观看| 中文字幕无码日韩专区免费| 毛片亚洲AV无码精品国产午夜| 精品亚洲成AV人在线观看| 亚洲精品乱码久久久久久蜜桃| 成人黄色免费网站| 中文字幕免费在线看电影大全| 亚洲色欲色欱wwW在线| 久久久亚洲欧洲日产国码二区| www.亚洲精品.com| 成人免费毛片视频| 99久久精品免费视频| 好吊色永久免费视频大全| 亚洲第一第二第三第四第五第六| 亚洲人成影院在线| 国产亚洲情侣一区二区无码AV| 日韩黄色免费观看| 免费不卡视频一卡二卡| 免费成人高清在线视频| 岛国精品一区免费视频在线观看|