2
votes

I've got an issue and would be nice to receive feedback from you. While updating the data of my table in MySQL, the following message appears:

Column count doesn't match value count at row 1

The table is:

IdUsuari INT NOT NULL AUTO_INCREMENT, Nickname VARCHAR(50) NOT NULL, DataRegistre DATE NOT NULL, DataDarrerAcces DATE NOT NULL, NumLlistes INT NOT NULL DEFAULT 0, Password VARCHAR(10) NOT NULL, Admin INT NOT NULL, PRIMARY KEY (IdUsuari)

And the code:

public static void RegistreUsuari(int port, String ip, String nickname, String password) throws SQLException{ /*Creem un usuari*/

        java.util.Date dt = new java.util.Date(); 



        Connection conn = getConnection(port,ip);
        Statement st = null;
        st = conn.createStatement();



        String query = new String();
        /*Data*/

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        String currentTime = sdf.format(dt);
        query = "INSERT INTO usuari(Nickname, Password, DataRegistre, DataDarrerAcces, NumLlistes, Admin) VALUES('" + nickname +"','"+password+"','"+currentTime+"','"+currentTime+"',0)";
        st.executeUpdate(query); }

And

Database.RegistreUsuari(port, ip, "Elder", "hola");

Thank you in advance!

1
Apart from the fact that storing passwords in plaintext is a bad idea in general, you should really look into PreparedStatement for any sort of user input!Dragondraikk
I'm voting to close this question as off-topic because You have 6 columns 6 values in your query and that's the answer.Hanky Panky
@Hanky웃Panky I don't see how that makes it off-topic, it just means that that'S where the problem lies as you have already stated in your answerDragondraikk
You should be using a PreapredStatement, see Using Prepared Statements for more detailsMadProgrammer
@Dragondraikk in my (possibly wrong) opinion, thats just a typo not a problem; hence the light hearet close-vote. Otherwise if you search SO for this very error message that this question contains there are 408 duplicate questions to be exact which is in itself a big enough close reason.Hanky Panky

1 Answers

6
votes

You are trying to insert 5 values to a table with 6 columns. You don't specify a value for NumLlistes.

If you wish the default value to be used, don't specify the NumLlistes column in the insert statement :

query = "INSERT INTO usuari(Nickname, Password, DataRegistre, DataDarrerAcces, Admin) VALUES('" + nickname +"','"+password+"','"+currentTime+"','"+currentTime+"',0)";