· 5 years ago · Feb 08, 2020, 09:02 PM
1import * as SQLite from 'expo-sqlite'
2
3const tableName = "qrs";
4const db = SQLite.openDatabase("es_control.db");
5
6console.log('@@@@@@@@@@@@@@@@@@ Opened the database!!!:: ');
7//console.log(db);
8
9// https://forums.expo.io/t/where-does-expo-store-its-sqlite-databases/1387/11
10
11export default class SQL {
12
13 static InitDatabase() {
14
15 db.transaction(tx => {
16
17 tx.executeSql(
18 `
19 CREATE TABLE IF NOT EXISTS tb_abastecimiento (
20 id_abastecimiento int(11) NOT NULL AUTO_INCREMENT,
21 id_es int(11) DEFAULT NULL,
22 placa varchar(255) DEFAULT NULL,
23 tipo_combustible enum('gasolina','gasoil') NOT NULL DEFAULT 'gasolina',
24 cant_litros int(50) DEFAULT NULL,
25 fecha_surtido datetime DEFAULT NULL,
26 fecha_prox_surtido datetime DEFAULT NULL,
27 estatus enum('activo','inactivo') DEFAULT NULL,
28 PRIMARY KEY (id_abastecimiento),
29 KEY id_es (id_es),
30 KEY placa (placa)
31 );
32
33 CREATE TABLE IF NOT EXISTS tb_automotor (
34 id_automotor int(11) NOT NULL AUTO_INCREMENT,
35 id_auth int(11) DEFAULT NULL,
36 cedula varchar(20) DEFAULT NULL,
37 placa varchar(255) DEFAULT NULL,
38 tipo_automotor enum('vehiculo','moto') DEFAULT NULL,
39 tipo_uso enum('turista','transportista','particular','funcionario') DEFAULT NULL,
40 color varchar(255) DEFAULT NULL,
41 marca varchar(255) DEFAULT NULL,
42 cilindros int(2) DEFAULT NULL,
43 estatus enum('activo','inactivo') DEFAULT NULL,
44 fecha_registro datetime DEFAULT NULL,
45 PRIMARY KEY (id_automotor),
46 UNIQUE KEY placa (placa),
47 KEY id_auth (id_auth)
48 );
49
50 `
51 );
52 });
53 }
54
55 static Guardar = (placa) => {
56
57 console.log(placa);
58
59 // aca tengo q verificar si una placa existe...
60 // y luego guardar en la db
61
62 // necesito ejemplos para ver como funciona esta cosa
63
64 };
65
66
67 static AddQR = text => {
68 db.transaction(
69 tx => {
70 tx.executeSql(`insert into ${tableName} (value, date) values (?,?)`, [
71 text,
72 new Date().toString() // usar este para el log de registro
73 //new Date().toUTCString() // este es utc universal
74 //new Date().toLocaleDateString() // usar este para el conteo de dias.
75 ]);
76 },
77 null,
78 null
79 );
80 };
81
82 static DeleteQR = id => {
83 db.transaction(
84 tx => {
85 tx.executeSql(`delete from qrs where id = ?;`, [id])
86 },
87 null,
88 null
89 );
90 };
91
92 static GetQRS = () => {
93 return new Promise((resolve, reject) => {
94 db.transaction(async tx => {
95 await tx.executeSql(
96 `select * from ${tableName} order by id DESC`,
97 null,
98 (_, { rows: { _array } }) => {
99 resolve(_array);
100 }
101 );
102 });
103 });
104 };
105
106}