Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wanxianliang/react-native-sqlite-tool
a easy way to use sqlite in react native
https://github.com/wanxianliang/react-native-sqlite-tool
react-native-sqlite reactnative sqlite sqlite3
Last synced: 28 days ago
JSON representation
a easy way to use sqlite in react native
- Host: GitHub
- URL: https://github.com/wanxianliang/react-native-sqlite-tool
- Owner: wanxianliang
- Created: 2021-02-22T13:37:08.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-03-08T01:48:23.000Z (over 3 years ago)
- Last Synced: 2024-10-09T12:33:46.020Z (about 1 month ago)
- Topics: react-native-sqlite, reactnative, sqlite, sqlite3
- Language: TypeScript
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# react-native-sqlite-tool
This repository is based on *[react-native-sqlite-storage](https://www.npmjs.com/package/react-native-sqlite-storage)*, you should install and link react-native-sqlite-storage first
# Installation
```js
npm install --save react-native-sqlite-tool
```Then follow the instructions to use react-native-sqlite-tool;
#### Steps
1. Create a class for one table
```js
import { SqlLite } from "react-native-sqlite-tool";
//if you use typescript
export interface T {
//primary key
id?: string;
value?: string;
}
//Set primaryKey dbName tableName createTableSql
class DemoSql extends SqlLite {
primaryKey = "id";
dbName = "default.db";
tableName = "todo";
createTableSql = `CREATE TABLE IF NOT EXISTS todo (
id varchar(32) NOT NULL,
value varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
)`;
constructor() {
super();
//important
this.init();
}
}
export const DemoSqlInstance = new DemoSql();
```
2. You can use follow apis
insert(item: T): Promise;
```js
import { demoSqlInstance } from "./Demo";
export interface T {
//primary key
id?: string;
value?: string;
}
class Todo {
insert(item: T) {
return demoSqlInstance.insert(item);
}
}
```
updateByCondition(newParams: T, conditionParams: T): Promise;
```js
const item={value:"new value"}
demoSqlInstance.updateByCondition(item,{id:"123"})
```
updateByPrimaryKey(newParams:T): Promise
```js
const item = { id: "123", value: "new value" }
demoSqlInstance.updateByPrimaryKey(item);
```deleteByPrimaryKey(primaryValue: any): Promise
```js
demoSqlInstance.deleteByPrimaryKey("123")
```
selectByPrimaryKey(primaryValue: any): Promise
```js
demoSqlInstance.selectByPrimaryKey("123")
```
select(selectSql?: string, orderSql?: string): Promise<[] | T[]>
```js
demoSqlInstance.select()
demoSqlInstance.select("id=123")
demoSqlInstance.select("id=123","create_time desc")
```
selectOne(selectSql?: string): Promise
```js
demoSqlInstance.selectOne("id=123")
```
selectBySql(selectSql: string): Promise
```js
demoSqlInstance.selectBySql("select * from todo where id='123' order by create_time desc")
```
executeSql(sql: string): Promise
```js
demoSqlInstance.executeSql("select * from todo where id='123' order by create_time desc")
```
If you like this tool, please add a star to [my Github Repo](https://github.com/wanxianliang/react-native-sqlite-tool). Thanks!
That's all. Enjoy! :)