0

I'm trying to insert three values into a table name,email and password(I know I don't have this hashed ,I'm just testing it to see if it works first) .It's telling me the column count don't match ,but the names and the number of columns do match ,I don't know what the issue could be .

This the MainActivity

package ie.example.artur.adminapp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class MainActivity extends AppCompatActivity {


    EditText editTextName,editTextEmail,editTextPassword;
    TextView textView;
    private static final String DB_URL = "jdbc:mysql://192.168.1.6/socialmedia_website";
    private static final String USER = "zzz";
    private static final String PASS = "zzz";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        textView = (TextView) findViewById(R.id.textView);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    public void btnConn(View view) {
        Send objSend = new Send();
        objSend.execute("");


    }

    private class Send extends AsyncTask<String, String, String>

    {
        String msg = "";
        String name = editTextName.getText().toString();
        String email = editTextEmail.getText().toString();
        String password = editTextPassword.getText().toString();

        @Override
        protected void onPreExecute() {
            textView.setText("Please Wait Inserting Data");
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
                if (conn == null) {
                    msg = "Connection goes wrong";
                } else {
                String query = "Insert INTO test1 (name,email,password) VALUES('"+name+","+email+","+password+"')";
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(query);
                msg = "Inserting Successful!!";

            }

                conn.close();

        }

        catch(
        Exception e
        )

        {
            msg = "Connection goes Wrong";
            e.printStackTrace();

        }

        return msg;


    }



@Override
    protected void onPostExecute(String msg) {textView.setText(msg);}



    }




}

The table in the database in phpmyadmin: enter image description here

The error : enter image description here

11
  • Can u try by adding , separated values here Insert INTO test1 (name,email,password) VALUES('" + name+email+password+"')". Here it'll take it as a single value Commented Jul 17, 2017 at 10:43
  • shouldn't you separate the values in your query with a comma? Commented Jul 17, 2017 at 10:44
  • @Raghavendra yes I can add one value ,but I want add the three values Commented Jul 17, 2017 at 10:47
  • You are passing a single value against 3 columns. Try separating your values by comma. Commented Jul 17, 2017 at 10:48
  • 1
    @Rachel check the single quote. Copy and paste derelict answer and try Commented Jul 17, 2017 at 11:18

2 Answers 2

2

The line:

String query = "Insert INTO test1 (name,email,password) VALUES('" + name+email+password+"')";

Should actually be:

String query = "Insert INTO test1 (name,email,password) VALUES('" + name+"','"+email+"','"+password+"')";

You were inserting only one value, which was the concatenation of the values of name+email+password, when you actually meant to insert the three separate values.

When you write "Insert INTO test1 (name,email,password)", that means you are going to INSERT 3 values into "test1", one value for each of the columns specified by "(name,email,password)". But, because you were concatenating name+email+password, you were only inserting one value (the result of the concatenation) instead of the three separate values. Print out the "query" string and you'll quickly grasp the difference.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree with you but that still didn't help I got the same error again :(
Nope, that was me, I had a + missing, check my code again ;)
I added that missing "+" before you update the code I was still getting the same error
Is it fixed now?
1

your query causes error with commas (",") and Blank spaces try above code

 String query = "Insert INTO test1 (name,email,password) VALUES('"+name+","+email+","+password+"')";
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(query);
                msg = "Inserting Successful!!";

Now try with this using prepared statement try above code

 String query= "Insert into table (name,email,password) Values(?,?,?)";
   Statement st = conn.preparedStatement(query);
   st.setString(1, name);
   st.setString(2, email); 
   st.setString(3, password);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.