SQL只插入到某些列

时间:2016-04-12 02:23:42

标签: java mysql sql oracle

我有一个包含5列的数据库。第一列是ID,每当使用此语句添加新行时,ID将自动递增。

ALTER TABLE help MODIFY COLUMN id INT auto_increment

所以,因为这会自动增加我不想把它设置为任何东西,因此我想到了这个陈述。但是,它会留下语法错误。有什么想法吗?

   String update = "INSERT INTO help(" + name + ", " + area + ", " + date + ", " + message + ") VALUES(?, ?, ?, ?)";
        try {
            connection = plugin.getHikari().getConnection();
            // Inserting into the table
            statement = connection.prepareStatement(update);
            // Replace the '?' with the actual information
            statement.setString(1, name);
            statement.setString(2, area);
            statement.setString(3, date);
            statement.setString(4, message);
            statement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }

谢谢, - Nicster

PS:是的,这是我的SQL冒险D的第2天:

3 个答案:

答案 0 :(得分:2)

您可以在查询中参数化,但不能参数列和表的名称。所以,你需要写:

String update = "INSERT INTO help(name, area, date, message) VALUES(?, ?, ?, ?)";
    try {
        connection = plugin.getHikari().getConnection();
        // Inserting into the table
        statement = connection.prepareStatement(update);
        // Values
        statement.setString(1, name);
        statement.setString(2, area);
        statement.setString(3, date);
        statement.setString(4, message);
        statement.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }

答案 1 :(得分:2)

您正在错误地执行SQ​​L。使用预备陈述时,您需要执行以下操作:

String update = "INSERT INTO help (column1, column2, column2, column4) VALUES(?, ?, ?, ?)";

答案 2 :(得分:0)

尝试用

替换data1<-c('/index.php/search?', '/tabel/graphic1_.php?', '/mod/Layout/variableView2.php?', '/table/tblmon-frameee.php?') tes<-c('http://aladdine/index.php/search?', 'http://aladdine/mod/params/returnParams.php', 'http://aladdine/mod/Layout/variableView2.php', 'http://aladdine/index.php/bos/index?', 'http://aladdine/index.php/Bos') > lapply(data1,FUN = function(x) which(grepl(x,tes))) [[1]] [1] 1 [[2]] integer(0) [[3]] [1] 3 [[4]] integer(0)
String update = "INSERT INTO help(?, ?, ?, ?) VALUES(?, ?, ?, ?)";