赛亿提成统计系统

MySqlite.php 4.0KB

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