Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dyazincahya/sqlite-helper-nativescript
This helper can helping you when you using SQLite in Nativescript (for NS 8 or newer)
https://github.com/dyazincahya/sqlite-helper-nativescript
example-code helper javascript nativescript nativescript-8 nativescript-core sqlite
Last synced: about 1 month ago
JSON representation
This helper can helping you when you using SQLite in Nativescript (for NS 8 or newer)
- Host: GitHub
- URL: https://github.com/dyazincahya/sqlite-helper-nativescript
- Owner: dyazincahya
- License: mit
- Created: 2024-02-25T10:46:34.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-05-21T04:44:00.000Z (6 months ago)
- Last Synced: 2024-09-27T09:03:02.304Z (about 2 months ago)
- Topics: example-code, helper, javascript, nativescript, nativescript-8, nativescript-core, sqlite
- Language: JavaScript
- Homepage:
- Size: 48.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SQLite Helper Nativescript
This ```helper``` can helping you when you using SQLite in Nativescript## Dependency
Before you using this helper, you must be install plugin [nativescript-community/sqlite](https://github.com/nativescript-community/sqlite), cause this helper running on this plugin.## Requirement
- [Nativescript 8 or newer](https://nativescript.org/)
- [nativescript-community/sqlite](https://github.com/nativescript-community/sqlite)## Instructions
1. Download file [sqlite_helper.js](https://github.com/dyazincahya/sqlite-helper-nativescript/blob/main/sqlite_helper.js) and save that file here : ```\YOUR_NATIVESCRIPT_PROJECT\app```
2. Create ```.db``` file using [SQLite Browser](https://sqlitebrowser.org)
3. And after that put the ```your_database.db``` in ```\YOUR_NATIVESCRIPT_PROJECT\app```
4. Then in the ```sqlite_helper.js``` file, find ```openOrCreate("your_database.db")``` code and change with your database name.
5. Still in the ```sqlite_helper.js``` file, find ```showError``` variable then set it to ```true``` if you want to see all errors that occur during development on your sqlite.
6. import file sql_helper.js on your module, like :
``` javascript
import { SQL__select, SQL__selectRaw, SQL__insert, SQL__update, SQL__delete, SQL__truncate, SQL__query } from "~/sqlite_helper";
```
7. Avaliable methode on ```sql_helper.js```
| Method | Description | Return |
|-------------------|-------------------------------------------------------------|--------|
| SQL__select(...) | for get data from table | Array |
| SQL__selectRaw(...) | for get data from table, same like ```SQL_select```, but here you can execute simple or advance query, like JOIN Query or etc | Array |
| SQL__insert(...) | for insert data to table | void |
| SQL__update(...) | for update data to table | void |
| SQL__delete(...) | for delete data row from table | void |
| SQL__truncate(...) | for clear all data on the table | void |
| SQL__query(...) | for execute raw query like Create new Table or Etc | ? |
8. For details, you can look at the [sqlite_helper.js](https://github.com/dyazincahya/sqlite-helper-nativescript/blob/main/sqlite_helper.js) file directly## Sample Code
### TABLE
Assummed I have a **users** table like this :
``` sql
CREATE TABLE "users" (
"id" INTEGER NOT NULL UNIQUE,
"fullname" TEXT NOT NULL,
"about" TEXT DEFAULT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
)
```#### CREATE new TABLE USERS
Before you can do something, make sure you already create the table. for create table in SQLite, you can use method ```SQL_query``` from ```sqlite_helper.js```, example like this :
``` javascript
import { SQL__query } from "~/sqlite_helper";SQL__query(`CREATE TABLE IF NOT EXISTS "users" (
"id" INTEGER NOT NULL UNIQUE,
"fullname" TEXT NOT NULL,
"about" TEXT DEFAULT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
)`);
```When you make create table query, make sure you use ```IF NOT EXISTS``` in your query. This is useful to avoid double execution of your query.
#### GET all USERS
``` sql
SQL__select(tableName)
```
I want to get all user data from the table
``` javascript
import { SQL__select } from "~/sqlite_helper";SQL__select("users").then((res) => {
console.log(res);
console.log(res.length);
});
```#### GET USER where FULLNAME is JOHN DUO
```sql
SQL__select(tableName, fields, conditionalQuery)
```
I want to get all user data from table by fullname is john duo
``` javascript
import { SQL__select } from "~/sqlite_helper";SQL__select("users", "*", "WHERE fullname='john duo'").then((res) => {
console.log(res);
console.log(res.length);
});
```#### CREATE new USER
``` sql
SQL__insert(tableName, data)
```
I want to create new user with fullname is Kang Cahya and about is Designer
``` javascript
import { SQL__insert } from "~/sqlite_helper";SQL__insert("users", [
{ field: "fullname", value: "Kang Cahya" },
{ field: "about", value: "Designer" }
]);
```#### UPDATE data USER by ID
``` sql
SQL__update(tableName, data, id, conditionalQuery)
```
I want to update field ABOUT by user ID number 3
``` javascript
import { SQL__insert } from "~/sqlite_helper";SQL__update("users", [{ field: "about", value: "Tester" }], 3);
```#### UPDATE data USER with WHERE condition
``` sql
SQL__update(tableName, data, id, conditionalQuery)
```
I want to update field about by user ID number 3
``` javascript
import { SQL__insert } from "~/sqlite_helper";SQL__update("users", [{ field: "about", value: "Tester" }], null, "WHERE id='3'");
```## More info about Sqlite
[Sqlite Tutorial by Tutorialspoint](https://www.tutorialspoint.com/sqlite/index.htm)