Use runPrepQuery not in the default database, but result shows the database is not defined

I try to download the file from the database, but not from the default database. The new database called SmartOpsAU. When I use the runPrepQuery, the result show the new database is not defined.

system.db.runPrepQuery(query, args, [database], [tx])

	id = self.view.params.post[0].fileid

	query = "select filename,attachment from postfile where id =?"
	args = [id]
	data = system.db.runPrepQuery (query, args, [SmartOpsAU])

	filename = data[0][0]
	attachment = data[0][1]

   	system.prespective.download(filename,attachment)

Thanks for help

The database argument in system.db.runPrepQuery is the name of the database connection. Your database connections are set up on the gateway under Config > Databases > Connections. If SmartOpsAU is a new database you will need to setup a connection for it.

1 Like


Thanks for reply, the new database have been connected.

I've just re-read your original post and noticed a couple more things:

  • you have put the database in square brackets, which is not the correct syntax. The square brackets around the parameter in the function name denote that the parameter is optional
  • you have not enclosed SmartOpsAU in quotation marks to indicate that it is a string value, not a variable name
5 Likes

Amy gave you the solution, but there's something else that's worth mentioning.

The error message doesn't say that the database is not defined, it says that the SmartOpsAu variable is not defined. Error messages like that are VERY useful for debugging, pay attention to what they say and more often than not they'll give you the answers you're looking for.

4 Likes

Thanks Amy, have been solved.

Thanks for advice.