· 5 years ago · Feb 08, 2020, 09:50 PM
1import * as SQLite from 'expo-sqlite'
2
3const tableName = "qrs";
4const db = SQLite.openDatabase("qrs.db");
5
6// https://forums.expo.io/t/where-does-expo-store-its-sqlite-databases/1387/11
7
8export default class SQL {
9
10 static InitDatabase() {
11 db.transaction(tx => {
12 tx.executeSql(
13 `create table if not exists ${tableName} (id integer primary key not null, value text, date text);`
14 );
15 });
16 }
17
18 static AddQR = text => {
19 db.transaction(
20 tx => {
21 tx.executeSql(`insert into ${tableName} (value, date) values (?,?)`, [
22 text,
23 new Date().toString() // usar este para el log de registro
24 //new Date().toUTCString() // este es utc universal
25 //new Date().toLocaleDateString() // usar este para el conteo de dias.
26 ]);
27 },
28 null,
29 null
30 );
31 };
32
33 static DeleteQR = id => {
34 db.transaction(
35 tx => {
36 tx.executeSql(`delete from qrs where id = ?;`, [id])
37 },
38 null,
39 null
40 );
41 };
42
43 static GetQRS = () => {
44 return new Promise((resolve, reject) => {
45 db.transaction(async tx => {
46 await tx.executeSql(
47 `select * from ${tableName} order by id DESC`,
48 null,
49 (_, { rows: { _array } }) => {
50 resolve(_array);
51 }
52 );
53 });
54 });
55 };
56
57}