Advertisement
Guest User

cfghjkl;

a guest
Jun 8th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Android.App;
  7. using Android.Content;
  8. using Android.OS;
  9. using Android.Runtime;
  10. using Android.Views;
  11. using Android.Widget;
  12. using SQLite;
  13. using P4AndroidApp.Model;
  14. using Android.Util;
  15.  
  16.  
  17.  
  18. namespace P4AndroidApp.Resources.Datahelp
  19. {
  20. public class DataBase
  21. {
  22. public static string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  23. #pragma warning disable IDE1006 // Naming Styles
  24. public static bool createDataBase()
  25. {
  26. try
  27. {
  28.  
  29. using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3")))
  30. {
  31. connection.CreateTable<Person>();
  32. return true;
  33. }
  34. }
  35. catch (SQLiteException ex)
  36. {
  37. Log.Info("SQLiteEx", ex.Message);
  38. return false;
  39. }
  40. }
  41.  
  42. public static bool InsertIntoTablePerson(Person person)
  43. {
  44. try
  45. {
  46. using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3")))
  47. {
  48. connection.Insert(person);
  49. return true;
  50. }
  51. }
  52. catch (SQLiteException ex)
  53. {
  54. Log.Info("SQLiteEx", ex.Message);
  55. return false;
  56. }
  57. }
  58.  
  59. public static List<Person> selectTablePerson()
  60. {
  61. try
  62. {
  63. using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3")))
  64. {
  65. return connection.Table<Person>().ToList();
  66.  
  67. }
  68. }
  69. catch (SQLiteException ex)
  70. {
  71. Log.Info("SQLiteEx", ex.Message);
  72. return null;
  73. }
  74. }
  75.  
  76. public static bool updateTablePerson(Person person)
  77. {
  78. try
  79. {
  80. using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3")))
  81. {
  82. connection.Query<Person>("UPDATE Person set Username=?,Password=?,Firstname=?,Lastname=?,Weight=? Where Id=?", person.Username, person.Password, person.Firstname, person.Lastname, person.Weight, person.Id);
  83. return true;
  84. }
  85. }
  86. catch (SQLiteException ex)
  87. {
  88. Log.Info("SQLiteEx", ex.Message);
  89. return false;
  90. }
  91. }
  92. public static bool selectQueryTablePerson(int Id)
  93. {
  94. try
  95. {
  96. using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3")))
  97. {
  98. connection.Query<Person>("Select * FROM Person Where Id=?", Id);
  99. return true;
  100. }
  101. }
  102. catch (SQLiteException ex)
  103. {
  104. Log.Info("SQLiteEx", ex.Message);
  105. return false;
  106. }
  107. }
  108. public static Boolean CheckIsDataAlreadyInDBorNot(string TableName,
  109. string dbfield, string fieldValue)
  110. {
  111. var check = new SQLiteConnection(System.IO.Path.Combine(folder, "Persons.db3"));
  112. check.Query<Person>("Select * FROM Person Where Username=?", fieldValue);
  113. //SQLiteDatabase sqldb = EGLifeStyleApplication.sqLiteDatabase;
  114. string Query = "Select * from " + TableName + " where " + dbfield + " = " + fieldValue;
  115. Person cursor = check.Query(Query, null);
  116. if (cursor.getCount() <= 0)
  117. {
  118. cursor.close();
  119. return false;
  120. }
  121. cursor.close();
  122. return true;
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement