在play框架SQL anorm中插入两个值列表(Insert Two List of values in play framework SQL anorm)

编程入门 行业动态 更新时间:2024-10-26 12:30:57
在play框架SQL anorm中插入两个值列表(Insert Two List of values in play framework SQL anorm) mysql

目前我正在开发Playframework。 我需要使用Anorm将两个List值插入数据库。 一个是names: List[String] ,另一个是numbers: List[Int] ,它们都具有相同的大小。

我需要在Database Table中的同一Row中插入names第一个位置和numbers List第一个位置,就像明智地需要在两个Lists插入所有值一样。

我试过了 :

for (no<- 0 to (names.size-1)) { SQL( """ insert into table(NAME,NUMBER) values( names[{no}],numbers[{no}] ) """).on( 'no-> no ).executeUpdate() }

它给我以下错误[MySQLSyntaxErrorException: 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 '[0],numbers[0] [MySQLSyntaxErrorException: 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 '[0],numbers[0]

我也尝试过

for (name<- names;number<- numbers) { SQL( """ insert into table(NAME,NUMBER) values( {name},{number} ) """).on( 'name-> name, 'number-> number ).executeUpdate() }

但它像java中的two for loops一样充当了two for loops 。

for(){ for(){} }

Currently I am working on Playframework. I need to insert two List values into the database by using Anorm. One is names: List[String] and another one is numbers: List[Int], and they both have the same size.

I need to insert first position of names and first position of numbers List in same Row in Database Table like wise need to insert all values in the both Lists.

I tried :

for (no<- 0 to (names.size-1)) { SQL( """ insert into table(NAME,NUMBER) values( names[{no}],numbers[{no}] ) """).on( 'no-> no ).executeUpdate() }

It gives me the below error [MySQLSyntaxErrorException: 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 '[0],numbers[0]

and I also tried with

for (name<- names;number<- numbers) { SQL( """ insert into table(NAME,NUMBER) values( {name},{number} ) """).on( 'name-> name, 'number-> number ).executeUpdate() }

but it was act as two for loops like in java.

for(){ for(){} }

最满意答案

names[{no}]的SQL语句无法以任何方式工作:数据库没有Scala值names 。

你可以使用.zipped :

scala> (List("A", "B"), List(1.2F, 34.5F)).zipped.foreach { (str, f) => println(s"zipped: $str -> $f") } zipped: A -> 1.2 zipped: B -> 34.5

然后Anorm执行如下。

(names, numbers).zipped.foreach { (name, num) => SQL"insert into table(NAME,NUMBER) values($name, $num)".executeUpdate() }

The SQL statement with names[{no}] cannot work in any way: the database doesn't have the Scala value names.

You can use .zipped:

scala> (List("A", "B"), List(1.2F, 34.5F)).zipped.foreach { (str, f) => println(s"zipped: $str -> $f") } zipped: A -> 1.2 zipped: B -> 34.5

Then the Anorm execution would be as following.

(names, numbers).zipped.foreach { (name, num) => SQL"insert into table(NAME,NUMBER) values($name, $num)".executeUpdate() }

更多推荐

本文发布于:2023-04-29 10:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   两个   列表   anorm   play

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!