【问题标题】:MySQL syntax error with commas? - Java逗号的MySQL语法错误? - 爪哇
【发布时间】:2013-11-16 22:09:21
【问题描述】:

我正在尝试在数据库中插入一些数据,但遇到了一些麻烦。目前我无法将数据添加到我的表中,我不知道为什么。

我使用了一些你不需要理解的通用方法,这适用于我的所有项目,并且在任何项目中都没有任何错误。

以下是我在 EscolaDAO 中的方法:

public boolean adicionarTurno(String turno) {
        String sql = "INSERT IGNORE INTO Turno (descricao) values ?;";
        ArrayList<Object> params = new ArrayList<Object>();
        params.add(turno);
        operacaoEscrita(sql, params);
        return true;
}

从我的控制器调用方法“adicionarTurno”:

EscolaDAO modelo = new EscolaDAO();

public void adicionarHorario(Escola escola){

       for(Turno temp : escola.getTurnos()){
           System.out.println("Controle Turno: " + temp.getNome());
           modelo.adicionarTurno(temp.getNome());
        }
}

这是我的 Escola 实体:

public class Escola {

    private String nomeEscola;
    private ArrayList<Turno> turnos = new ArrayList<Turno>();
    private ArrayList<Dia> dias = new ArrayList<Dia>();

    public Escola() { }

    public Escola(String nomeEscola) {
        this.nomeEscola = nomeEscola;
    }

    public ArrayList<Dia> getDias() {
        return dias;
    }

    public void setDias(ArrayList<Dia> dias) {
        this.dias = dias;
    }

    public String getNomeEscola() {
        return nomeEscola;
    }

    public void setNomeEscola(String nomeEscola) {
        this.nomeEscola = nomeEscola;
    }

    public ArrayList<Turno> getTurnos() {
        return turnos;
    }

    public void setTurnos(ArrayList<Turno> turnos) {
        this.turnos = turnos;
    }

}


当我尝试在 arrayList 中插入一些值并尝试将它们插入数据库中时(使用 EscolaController),我收到以下错误消息(第一行是我尝试插入的“toString”):
Controle Turno: Vespertino
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 ''Vespertino'' at line 1

Controle Dia: Segunda-feira
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 ''Segunda-feira'' at line 1

Controle Dia: Terça-feira
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 ''Terça-feira'' at line 1

Controle Dia: Quarta-feira
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 ''Quarta-feira'' at line 1

Controle Dia: Quinta-feira
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 ''Quinta-feira'' at line 1

我做错了什么?

【问题讨论】:

    标签: java mysql sql syntax


    【解决方案1】:

    正确的语法是:

    insert ignore into Turno values (?)
    

    请注意:

    • 值需要用括号括起来
    • 结尾不能有分号。

    您最好指定列名,以使您的代码更清晰、更安全:

    insert ignore into Turno (name_of_the_column) values (?)
    

    阅读文档可以节省时间:

    http://dev.mysql.com/doc/refman/5.7/en/insert.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 2017-10-27
      相关资源
      最近更新 更多