Giter Site home page Giter Site logo

sqlclient's Introduction

SQLClient

A library to help you that connect MySQL as simply as HTTP request on Android.

Usage

  1. Your app module must depend on jdbc library: https://github.com/ysy950803/SQLClient/blob/master/app/libs/mysql-connector-java-5.1.36-bin.jar
  2. Download dbconnector module.
  3. If your Android Studio is the lastest version, edit your app's build.gradle and add as below:
dependencies {
    implementation project(':dbconnector')
}

If your Android Studio is the old version just like 2.3, add as below:

dependencies {
    compile project(':dbconnector')
}

Example

You can connect MySQL Databases and execute INSERT, UPDATE, SELECT, DELETE operations as using HttpClient to GET and POST.

    // Single SQL operation.
    private void queryDataFromDB() {
        SQLEntity<String> entity = new SQLEntity<>();
        entity.setSQL("select * from table_name where id = 1");
        entity.setType(SQLRequest.RequestType.SELECT);
        SQLClient.invokeStringRequest(this, entity, new SQLCallback<ResultSet>() {
            @Override
            public void onSuccess(ResultSet res) {
                // List<DataEntity> list = new ArrayList<>();
                try {
                    while (res.next()) {
                        // DataEntity data = new DataEntity();
                        String resultColumn1 = res.getString(1);
                        int resultColumn2 = res.getInt(2);
                        // ...
                        // data.setOne(resultColumn1);
                        // data.setTwo(resultColumn2);
                        // ...
                        // list.add(data);
                    }
                    // ...(some code using list)
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int errorCode) {
                Toast.makeText(MainActivity.this, SQLErrorConstant.getErrorMsg(errorCode),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    // Also support multi SQL operations.
    private void updateDataToDB() {
        SQLEntity<List<String>> entity = new SQLEntity<>();
        List<String> sqls = new ArrayList<>();
        sqls.add("update ...");
        sqls.add("insert into ...");
        sqls.add("delete ...");
        entity.setSQL(sqls);
        entity.setType(SQLRequest.RequestType.MULTI_UPDATE);
        SQLClient.invokeListRequest(this, entity, new SQLCallback<List<ResultSet>>() {
            @Override
            public void onSuccess(List<ResultSet> res) {
                // Usually, res here is not useful.
                // Do something about success.
                Toast.makeText(MainActivity.this, "Update DB Successfully.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(int errorCode) {
                Toast.makeText(MainActivity.this, SQLErrorConstant.getErrorMsg(errorCode),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    // Please invoke cancel method to clear all unfinished requests.
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SQLClient.cancel(this);
    }

sqlclient's People

Contributors

ysy950803 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.