无为清净楼资源网 Design By www.qnjia.com
以下是频繁用到的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。
而且,我发现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。
标签:
函数,封装,Codeeer
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月17日
2024年11月17日
- 中国武警男声合唱团《辉煌之声1天路》[DTS-WAV分轨]
- 紫薇《旧曲新韵》[320K/MP3][175.29MB]
- 紫薇《旧曲新韵》[FLAC/分轨][550.18MB]
- 周深《反深代词》[先听版][320K/MP3][72.71MB]
- 李佳薇.2024-会发光的【黑籁音乐】【FLAC分轨】
- 后弦.2012-很有爱【天浩盛世】【WAV+CUE】
- 林俊吉.2012-将你惜命命【美华】【WAV+CUE】
- 晓雅《分享》DTS-WAV
- 黑鸭子2008-飞歌[首版][WAV+CUE]
- 黄乙玲1989-水泼落地难收回[日本天龙版][WAV+CUE]
- 周深《反深代词》[先听版][FLAC/分轨][310.97MB]
- 姜育恒1984《什么时候·串起又散落》台湾复刻版[WAV+CUE][1G]
- 那英《如今》引进版[WAV+CUE][1G]
- 蔡幸娟.1991-真的让我爱你吗【飞碟】【WAV+CUE】
- 群星.2024-好团圆电视剧原声带【TME】【FLAC分轨】