f.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. function openFSqlite(){
  2. //创建数据库或者打开
  3. return new Promise((resolve,reject) =>{
  4. plus.sqlite.openDatabase({
  5. name:'f', //数据库名称
  6. path:'_doc/f.db',
  7. //数据库地址,uniapp推荐以下划线为开头,这到底存在哪里去了,我也不清楚,哈哈
  8. success(e){
  9. resolve(e); //成功回调
  10. },
  11. fail(e){
  12. reject(e); //失败回调
  13. }
  14. })
  15. })
  16. }
  17. //在该数据库里创建表格, 这一步也必须要!
  18. function createFSQL(userId){
  19. return new Promise((resolve,reject) =>{
  20. //创建表格在executeSql方法里写
  21. plus.sqlite.executeSql({
  22. name:'f',
  23. //表格创建或者打开,后面为表格结构
  24. sql:'create table if not exists s_friend_'+userId+' ("id" INT(11) PRIMARY KEY NOT NULL,"nickName" CHAR(255),"name" CHAR(255),"avatar" CHAR(255))',
  25. success(e){
  26. resolve(e);
  27. },
  28. fail(e){
  29. reject(e);
  30. }
  31. })
  32. })
  33. }
  34. //向表格里添加数据
  35. function addFSQL(obj,userId){
  36. //判断有没有传参
  37. if(obj !== undefined){
  38. //判断传的参是否有值
  39. var b = (JSON.stringify(obj) == "{}");
  40. if(!b){
  41. //obj传来的参数对象
  42. var id = obj.id || null; //id
  43. var nickName = obj.nickName || null; //昵称
  44. var name = obj.name || null; //字母
  45. var avatar = obj.avatar || null; //头像
  46. return new Promise((resolve,reject) =>{
  47. plus.sqlite.executeSql({
  48. name:'f',
  49. sql:"INSERT INTO s_friend_" + userId + " (`id`, `nickName`, `name`, `avatar`) VALUES ('" + id + "', '" + nickName + "', '" + name + "', '" + avatar + "')",
  50. success(e){
  51. resolve(e);
  52. },
  53. fail(e){
  54. reject(e);
  55. }
  56. })
  57. })
  58. }else{
  59. return new Promise((resolve,reject) =>{reject("错误添加")})
  60. }
  61. }else{
  62. return new Promise((resolve,reject) =>{reject("错误添加")})
  63. }
  64. }
  65. //查询获取数据库里的数据
  66. function selectFSQL(userId){
  67. if(userId !== undefined){
  68. //第一个是表单名称,后两个参数是列表名,用来检索
  69. var sql = 'select * from s_friend_'+userId;
  70. return new Promise((resolve,reject)=>{
  71. plus.sqlite.selectSql({
  72. name:'f',
  73. sql:sql,
  74. success(e){
  75. resolve(e);
  76. },
  77. fail(e){
  78. reject(e);
  79. }
  80. })
  81. })
  82. }else{
  83. return new Promise((resolve,reject) =>{reject("错误查询")});
  84. }
  85. }
  86. //删除数据库里的数据
  87. function deleteFSQL(name,sol,qq,ww,ee){
  88. if(name !== undefined && sol !== undefined){
  89. //listId为表名,后面两个是列表名,检索用的
  90. if(ww !== undefined){
  91. //两个检索条件
  92. var sql = 'delete from '+name+' where '+sol+'="'+qq+'" and '+ww+'='+ee+'';
  93. }else{
  94. //一个检索条件
  95. var sql = 'delete from '+name+' where '+sol+'="'+qq+'"';
  96. }
  97. return new Promise((resolve,reject) =>{
  98. plus.sqlite.executeSql({
  99. name:'f',
  100. sql:sql,
  101. success(e){
  102. resolve(e);
  103. },
  104. fail(e){
  105. reject(e);
  106. }
  107. })
  108. })
  109. }else{
  110. return new Promise((resolve,reject) =>{reject("错误删除")});
  111. }
  112. }
  113. //修改数据表里的数据
  114. function modifyFSQL(listName,name,cont,use,sel){
  115. //表格名,要修改地方的列名,修改后的内容,修改条件查询,列名,内容
  116. var sql;
  117. if(use == undefined){
  118. sql ='update '+listName+' set '+name+'="'+cont+'"';
  119. }else{
  120. sql ='update '+listName+' set '+name+'="'+cont+'" where '+use+'="'+sel+'"';
  121. }
  122. //where前面的是要修改的,后面的是条件,选择哪个
  123. return new Promise((resolve,reject) =>{
  124. plus.sqlite.executeSql({
  125. name:'f',
  126. sql:sql,
  127. success(e){
  128. resolve(e);
  129. },
  130. fail(e){
  131. reject(e);
  132. }
  133. })
  134. })
  135. }
  136. //关闭数据库
  137. function closeSQL(name){
  138. return new Promise((resolve,reject) =>{
  139. plus.sqlite.closeDatabase({
  140. name:'f',
  141. success(e){
  142. resolve(e);
  143. },
  144. fail(e){
  145. reject(e);
  146. }
  147. })
  148. })
  149. }
  150. //监听数据库是否开启
  151. function isOpen(name,path){
  152. var ss = name || 'f';
  153. var qq = path || '_doc/f.db';
  154. //数据库打开了就返回true,否则返回false
  155. var open = plus.sqlite.isOpenDatabase({
  156. name:ss,
  157. path:qq
  158. })
  159. return open;
  160. }
  161. //一次获取指定数据条数
  162. function pullSQL(id,num){
  163. //id为表名,num为跳过多少条数据
  164. //根据list来倒序拿数据,跳过num条拿取15条
  165. return new Promise((resolve,reject) =>{
  166. plus.sqlite.selectSql({
  167. name:'f',
  168. sql:'select * from '+id+' order by list desc limit 15 offset '+num+'',
  169. success(e){
  170. resolve(e);
  171. },
  172. fail(e){
  173. reject(e);
  174. }
  175. })
  176. })
  177. }
  178. //把这些方法导出去
  179. export{
  180. openFSqlite,
  181. createFSQL,
  182. addFSQL,
  183. selectFSQL
  184. }