MySQL插入自动增量 [英] MySQL insert with auto increment

查看:50
本文介绍了MySQL插入自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据从我的应用程序插入到我的数据库中,但我不断收到如下所述的错误.

Im trying to insert some data from my application into my database, but I keep getting the error stated below.

我的表中有 4 列,其中一列设置为自动递增.我怀疑这是导致错误的原因.

I have 4 columns in my table and one is set to auto increment. I suspect that is causing the error.

谁能帮我解决这个问题?

Can anyone help me out with this?

表格列:

Id (auto increment), userID(generated from application), username, name

代码:

    String sql = "insert into details values (?, ?, ?)";

    PreparedStatement preparedStatement = connect.prepareStatement(sql);
    preparedStatement.setInt(1, userID);
    preparedStatement.setString(2, username);
    preparedStatement.setString(3, name);
    preparedStatement.executeUpdate(); 

    preparedStatement.executeUpdate(sql);

错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1

推荐答案

显然这只是一个简单的人为错误.写了两次执行语句.

Apparently it was just a simple human error. Wrote the execute statement twice.

    String sql = "insert into details values (?, ?, ?)";

    PreparedStatement preparedStatement = connect.prepareStatement(sql);
    preparedStatement.setInt(1, userID);
    preparedStatement.setString(2, username);
    preparedStatement.setString(3, name);

    preparedStatement.executeUpdate(); <-- Changed to preparedStatement.execute();

    preparedStatement.executeUpdate(sql); <-- Removed this line

这篇关于MySQL插入自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆