T
T
TILLhen2018-05-10 16:26:33
MySQL
TILLhen, 2018-05-10 16:26:33

How to add +1 to a column?

I have an application that has a built-in QR code scanner, I need to add 1 to the "Kolichestvo" column after scanning, now I've only learned how to display the entire line in the table, but I still haven't figured out how to add +1 to the column.
Code for adding a row to the table:

private static final String url = "jdbc:mysql://95.58.161.174:3306/Priem_zayvok";
    private static final String user = "Glazik";
    private static final String pass = "";
    private TextView firstName, lastName;
    public String contents;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        Intent content = this.getIntent();
        contents = content.getStringExtra("contents");
        firstName = findViewById(R.id.textViewFirstName);
        lastName = findViewById(R.id.textViewLastName);
        Button buttonLoad =  findViewById(R.id.button);

        buttonLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyTask().execute();
            }
        });
    }

    private class MyTask extends AsyncTask<Void, Void, Void>{
        private String ID="", Katridj="";
        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);
                //Statement st = con.createStatement();
                PreparedStatement stmt = null;






                String sql = "INSERT INTO `Printer` (`Katridj`) VALUES ('"+contents+"')";

                stmt =  con.prepareStatement(sql);
                stmt.addBatch();
                stmt.executeBatch();

                firstName.setText("true");

               final ResultSet rs = stmt.executeQuery("select * from Printer");
                //rs.next();
                ID = rs.getString(2);
                Katridj = rs.getString("Katridj");
                lastName.setText(Katridj);

            }
            catch (Exception e) {
                e.printStackTrace();
                firstName.setText("error");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

          //  firstName.setText(ID);
            super.onPostExecute(result);
        }
    }
}

5af44881df3a5240693641.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2018-05-10
@TILLhen

Added, do not blogodori
But seriously, this is some kind of trash. Why are you directly working with the database from the android application? For this, there should be a layer in the form of a REST API.

String sql = "UPDATE printer SET Kolichestvo = Kolichestvo + 1 WHERE id = " + id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question