Question

I'm using Play Framework 2.1 and I'm getting an exception when I try to execute this MySQL code to create a new table.

[SQLException: Parameter index out of range (1 > number of parameters, which is 0).]

The code being executed to call this is this def:

def create(name: String):Boolean = {
if(!contains(name)) // a function that tests if the table already exists
{
  val query = SQL("""
      CREATE TABLE `database`.`{name}` (
      `id` INT NOT NULL ,
      `address` TEXT NULL ,
      `city` TEXT NULL ,
      `state` TEXT NULL ,
      `zip` INT NULL ,
      `ownerid` INT NULL ,
      `price` DOUBLE NULL ,
      `rooms` INT NULL ,
      `available` BIT NULL ,
      `type` TEXT NULL ,
      PRIMARY KEY (`id`) );
  """).on('name -> name).execute()(conn) //Play compilation error is highlighting this line
  true
}
else    false

}

This works if I just use the "+name+" method of changing the string, but that leaves it open to SQL injection and using .on seems the proper way to do this.

Was it helpful?

Solution

So what it looks like is the PreparedStatement can't use tablenames as parameters, so it looks like I'll have to manually change the String. If anyone has any insight on how to do this while avoiding SQL Injection, that would be great.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top