Coder Social home page Coder Social logo

react-native-sqlite-tool's Introduction

react-native-sqlite-tool

This repository is based on react-native-sqlite-storage, you should install and link react-native-sqlite-storage first

Installation

 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

    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<T> {
        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;

    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;

    const item={value:"new value"}
    demoSqlInstance.updateByCondition(item,{id:"123"})

    updateByPrimaryKey(newParams:T): Promise

    const item = { id: "123", value: "new value" }
    demoSqlInstance.updateByPrimaryKey(item);

    deleteByPrimaryKey(primaryValue: any): Promise

    demoSqlInstance.deleteByPrimaryKey("123")

    selectByPrimaryKey(primaryValue: any): Promise<T | null>

    demoSqlInstance.selectByPrimaryKey("123")

    select(selectSql?: string, orderSql?: string): Promise<[] | T[]>

    demoSqlInstance.select()
    demoSqlInstance.select("id=123")
    demoSqlInstance.select("id=123","create_time desc")

    selectOne(selectSql?: string): Promise<T | null>

    demoSqlInstance.selectOne("id=123")

    selectBySql(selectSql: string): Promise<T[]>

    demoSqlInstance.selectBySql("select * from todo where id='123' order by create_time desc")

    executeSql(sql: string): Promise

    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. Thanks!

That's all. Enjoy! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.