markdown格式wiki文档

MySqlite.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\model;
  3. use \Exception;
  4. class MySqlite
  5. {
  6. private $mydb;
  7. protected $tablename;
  8. public function __construct()
  9. {
  10. $mydb = new \SQLite3(DB . DIRECTORY_SEPARATOR . 'mysqlitedb.sqlite');
  11. if (!$mydb) {
  12. throw new \Exception("$mydb->lastErrorMsg()", 1);
  13. } else {
  14. // echo "Opened database successfully\n";
  15. $this->mydb = $mydb;
  16. }
  17. }
  18. /**
  19. * 执行sql
  20. * @param string $sql
  21. * @return mixd $res
  22. */
  23. public function exec($sql)
  24. {
  25. @$res = $this->mydb->exec($sql);
  26. return $res;
  27. }
  28. public function query($sql)
  29. {
  30. $result = $this->mydb->query($sql);
  31. return $result;
  32. }
  33. public function lastInsertRowID ()
  34. {
  35. $result = $this->mydb->lastInsertRowID();
  36. return $result;
  37. }
  38. public function lastErrorMsg()
  39. {
  40. return $this->mydb->lastErrorMsg();
  41. }
  42. /**
  43. * 查询数组列表
  44. */
  45. public function select($sql)
  46. {
  47. $result = $this->mydb->query($sql);
  48. $data = array();
  49. // var_dump($result);exit;
  50. while ($arr = $result->fetchArray(SQLITE3_ASSOC)) {
  51. $data[] = $arr;
  52. }
  53. return $data;
  54. }
  55. /**
  56. * 查询一条
  57. */
  58. public function getOneById($id, $tablename='')
  59. {
  60. $tablename = $tablename ? $tablename : $this->tablename;
  61. $sql = "SELECT * FROM `$tablename` WHERE `id`=$id;";
  62. $result = $this->mydb->query($sql);
  63. // var_dump($result);
  64. $data = $result->fetchArray(SQLITE3_ASSOC);
  65. // var_dump($data);
  66. return $data;
  67. }
  68. public function getOne($sql = "")
  69. {
  70. $result = $this->mydb->query($sql);
  71. // var_dump($result);
  72. $data = $result->fetchArray(SQLITE3_ASSOC);
  73. // var_dump($data);
  74. return $data;
  75. }
  76. /**
  77. * 单列合计
  78. */
  79. public function sumColumn($column, $tablename)
  80. {
  81. $tablename = $tablename ? $tablename : $this->tablename;
  82. $sql = "SELECT sum(`$column`) as sumData FROM `$tablename`;";
  83. $result = $this->mydb->query($sql);
  84. // $data = $result->fetchArray();
  85. // var_dump($data['sumData']);exit;
  86. if ($data = $result->fetchArray(SQLITE3_ASSOC)) {
  87. return $data['sumData'];
  88. }
  89. return 0;
  90. }
  91. /**
  92. * 列表结果集
  93. */
  94. public function dataList($where='', $order= '', $desc = false, $limit = 0)
  95. {
  96. if ($order) {
  97. $where .= " order by $order";
  98. if ($desc) {
  99. $where .= " desc";
  100. } else {
  101. $where .= " asc";
  102. }
  103. }
  104. if ($limit) {
  105. $where .= " limit " . $limit;
  106. }
  107. $sql = "select * from $this->tablename $where;";
  108. return $this->select($sql);
  109. }
  110. /**
  111. * 分页结果
  112. */
  113. public function pageList($where, $page = 1, $limit = 10)
  114. {
  115. $res = $this->query("select count(*) as total from $this->tablename $where;");
  116. $data = $res->fetchArray(SQLITE3_ASSOC);
  117. $offset = ($page - 1) * $limit;
  118. $sql = "select * from $this->tablename $where limit $offset, $limit;";
  119. $list = $this->select($sql);
  120. $data['list'] = $list;
  121. $data['page'] = $page;
  122. $data['limit'] = $limit;
  123. return $data;
  124. }
  125. public function listByName($name='', $order = '', $desc = false)
  126. {
  127. $where = "";
  128. if ($name) {
  129. $where = " where name like '%$name%'";
  130. }
  131. if ($order) {
  132. $where .= " order by $order";
  133. if ($desc) {
  134. $where .= " desc";
  135. } else {
  136. $where .= " asc";
  137. }
  138. }
  139. $sql = "select * from $this->tablename $where;";
  140. $res = $this->select($sql);
  141. return $res;
  142. }
  143. /**
  144. * save
  145. */
  146. public function save($data)
  147. {
  148. $columns = "";
  149. $values = "";
  150. foreach ($data as $key => $value) {
  151. $columns .= "`" . $key . "`,";
  152. $values .= "'" . $value . "',";
  153. }
  154. $columns = rtrim($columns, ',');
  155. $values = rtrim($values, ',');
  156. $sql = "INSERT INTO `$this->tablename`(" . $columns . ") VALUES(". $values . ");";
  157. if ($this->exec($sql)) {
  158. return true;
  159. } else {
  160. throw new Exception("Error SQL Request", 1);
  161. }
  162. }
  163. /**
  164. * updateById
  165. */
  166. public function updateById($data)
  167. {
  168. $id = $data['id'];
  169. unset($data['id']);
  170. $columns = "";
  171. foreach ($data as $key => $value) {
  172. $columns .= "`" . $key . "`='" . $value ."',";
  173. }
  174. $columns = rtrim($columns, ',');
  175. $sql = "UPDATE `$this->tablename` SET $columns WHERE `id`=$id";
  176. if ($this->exec($sql)) {
  177. return true;
  178. } else {
  179. throw new Exception("Error SQL Request", 1);
  180. }
  181. }
  182. /**
  183. * deleteByIds
  184. */
  185. public function deleteById($id)
  186. {
  187. if (is_array($id)) {
  188. $sql = "DELETE FROM `$this->tablename` WHERE `id` IN(";
  189. for ($i=0; $i < count($id); $i++) {
  190. $sql .= $id[$i] . ',';
  191. }
  192. $sql = rtrim($sql, ',');
  193. $sql .= ");";
  194. } else {
  195. $sql = "DELETE FROM `$this->tablename` WHERE `id`=$id;";
  196. }
  197. return $this->exec($sql);
  198. }
  199. public function __destruct()
  200. {
  201. $this->mydb->close();
  202. }
  203. }