java 从数组中插入/更新表中的单个列

标签 java mysql

如何在表中插入/更新单个列。我想从数组值填充该列。 enter image description here

我只想插入/更新inGram表。

我已经尝试过这个但没有成功:

String sql="INSERT INTO total_ingredient (inGram) VALUES('"+subtractIngredent[0]+"',"
        + " '"+subtractIngredent[1]+"','"+subtractIngredent[2]+"',"
        + "'"+subtractIngredent[3]+"','"+subtractIngredent[4]+"',"
        + "'"+subtractIngredent[5]+"','"+subtractIngredent[6]+"', )";

谢谢。

最佳答案

这应该写成:

String sql="INSERT INTO total_ingredient (inGram) VALUES('"+subtractIngredent[0]+"'),"
        + "('"+subtractIngredent[1]+"'),('"+subtractIngredent[2]+"'),"
        + "('"+subtractIngredent[3]+"'),('"+subtractIngredent[4]+"'),"
        + "('"+subtractIngredent[5]+"'),('"+subtractIngredent[6]+"')";

要使您的代码处理任意大小的数组,您应该考虑使用循环。正如 JB Nizer 提到的,考虑使用准备好的语句而不是自己生成查询。

关于java 从数组中插入/更新表中的单个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145583/

相关文章:

mysql - 检测表格中的模式

MySQL嵌套查询问题及优化

Java Swing——将 JPanel 的按键输入添加到 JOptionpane

java - 防止(普通用户)下载 PDF

mysql - 未定义方法 `code' 为 nil :NilClass message with rails and a legacy database

mysql - 我可以运行自己的 MySQL 实例吗?

java - 在 JTable header 中显示排序图标而不使用内置排序机制

java - 不理解 Java EE 中上下文的概念

java - 为什么 ObjectOutputStream 和 ObjectInputStream 抛出 EOFException 的这种奇怪行为?

php - 在 PHP 中获取重命名的文件名