原文地址:http://www.jb51.cc/article/p-unrjqlwc-gu.html
以下是频繁用到的sqlite函数,内容格式相对固定,封装一下有助于提高开发效率(^_^至少提高Codeeer的效率了)
而且,我发现sqlite中文资料比较少,起码相对其他找起来要复杂些,服务一下大众~
我没有封装读取部分,因为数据库读取灵活性太大,封装起来难度也大,而且就算封装好了,也难以应付所有情况,还是建议根据实际情况设计代码逻辑。
解释下,为啥代码中的注释基本都用英文写了,因为这段时间在学双拼- -。可是还不太熟悉,打字超慢,而且Code的时候容易打断思路,好在~英文不多,而且这些都看不懂的话你……你要向我解释一下你是怎么一路学到数据库的 0。0
- ///<summary>
- ///ChangePassword
- ///</summary>
- ///<paramname="DataSource"></param>
- ///<paramname="OldPassword"></param>
- ///<paramname="NewPassword"></param>
- ///<returns></returns>
- publicstaticboolPasswordChange(stringDataSource,stringOldPassword,stringNewPassword)
- {
- try
- {
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=OldPassword;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- //ChangePassword
- conn.ChangePassword(NewPassword);
- }
- returntrue;
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
- returnfalse;
- }
- }
/// <summary> /// Change Password /// </summary> /// <param name="DataSource"></param> /// <param name="OldPassword"></param> /// <param name="NewPassword"></param> /// <returns></returns> public static bool PasswordChange(string DataSource,string OldPassword,string NewPassword) { try { using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = OldPassword; conn.ConnectionString = SCS.ToString(); conn.Open(); //Change Password conn.ChangePassword(NewPassword); } return true; } catch (Exception E) { MessageBox.Show(E.Message,"提示",MessageBoxIcon.Information); return false; } }
创建数据库
- ///<summary>
- ///CreatNewsqliteFile
- ///</summary>
- ///<paramname="NewTable">NewTableName</param>
- ///<paramname="NewWords">WordslistoftheNewTable</param>
- ///<paramname="CreatNew">CreatorAddtotheDatabase(UsingtomakeMultiTables)</param>
- ///<returns></returns>
- publicstaticboolCreat(stringDataSource,stringNewTable,List<string>NewWords,stringPassWord=null,boolCreatNew=true)
- {
- try
- {
- if(CreatNew)
- {
- //CreatDataFile
- sqliteConnection.CreateFile(DataSource);
- }
- //CreatTable
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- //Creat
- stringBazinga="createtable["+NewTable+"](";
- foreach(stringWordsinNewWords)
- {
- Bazinga+="["+Words+"]TEXTCOLLATENOCASE,";
- }
- //SetPrimaryKey
- //TheTopitemfromthe"NewWords"
- Bazinga+=@"PRIMARYKEY(["+NewWords[0]+"]))";
- DbCommandcmd=conn.CreateCommand();
- cmd.Connection=conn;
- cmd.CommandText=Bazinga;
- cmd.ExecuteNonQuery();
- }
- returntrue;
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- returnfalse;
- }
- }
/// <summary> /// Creat New sqlite File /// </summary> /// <param name="NewTable">New Table Name</param> /// <param name="NewWords">Words list of the New Table</param> /// <param name="CreatNew">Creat or Add to the Database(Using to make Multi Tables)</param> /// <returns></returns> public static bool Creat(string DataSource,string NewTable,List<string> NewWords,string PassWord = null,bool CreatNew = true) { try { if (CreatNew) { //Creat Data File sqliteConnection.CreateFile(DataSource); } //Creat Table using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); conn.Open(); //Creat string Bazinga = "create table [" + NewTable + "] ("; foreach (string Words in NewWords) { Bazinga += "[" + Words + "] TEXT 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,MessageBoxIcon.Information); return false; } }
获取表段
- ///<summary>
- ///GetTablesFromsqlite
- ///</summary>
- ///<returns>listofTables</returns>
- publicstaticList<string>GetTables(stringDataSource,stringPassWord=null)
- {
- List<string>ResultLst=newList<string>();
- try
- {
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- using(sqliteCommandtablesGet=newsqliteCommand("SELECTnamefromsqlite_masterwheretype='table'",conn))
- {
- using(sqliteDataReadertables=tablesGet.ExecuteReader())
- {
- while(tables.Read())
- {
- try
- {
- ResultLst.Add(tables[0].ToString());
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- }
- }
- }
- }
- }
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- }
- returnResultLst;
- }
/// <summary> /// Get Tables From sqlite /// </summary> /// <returns>list of Tables</returns> public static List<string> GetTables(string DataSource,string PassWord = null) { List<string> ResultLst = new List<string>(); try { using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); 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,MessageBoxIcon.Information); } } } } } } catch (Exception E) { MessageBox.Show(E.Message,MessageBoxIcon.Information); } return ResultLst; }
获取字段
- ///<summary>
- ///GetWordsFromTable->sqlite
- ///</summary>
- ///<paramname="TargetTable">TargetTable</param>
- ///<returns>listofWords</returns>
- publicstaticList<string>GetWords(stringDataSource,stringTargetTable,stringPassWord=null)
- {
- List<string>WordsLst=newList<string>();
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- using(sqliteCommandtablesGet=newsqliteCommand(@"SELECT*FROM"+TargetTable,conn))
- {
- using(sqliteDataReaderWords=tablesGet.ExecuteReader())
- {
- try
- {
- for(inti=0;i<Words.FieldCount;i++)
- {
- WordsLst.Add(Words.GetName(i));
- }
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- }
- }
- }
- }
- returnWordsLst;
- }
/// <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,string PassWord = null) { List<string> WordsLst = new List<string>(); using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); 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,MessageBoxIcon.Information); } } } } return WordsLst; }
取值
- ///<summary>
- ///GetValuesFromsqlite
- ///</summary>
- ///<returns>listofValues</returns>
- publicstaticList<string>GetValues(stringDataSource,stringsql,stringGetColumu,stringPassWord=null)
- {
- List<string>ResultLst=newList<string>();
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- using(sqliteCommandcmd=newsqliteCommand(sql,conn))
- {
- using(sqliteDataReaderdr=cmd.ExecuteReader())
- {
- while(dr.Read())
- {
- try
- {
- ResultLst.Add(dr[GetColumu].ToString());
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- }
- }
- }
- }
- }
- returnResultLst;
- }
/// <summary> /// Get Values From sqlite /// </summary> /// <returns>list of Values</returns> public static List<string> GetValues(string DataSource,string sql,string GetColumu,string PassWord = null) { List<string> ResultLst = new List<string>(); using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); conn.Open(); using (sqliteCommand cmd = new sqliteCommand(sql,conn)) { using (sqliteDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { try { ResultLst.Add(dr[GetColumu].ToString()); } catch (Exception E) { MessageBox.Show(E.Message,MessageBoxIcon.Information); } } } } } return ResultLst; }
插入数据
- ///<summary>
- ///InsertData
- ///</summary>
- ///<paramname="DataSource"></param>
- ///<paramname="TargetTable"></param>
- ///<returns></returns>
- publicstaticboolInsert(stringDataSource,stringColumnS,stringValueS,stringPassWord=null)
- {
- try
- {
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- //Insert
- DbCommandcmd=conn.CreateCommand();
- cmd.CommandText="insertinto["+TargetTable+"]("+ColumnS+")values("+ValueS+")";
- cmd.ExecuteNonQuery();
- returntrue;
- }
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- returnfalse;
- }
- }
/// <summary> /// Insert Data /// </summary> /// <param name="DataSource"></param> /// <param name="TargetTable"></param> /// <returns></returns> public static bool Insert(string DataSource,string ColumnS,string ValueS,string PassWord = null) { try { using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); conn.Open(); //Insert DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into [" + TargetTable + "] (" + ColumnS + ") values (" + ValueS + ")"; cmd.ExecuteNonQuery(); return true; } } catch (Exception E) { MessageBox.Show(E.Message,MessageBoxIcon.Information); return false; } }
删除数据
- ///<summary>
- ///DeleteDate
- ///</summary>
- ///<paramname="DataSource"></param>
- ///<paramname="TargetTable"></param>
- ///<paramname="Word"></param>
- ///<paramname="Value"></param>
- ///<returns></returns>
- publicstaticboolDelete(stringDataSource,stringWord,stringValue,stringPassWord=null)
- {
- try
- {
- //Connect
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- DbCommandcmd=conn.CreateCommand();
- cmd.Connection=conn;
- //Delete
- cmd.CommandText="DeleteFrom"+TargetTable+"where["+Word+"]='"+Value+"'";
- cmd.ExecuteNonQuery();
- }
- returntrue;
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- returnfalse;
- }
- }
/// <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 Word,string Value,string PassWord = null) { try { //Connect using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); 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,MessageBoxIcon.Information); return false; } }
执行sqlite指令
- ///<summary>
- ///sqliteCommand
- ///</summary>
- ///<paramname="DataSource"></param>
- ///<paramname="sql"></param>
- ///<returns></returns>
- publicstaticboolsqlCommand(stringDataSource,stringPassWord=null)
- {
- try
- {
- using(sqliteConnectionconn=newsqliteConnection())
- {
- sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
- SCS.DataSource=DataSource;
- SCS.Password=PassWord;
- conn.ConnectionString=SCS.ToString();
- conn.Open();
- using(sqliteCommandcmd_Re=newsqliteCommand(sql,conn))
- {
- cmd_Re.ExecuteNonQuery();
- }
- }
- returntrue;
- }
- catch(ExceptionE)
- {
- MessageBox.Show(E.Message,MessageBoxIcon.Information);
- returnfalse;
- }
- }
/// <summary> /// sqliteCommand /// </summary> /// <param name="DataSource"></param> /// <param name="sql"></param> /// <returns></returns> public static bool sqlCommand(string DataSource,string PassWord = null) { try { using (sqliteConnection conn = new sqliteConnection()) { sqliteConnectionStringBuilder SCS = new sqliteConnectionStringBuilder(); SCS.DataSource = DataSource; SCS.Password = PassWord; conn.ConnectionString = SCS.ToString(); conn.Open(); using (sqliteCommand cmd_Re = new sqliteCommand(sql,conn)) { cmd_Re.ExecuteNonQuery(); } } return true; } catch (Exception E) { MessageBox.Show(E.Message,MessageBoxIcon.Information); return false; } }原文链接:https://www.f2er.com/sqlite/201239.html