YSSqflite.dart 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. import 'dart:convert';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter_wallet/tools/YSTools.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import 'YSNetWork.dart';
  7. String sqName = 'walletDb';
  8. class YSSqflite{
  9. Database? db;
  10. String? tabName = YSData().exName.replaceAll('-', '').replaceAll(' ', '');
  11. YSSqflite init({String? name}) {
  12. final file = YSSqflite();
  13. open(name);
  14. return file;
  15. }
  16. //判断表是否存在
  17. isTableExits(String tableName) async {
  18. db ??= await openDatabase('$sqName.db');
  19. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tableName'";
  20. var res = await db!.rawQuery(sql);
  21. var returnRes = res.isNotEmpty;
  22. return returnRes;
  23. }
  24. open(String? name) async {
  25. if (await isTableExits(name??tabName!)==false) {
  26. execute(name);
  27. }
  28. }
  29. ///创建表
  30. execute(String? name) async{
  31. db ??= await openDatabase('$sqName.db');
  32. await db!.execute(
  33. 'CREATE TABLE ${name??tabName} (id INTEGER PRIMARY KEY, name TEXT, password TEXT, word TEXT, private TEXT, public TEXT,chooseType INTEGER,sort INTEGER)'
  34. );
  35. }
  36. ///插入数据
  37. Future<int> rawInsert(YSWalletDbData data,{String? name}) async{
  38. db ??= await openDatabase('$sqName.db');
  39. List list = await rawQuery();
  40. bool isHas = list.any((element) => element['public']==data.public);
  41. if(isHas){
  42. ysFlutterToast('该钱包已存在');
  43. return 0;
  44. }else{
  45. int id = await db!.rawInsert(
  46. 'INSERT INTO ${name??tabName}(name, password, word, private, public, chooseType, sort) VALUES("${data.name}","${data.password}","${data.word}",'
  47. '"${data.private}","${data.public}","${data.chooseType}","${list.length}")'
  48. );
  49. return id;
  50. }
  51. }
  52. ///查询数据
  53. Future<List> rawQuery({String? name}) async{
  54. try{
  55. db ??= await openDatabase('$sqName.db');
  56. List<Map> list = await db!.rawQuery('SELECT * FROM ${name??tabName}');
  57. List array = jsonDecode(jsonEncode(list));
  58. array.sort((a, b) => a['sort'].compareTo(b['sort']));
  59. return array;
  60. }catch(e){
  61. return [];
  62. }
  63. }
  64. ///更新数据
  65. Future<int> rawUpdate(YSWalletDbData data,int sort,String id,{String? name}) async{
  66. db ??= await openDatabase('$sqName.db');
  67. int count = await db!.rawUpdate(
  68. 'UPDATE ${name??tabName} SET name = ?, password = ? , word = ?, private = ?, public = ? ,chooseType = ? ,sort = ? WHERE id = $id',
  69. [data.name, data.password, data.word,data.private,data.public,data.chooseType,sort]
  70. );
  71. return count;
  72. }
  73. ///删除数据
  74. Future<int> rawDelete(String id) async{
  75. db ??= await openDatabase('$sqName.db');
  76. int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
  77. return count;
  78. }
  79. Future<int> delete() async{
  80. db ??= await openDatabase('$sqName.db');
  81. final status = await db?.delete(tabName!)??0;
  82. return status;
  83. }
  84. static notifier(BuildContext context,List array) {
  85. YSData().isLoad = true;
  86. Provider.of<YSWalletNotifier>(context, listen: false) .setValue(array);
  87. }
  88. }
  89. class YSSqflite2{
  90. Database? db;
  91. YSSqflite2 init() {
  92. final file = YSSqflite2();
  93. open();
  94. YSData().tokenTable = '${YSData().typeId}${YSData().wallet['id']}'.replaceAll('-', '').replaceAll(' ', '');
  95. return file;
  96. }
  97. //判断表是否存在
  98. isTableExits(String tableName) async {
  99. db ??= await openDatabase('$sqName.db');
  100. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '${YSData().tokenTable}'";
  101. var res = await db!.rawQuery(sql);
  102. var returnRes = res.isNotEmpty;
  103. return returnRes;
  104. }
  105. open() async {
  106. if (await isTableExits(YSData().tokenTable)==false) {
  107. execute();
  108. }
  109. }
  110. ///创建表
  111. execute() async{
  112. db ??= await openDatabase('$sqName.db');
  113. await db!.execute(
  114. 'CREATE TABLE ${YSData().tokenTable} (id INTEGER PRIMARY KEY, hid TEXT, name TEXT, gas TEXT, decimal TEXT, rice_usd TEXT, percent_change_24h TEXT, '
  115. 'address TEXT,total_supply TEXT,icon_url TEXT,symbol TEXT)'
  116. );
  117. }
  118. //
  119. ///插入数据
  120. Future<int> rawInsert({
  121. required String hid,
  122. required String name,
  123. required String gas,
  124. required String decimal,
  125. required String rice_usd,
  126. required String percent_change_24h,
  127. required String address,
  128. required String total_supply,
  129. required String icon_url,
  130. required String symbol
  131. }) async{
  132. db ??= await openDatabase('$sqName.db');
  133. int id = await db!.rawInsert(
  134. 'INSERT INTO ${YSData().tokenTable} (hid,name,gas,decimal,rice_usd,percent_change_24h,address,total_supply,icon_url,symbol) '
  135. 'VALUES("$hid","$name","$gas","$decimal","$rice_usd","$percent_change_24h","$address","$total_supply","$icon_url","$symbol")'
  136. );
  137. return id;
  138. }
  139. ///查询数据
  140. Future<List> rawQuery() async{
  141. try{
  142. db ??= await openDatabase('$sqName.db');
  143. List<Map> list = await db!.rawQuery('SELECT * FROM ${YSData().tokenTable}');
  144. List array = jsonDecode(jsonEncode(list));
  145. YSData().tokenArray = array;
  146. return array;
  147. }catch(e){
  148. LogUtil.d(e);
  149. return [];
  150. }
  151. }
  152. ///更新数据
  153. Future<int> rawUpdate(String value,String id) async{
  154. db ??= await openDatabase('$sqName.db');
  155. int count = await db!.rawUpdate(
  156. 'UPDATE ${YSData().tokenTable} SET hid = ?,name = ?,gas = ?,decimal = ?,rice_usd = ?,percent_change_24h = ?,total_supply = ? ,icon_url = ? ,symbol = ? WHERE id = $id',
  157. [value]
  158. );
  159. return count;
  160. }
  161. ///删除数据
  162. Future<int> rawDelete(String hid) async{
  163. db ??= await openDatabase('$sqName.db');
  164. int count = await db!.rawDelete('DELETE FROM ${YSData().tokenTable} WHERE hid = ?', [hid]);
  165. return count;
  166. }
  167. Future<int> delete() async{
  168. db ??= await openDatabase('$sqName.db');
  169. final status = await db?.delete(YSData().tokenTable)??0;
  170. return status;
  171. }
  172. static notifier(BuildContext context,List array) {
  173. Provider.of<YSTokenNotifier>(context, listen: false) .setValue(array);
  174. }
  175. }
  176. //{
  177. // hid: 853860756,
  178. // blockchain_id: 1,
  179. // blockchain: null,
  180. // name: HarryPotterObamaSonic10Inu,
  181. // symbol: BITCOIN,
  182. // bl_symbol: BITCOIN,
  183. // gas: 60000,
  184. // decimal: 8,
  185. // precision: 0,
  186. // balance: , p
  187. // rice_usd: 0.0964896898818645,
  188. // percent_change_24h: 106.40984418,
  189. // asset: 0,
  190. // added: 0,
  191. // address: 0x72e4f9f808c49a2a61de9c5896298920dc4eeea9,
  192. // owner_address: ,
  193. // website: ,
  194. // description:
  195. // HarryPotterObamaSonic10Inu,
  196. // update_contract: 0,
  197. // total_supply: 1000000000,
  198. // holder_count: 0,
  199. // published: 0,
  200. // ti_link: ,
  201. // token_status: 0,
  202. // validated: 0,
  203. // icon_url: https://hk.tpstatic.net/token/tokenpocket-1689745640961.jpeg,
  204. // auto_add: 0,
  205. // dapp_code: ,
  206. // token_protocol: 0,
  207. // metadata_type: 0,
  208. // create_time: 2023-05-10T14:33:16+08:00,
  209. // Weight: 1
  210. // }
  211. class YSWalletDbData {
  212. final String name;
  213. final String password;
  214. final String word;
  215. final String private;
  216. final String public;
  217. final int chooseType;
  218. YSWalletDbData(this.name, this.password, this.word, this.private, this.public, this.chooseType);
  219. }
  220. class YSWalletNotifier with ChangeNotifier {
  221. List walletArray = [];
  222. List get value => walletArray;
  223. void setValue(value)
  224. {
  225. walletArray = value;
  226. LogUtil.d('walletArray====$walletArray');
  227. notifyListeners();
  228. }
  229. }
  230. class YSTokenNotifier with ChangeNotifier {
  231. List tokenArray = [];
  232. List get value => tokenArray;
  233. void setValue(value)
  234. {
  235. tokenArray = value;
  236. LogUtil.d('tokenArray====$tokenArray');
  237. notifyListeners();
  238. }
  239. }
  240. class YSSqflite3{
  241. Database? db;
  242. String? tabName = 'netTable';
  243. YSSqflite3 init() {
  244. final file = YSSqflite3();
  245. open();
  246. return file;
  247. }
  248. //判断表是否存在
  249. isTableExits() async {
  250. db ??= await openDatabase('$sqName.db');
  251. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
  252. var res = await db!.rawQuery(sql);
  253. var returnRes = res.isNotEmpty;
  254. return returnRes;
  255. }
  256. open() async {
  257. if (await isTableExits()==false) {
  258. execute();
  259. }
  260. }
  261. ///创建表
  262. execute() async{
  263. db ??= await openDatabase('$sqName.db');
  264. await db!.execute(
  265. 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, key TEXT, value TEXT)'
  266. );
  267. }
  268. ///插入数据
  269. Future<int> rawInsert(String key,Object data) async{
  270. db ??= await openDatabase('$sqName.db');
  271. String dataStr = jsonEncode(data);
  272. dataStr = dataStr.replaceAll('"', 'ys');
  273. rawDelete(key);
  274. int id = await db!.rawInsert(
  275. 'INSERT INTO $tabName(key,value) VALUES("${key.replaceAll('/', '')}","$dataStr")'
  276. );
  277. return id;
  278. }
  279. ///查询数据
  280. Future<Object> rawQuery(String key) async{
  281. try{
  282. db ??= await openDatabase('$sqName.db');
  283. List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
  284. List array = jsonDecode(jsonEncode(list));
  285. List valueList = array.where((element) => element['key']==key.replaceAll('/', '')).toList();
  286. if(valueList.isNotEmpty){
  287. Map value = valueList.last;
  288. String data = value['value'];
  289. data = data.replaceAll('ys', '"');
  290. return jsonDecode(data);
  291. }
  292. return {};
  293. }catch(e){
  294. return {};
  295. }
  296. }
  297. ///更新数据
  298. Future<int> rawUpdate(String key,Object data) async{
  299. db ??= await openDatabase('$sqName.db');
  300. String dataStr = jsonEncode(data);
  301. dataStr = dataStr.replaceAll('"', 'ys');
  302. int count = await db!.rawUpdate(
  303. 'UPDATE $tabName SET value = ? WHERE key = ${key.replaceAll('/', '')}',
  304. [dataStr]
  305. );
  306. return count;
  307. }
  308. ///删除数据
  309. Future<int> rawDelete(String key) async{
  310. db ??= await openDatabase('$sqName.db');
  311. int count = await db!.rawDelete('DELETE FROM $tabName WHERE key = ?', [key]);
  312. return count;
  313. }
  314. Future<int> delete() async{
  315. db ??= await openDatabase('$sqName.db');
  316. final status = await db?.delete(tabName!)??0;
  317. return status;
  318. }
  319. }
  320. class YSAlwayAddressTable{
  321. Database? db;
  322. String? tabName = 'YSAlwayAddressTable';
  323. YSAlwayAddressTable init() {
  324. final file = YSAlwayAddressTable();
  325. open();
  326. return file;
  327. }
  328. //判断表是否存在
  329. isTableExits() async {
  330. db ??= await openDatabase('$sqName.db');
  331. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
  332. var res = await db!.rawQuery(sql);
  333. var returnRes = res.isNotEmpty;
  334. return returnRes;
  335. }
  336. open() async {
  337. if (await isTableExits()==false) {
  338. execute();
  339. }
  340. }
  341. ///创建表
  342. execute() async{
  343. db ??= await openDatabase('$sqName.db');
  344. await db!.execute(
  345. 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, address TEXT, name TEXT, remark TEXT, icon TEXT, netId TEXT)'
  346. );
  347. }
  348. ///插入数据
  349. Future<int> rawInsert(String address,String name,String remark,String icon,String netId) async{
  350. db ??= await openDatabase('$sqName.db');
  351. int id = await db!.rawInsert(
  352. 'INSERT INTO $tabName(address,name,remark,icon,netId) VALUES("$address","$name","$remark","$icon","$netId")'
  353. );
  354. return id;
  355. }
  356. ///查询数据
  357. Future<List> rawQuery() async{
  358. try{
  359. db ??= await openDatabase('$sqName.db');
  360. List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
  361. List array = jsonDecode(jsonEncode(list));
  362. return array;
  363. }catch(e){
  364. LogUtil.d(e);
  365. return [];
  366. }
  367. }
  368. ///更新数据
  369. Future<int> rawUpdate(String id,String address,String name,String remark,String icon,String netId) async{
  370. db ??= await openDatabase('$sqName.db');
  371. int count = await db!.rawUpdate(
  372. 'UPDATE ${YSData().tokenTable} SET address = ?,name = ?,remark = ? ,icon = ?,netId = ? WHERE id = $id',
  373. [address,name,remark,icon,netId]
  374. );
  375. return count;
  376. }
  377. ///删除数据
  378. Future<int> rawDelete(String id) async{
  379. db ??= await openDatabase('$sqName.db');
  380. int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
  381. return count;
  382. }
  383. Future<int> delete() async{
  384. db ??= await openDatabase('$sqName.db');
  385. final status = await db?.delete(tabName!)??0;
  386. return status;
  387. }
  388. }
  389. class YSTansTable{
  390. Database? db;
  391. String? tabName = '${YSData().contName.replaceAll(' ', '').replaceAll('-', '')}Tans';
  392. YSTansTable init() {
  393. final file = YSTansTable();
  394. open();
  395. return file;
  396. }
  397. //判断表是否存在
  398. isTableExits() async {
  399. db ??= await openDatabase('$sqName.db');
  400. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
  401. var res = await db!.rawQuery(sql);
  402. var returnRes = res.isNotEmpty;
  403. return returnRes;
  404. }
  405. open() async {
  406. if (await isTableExits()==false) {
  407. execute();
  408. }
  409. }
  410. ///创建表
  411. execute() async{
  412. db ??= await openDatabase('$sqName.db');
  413. await db!.execute(
  414. 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, from_addr TEXT, to_addr TEXT, hash TEXT, status TEXT, type TEXT,nonce TEXT,amount TEXT)'
  415. );
  416. }
  417. ///插入数据
  418. Future<int> rawInsert({required String from,required String to,required String hash,required String status,required String type,required String nonce,required String amount}) async{
  419. db ??= await openDatabase('$sqName.db');
  420. int id = await db!.rawInsert(
  421. 'INSERT INTO $tabName(from_addr,to_addr,hash,status,type,nonce,amount) VALUES("$from","$to","$hash","$status","$type","$nonce","$amount")'
  422. );
  423. return id;
  424. }
  425. ///查询数据
  426. Future<List> rawQuery() async{
  427. try{
  428. db ??= await openDatabase('$sqName.db');
  429. List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
  430. List array = jsonDecode(jsonEncode(list));
  431. return array;
  432. }catch(e){
  433. LogUtil.d(e);
  434. return [];
  435. }
  436. }
  437. ///更新数据
  438. Future<int> rawUpdate({required String id,required String from,required String to,required String hash,required String status,required String type,required String nonce,required String amount}) async{
  439. db ??= await openDatabase('$sqName.db');
  440. int count = await db!.rawUpdate(
  441. 'UPDATE ${YSData().tokenTable} SET from_addr = ?,to_addr = ?,hash = ? ,status = ?,type = ? ,nonce = ? ,amount = ? WHERE id = $id',
  442. [from,to,hash,status,type,nonce,amount]
  443. );
  444. return count;
  445. }
  446. ///删除数据
  447. Future<int> rawDelete(String id) async{
  448. db ??= await openDatabase('$sqName.db');
  449. int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
  450. return count;
  451. }
  452. Future<int> delete() async{
  453. db ??= await openDatabase('$sqName.db');
  454. final status = await db?.delete(tabName!)??0;
  455. return status;
  456. }
  457. }
  458. class YSTronTable{
  459. Database? db;
  460. String? tabName = 'tronExchange';
  461. YSTronTable init() {
  462. final file = YSTronTable();
  463. open();
  464. return file;
  465. }
  466. //判断表是否存在
  467. isTableExits() async {
  468. db ??= await openDatabase('$sqName.db');
  469. var sql ="SELECT * FROM sqlite_master WHERE TYPE = 'table' AND NAME = '$tabName'";
  470. var res = await db!.rawQuery(sql);
  471. var returnRes = res.isNotEmpty;
  472. return returnRes;
  473. }
  474. open() async {
  475. if (await isTableExits()==false) {
  476. execute();
  477. }
  478. }
  479. ///创建表
  480. execute() async{
  481. db ??= await openDatabase('$sqName.db');
  482. await db!.execute(
  483. 'CREATE TABLE $tabName (id INTEGER PRIMARY KEY, from_addr TEXT, to_addr TEXT, from_token_addr TEXT, to_token_addr TEXT,amount TEXT, hash TEXT)'
  484. );
  485. }
  486. ///插入数据
  487. Future<int> rawInsert({required String from_addr,required String to_addr,required String from_token_addr,required String to_token_addr,required String amount,required String hash}) async{
  488. db ??= await openDatabase('$sqName.db');
  489. int id = await db!.rawInsert(
  490. 'INSERT INTO $tabName(from_addr,to_addr,from_token_addr,to_token_addr,amount,hash) VALUES("$from_addr","$to_addr","$from_token_addr","$to_token_addr","$amount","$hash")'
  491. );
  492. return id;
  493. }
  494. ///查询数据
  495. Future<List> rawQuery() async{
  496. try{
  497. db ??= await openDatabase('$sqName.db');
  498. List<Map> list = await db!.rawQuery('SELECT * FROM $tabName');
  499. List array = jsonDecode(jsonEncode(list));
  500. return array;
  501. }catch(e){
  502. LogUtil.d(e);
  503. return [];
  504. }
  505. }
  506. ///更新数据
  507. Future<int> rawUpdate({required String id,required String from,required String to,required String hash,required String status,required String type,required String nonce,required String amount}) async{
  508. db ??= await openDatabase('$sqName.db');
  509. int count = await db!.rawUpdate(
  510. 'UPDATE ${YSData().tokenTable} SET from_addr = ?,to_addr = ?,from_token_addr = ? ,to_token_addr = ?,amount = ? ,hash = ? WHERE id = $id',
  511. [from,to,hash,status,type,nonce,amount]
  512. );
  513. return count;
  514. }
  515. ///删除数据
  516. Future<int> rawDelete(String id) async{
  517. db ??= await openDatabase('$sqName.db');
  518. int count = await db!.rawDelete('DELETE FROM $tabName WHERE id = ?', [id]);
  519. return count;
  520. }
  521. Future<int> delete() async{
  522. db ??= await openDatabase('$sqName.db');
  523. final status = await db?.delete(tabName!)??0;
  524. return status;
  525. }
  526. }