【发布时间】:2016-02-20 13:10:24
【问题描述】:
所以基本上我创建了一个数据库,里面有一个表(仅用于测试目的),有 2 个元素(第一个 VARCHAR(255) 和年龄 INTEGER),如下所示:
protected void createTableExamPeriod() throws SQLException {
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String drop = "DROP TABLE IF EXISTS REGISTRATION ";
String table = "CREATE TABLE IF NOT EXISTS REGISTRATION " +
" (first VARCHAR(255), " +
" age INTEGER )";
stmt.executeUpdate(drop);
stmt.executeUpdate(table);
populateExamPeriodTable();
System.out.println("Created table in given database...");
}
但是当我查看我的数据库时,有一个额外的列,名为 'New' :
所以我的问题是,我如何摆脱这个额外的列,因为现在,由于这个额外的列,我无法在其中插入“正确”数量的参数:
protected void populateExamPeriodTable() throws SQLException {
Statement stmt = conn.createStatement();
String insertSql = "INSERT REGISTRATION VALUES(3 + 2 + 5)";
stmt.executeUpdate(insertSql);
}
【问题讨论】:
-
您的
INSERT语句不起作用,因为您只提供了 1 值,并且该表有两列。对于您的INSERT,您希望列first具有什么价值? -
3 + 2 + 5是一个计算结果为10的表达式,所以你说的是INSERT REGISTRATION VALUES(10)。多个值必须用逗号分隔(,),并且您的第一个值应该是字符串文字,因为列是VARCHAR,这意味着您应该使用:INSERT REGISTRATION VALUES('Hello', 10) -
谢谢!我现在改了!非常感谢大家!祝你有美好的一天! :)
标签: java mysql jdbc phpmyadmin