Sqlite 常用函数封装:修改密码,创建数据库、获取表段、字段,取值、插入、删除数据、执行Sqlite指令

前端之家收集整理的这篇文章主要介绍了Sqlite 常用函数封装:修改密码,创建数据库、获取表段、字段,取值、插入、删除数据、执行Sqlite指令前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址:http://www.jb51.cc/article/p-unrjqlwc-gu.html

以下是频繁用到的sqlite函数内容格式相对固定,封装一下有助于提高开发效率(^_^至少提高Codeeer的效率了)

而且,我发现sqlite中文资料比较少,起码相对其他找起来要复杂些,服务一下大众~

我没有封装读取部分,因为数据库读取灵活性太大,封装起来难度也大,而且就算封装好了,也难以应付所有情况,还是建议根据实际情况设计代码逻辑。

解释下,为啥代码中的注释基本都用英文写了,因为这段时间在学双拼- -。可是还不太熟悉,打字超慢,而且Code的时候容易打断思路,好在~英文不多,而且这些都看不懂的话你……你要向我解释一下你是怎么一路学到数据库的 0。0


修改密码

  1. ///<summary>
  2. ///ChangePassword
  3. ///</summary>
  4. ///<paramname="DataSource"></param>
  5. ///<paramname="OldPassword"></param>
  6. ///<paramname="NewPassword"></param>
  7. ///<returns></returns>
  8. publicstaticboolPasswordChange(stringDataSource,stringOldPassword,stringNewPassword)
  9. {
  10. try
  11. {
  12. using(sqliteConnectionconn=newsqliteConnection())
  13. {
  14. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  15. SCS.DataSource=DataSource;
  16. SCS.Password=OldPassword;
  17. conn.ConnectionString=SCS.ToString();
  18. conn.Open();
  19. //ChangePassword
  20. conn.ChangePassword(NewPassword);
  21. }
  22. returntrue;
  23. }
  24. catch(ExceptionE)
  25. {
  26. MessageBox.Show(E.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  27. returnfalse;
  28. }
  29. }


创建数据库

  1. ///<summary>
  2. ///CreatNewsqliteFile
  3. ///</summary>
  4. ///<paramname="NewTable">NewTableName</param>
  5. ///<paramname="NewWords">WordslistoftheNewTable</param>
  6. ///<paramname="CreatNew">CreatorAddtotheDatabase(UsingtomakeMultiTables)</param>
  7. ///<returns></returns>
  8. publicstaticboolCreat(stringDataSource,stringNewTable,List<string>NewWords,stringPassWord=null,boolCreatNew=true)
  9. {
  10. try
  11. {
  12. if(CreatNew)
  13. {
  14. //CreatDataFile
  15. sqliteConnection.CreateFile(DataSource);
  16. }
  17. //CreatTable
  18. using(sqliteConnectionconn=newsqliteConnection())
  19. {
  20. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  21. SCS.DataSource=DataSource;
  22. SCS.Password=PassWord;
  23. conn.ConnectionString=SCS.ToString();
  24. conn.Open();
  25. //Creat
  26. stringBazinga="createtable["+NewTable+"](";
  27. foreach(stringWordsinNewWords)
  28. {
  29. Bazinga+="["+Words+"]TEXTCOLLATENOCASE,";
  30. }
  31. //SetPrimaryKey
  32. //TheTopitemfromthe"NewWords"
  33. Bazinga+=@"PRIMARYKEY(["+NewWords[0]+"]))";
  34. DbCommandcmd=conn.CreateCommand();
  35. cmd.Connection=conn;
  36. cmd.CommandText=Bazinga;
  37. cmd.ExecuteNonQuery();
  38. }
  39. returntrue;
  40. }
  41. catch(ExceptionE)
  42. {
  43. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  44. returnfalse;
  45. }
  46. }


获取表段

  1. ///<summary>
  2. ///GetTablesFromsqlite
  3. ///</summary>
  4. ///<returns>listofTables</returns>
  5. publicstaticList<string>GetTables(stringDataSource,stringPassWord=null)
  6. {
  7. List<string>ResultLst=newList<string>();
  8. try
  9. {
  10. using(sqliteConnectionconn=newsqliteConnection())
  11. {
  12. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  13. SCS.DataSource=DataSource;
  14. SCS.Password=PassWord;
  15. conn.ConnectionString=SCS.ToString();
  16. conn.Open();
  17. using(sqliteCommandtablesGet=newsqliteCommand("SELECTnamefromsqlite_masterwheretype='table'",conn))
  18. {
  19. using(sqliteDataReadertables=tablesGet.ExecuteReader())
  20. {
  21. while(tables.Read())
  22. {
  23. try
  24. {
  25. ResultLst.Add(tables[0].ToString());
  26. }
  27. catch(ExceptionE)
  28. {
  29. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. catch(ExceptionE)
  37. {
  38. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  39. }
  40. returnResultLst;
  41. }


获取字段

  1. ///<summary>
  2. ///GetWordsFromTable->sqlite
  3. ///</summary>
  4. ///<paramname="TargetTable">TargetTable</param>
  5. ///<returns>listofWords</returns>
  6. publicstaticList<string>GetWords(stringDataSource,stringTargetTable,stringPassWord=null)
  7. {
  8. List<string>WordsLst=newList<string>();
  9. using(sqliteConnectionconn=newsqliteConnection())
  10. {
  11. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  12. SCS.DataSource=DataSource;
  13. SCS.Password=PassWord;
  14. conn.ConnectionString=SCS.ToString();
  15. conn.Open();
  16. using(sqliteCommandtablesGet=newsqliteCommand(@"SELECT*FROM"+TargetTable,conn))
  17. {
  18. using(sqliteDataReaderWords=tablesGet.ExecuteReader())
  19. {
  20. try
  21. {
  22. for(inti=0;i<Words.FieldCount;i++)
  23. {
  24. WordsLst.Add(Words.GetName(i));
  25. }
  26. }
  27. catch(ExceptionE)
  28. {
  29. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  30. }
  31. }
  32. }
  33. }
  34. returnWordsLst;
  35. }


取值

  1. ///<summary>
  2. ///GetValuesFromsqlite
  3. ///</summary>
  4. ///<returns>listofValues</returns>
  5. publicstaticList<string>GetValues(stringDataSource,stringsql,stringGetColumu,stringPassWord=null)
  6. {
  7. List<string>ResultLst=newList<string>();
  8. using(sqliteConnectionconn=newsqliteConnection())
  9. {
  10. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  11. SCS.DataSource=DataSource;
  12. SCS.Password=PassWord;
  13. conn.ConnectionString=SCS.ToString();
  14. conn.Open();
  15. using(sqliteCommandcmd=newsqliteCommand(sql,conn))
  16. {
  17. using(sqliteDataReaderdr=cmd.ExecuteReader())
  18. {
  19. while(dr.Read())
  20. {
  21. try
  22. {
  23. ResultLst.Add(dr[GetColumu].ToString());
  24. }
  25. catch(ExceptionE)
  26. {
  27. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  28. }
  29. }
  30. }
  31. }
  32. }
  33. returnResultLst;
  34. }


插入数据

  1. ///<summary>
  2. ///InsertData
  3. ///</summary>
  4. ///<paramname="DataSource"></param>
  5. ///<paramname="TargetTable"></param>
  6. ///<returns></returns>
  7. publicstaticboolInsert(stringDataSource,stringColumnS,stringValueS,stringPassWord=null)
  8. {
  9. try
  10. {
  11. using(sqliteConnectionconn=newsqliteConnection())
  12. {
  13. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  14. SCS.DataSource=DataSource;
  15. SCS.Password=PassWord;
  16. conn.ConnectionString=SCS.ToString();
  17. conn.Open();
  18. //Insert
  19. DbCommandcmd=conn.CreateCommand();
  20. cmd.CommandText="insertinto["+TargetTable+"]("+ColumnS+")values("+ValueS+")";
  21. cmd.ExecuteNonQuery();
  22. returntrue;
  23. }
  24. }
  25. catch(ExceptionE)
  26. {
  27. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  28. returnfalse;
  29. }
  30. }


删除数据

  1. ///<summary>
  2. ///DeleteDate
  3. ///</summary>
  4. ///<paramname="DataSource"></param>
  5. ///<paramname="TargetTable"></param>
  6. ///<paramname="Word"></param>
  7. ///<paramname="Value"></param>
  8. ///<returns></returns>
  9. publicstaticboolDelete(stringDataSource,stringWord,stringValue,stringPassWord=null)
  10. {
  11. try
  12. {
  13. //Connect
  14. using(sqliteConnectionconn=newsqliteConnection())
  15. {
  16. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  17. SCS.DataSource=DataSource;
  18. SCS.Password=PassWord;
  19. conn.ConnectionString=SCS.ToString();
  20. conn.Open();
  21. DbCommandcmd=conn.CreateCommand();
  22. cmd.Connection=conn;
  23. //Delete
  24. cmd.CommandText="DeleteFrom"+TargetTable+"where["+Word+"]='"+Value+"'";
  25. cmd.ExecuteNonQuery();
  26. }
  27. returntrue;
  28. }
  29. catch(ExceptionE)
  30. {
  31. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  32. returnfalse;
  33. }
  34. }


执行sqlite指令

  1. ///<summary>
  2. ///sqliteCommand
  3. ///</summary>
  4. ///<paramname="DataSource"></param>
  5. ///<paramname="sql"></param>
  6. ///<returns></returns>
  7. publicstaticboolsqlCommand(stringDataSource,stringPassWord=null)
  8. {
  9. try
  10. {
  11. using(sqliteConnectionconn=newsqliteConnection())
  12. {
  13. sqliteConnectionStringBuilderSCS=newsqliteConnectionStringBuilder();
  14. SCS.DataSource=DataSource;
  15. SCS.Password=PassWord;
  16. conn.ConnectionString=SCS.ToString();
  17. conn.Open();
  18. using(sqliteCommandcmd_Re=newsqliteCommand(sql,conn))
  19. {
  20. cmd_Re.ExecuteNonQuery();
  21. }
  22. }
  23. returntrue;
  24. }
  25. catch(ExceptionE)
  26. {
  27. MessageBox.Show(E.Message,MessageBoxIcon.Information);
  28. returnfalse;
  29. }
  30. }
原文链接:https://www.f2er.com/sqlite/201239.html

猜你在找的Sqlite相关文章