Rachel
Rachel

Reputation: 63

Column count doesn't match value count at row 1 when inserting into mysql

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

Upvotes: 0

Views: 121

Answers (2)

jmng
jmng

Reputation: 2568

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.

Upvotes: 2

Omkar
Omkar

Reputation: 3100

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);

Upvotes: 1

Related Questions