-
Notifications
You must be signed in to change notification settings - Fork 1
/
React native sqlite optimised
153 lines (127 loc) · 4.05 KB
/
React native sqlite optimised
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import SQLite, { SQLiteDatabase, ResultSet } from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(true);
const database_name = "MyDatabase.db";
const database_version = "1.0";
const database_displayname = "My SQLite Database";
const database_size = 200000;
interface Item {
id?: number;
name: string;
description: string;
new_column?: string;
}
class DatabaseService {
private async initDB(): Promise<SQLiteDatabase> {
return SQLite.openDatabase(
database_name,
database_version,
database_displayname,
database_size
);
}
private closeDatabase(db: SQLiteDatabase): void {
db.close().catch(error => console.error(error));
}
async createTable(): Promise<void> {
const db = await this.initDB();
await db.executeSql(`
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
description TEXT
)
`);
this.closeDatabase(db);
}
async addItem(item: Item): Promise<ResultSet> {
const db = await this.initDB();
const result = await db.executeSql('INSERT INTO items (name, description) VALUES (?, ?)', [item.name, item.description]);
this.closeDatabase(db);
return result[0];
}
async updateItem(id: number, item: Item): Promise<ResultSet> {
const db = await this.initDB();
const result = await db.executeSql('UPDATE items SET name = ?, description = ? WHERE id = ?', [item.name, item.description, id]);
this.closeDatabase(db);
return result[0];
}
async deleteItem(id: number): Promise<ResultSet> {
const db = await this.initDB();
const result = await db.executeSql('DELETE FROM items WHERE id = ?', [id]);
this.closeDatabase(db);
return result[0];
}
async getItems(): Promise<Item[]> {
const db = await this.initDB();
const [result] = await db.executeSql('SELECT * FROM items', []);
this.closeDatabase(db);
const items: Item[] = [];
for (let i = 0; i < result.rows.length; i++) {
items.push(result.rows.item(i));
}
return items;
}
async updateDatabaseVersion(newVersion: number): Promise<void> {
const db = await this.initDB();
const [result] = await db.executeSql('PRAGMA user_version');
const currentVersion = result.rows.item(0).user_version;
if (currentVersion < newVersion) {
await db.executeSql('ALTER TABLE items ADD COLUMN new_column TEXT');
await db.executeSql(`PRAGMA user_version = ${newVersion}`);
}
this.closeDatabase(db);
}
}
export default new DatabaseService();
import React, { useEffect, useState } from 'react';
import { View, Text, Button } from 'react-native';
import DatabaseService from './DatabaseService';
interface Item {
id: number;
name: string;
description: string;
new_column?: string;
}
const App: React.FC = () => {
const [items, setItems] = useState<Item[]>([]);
useEffect(() => {
(async () => {
await DatabaseService.createTable();
await DatabaseService.updateDatabaseVersion(2);
await fetchItems();
})();
}, []);
const fetchItems = async () => {
const data = await DatabaseService.getItems();
setItems(data);
};
const addItem = async () => {
const newItem: Item = { name: 'New Item', description: 'Item Description' };
await DatabaseService.addItem(newItem);
await fetchItems();
};
const updateItem = async (id: number) => {
const updatedItem: Item = { name: 'Updated Item', description: 'Updated Description' };
await DatabaseService.updateItem(id, updatedItem);
await fetchItems();
};
const deleteItem = async (id: number) => {
await DatabaseService.deleteItem(id);
await fetchItems();
};
return (
<View>
<Button title="Add Item" onPress={addItem} />
{items.map(item => (
<View key={item.id}>
<Text>{item.name}</Text>
<Text>{item.description}</Text>
<Button title="Update" onPress={() => updateItem(item.id)} />
<Button title="Delete" onPress={() => deleteItem(item.id)} />
</View>
))}
</View>
);
};
export default App;