Uploading a file to a table

I created a database with Filestream activated, and I created a “pdfs” table inside of that the database which has the following columns (Name,Address,Date,File) and I am trying to use the file uploader in perspective to upload a pdf file to the table. The code is:

	filename = event.file.name
	fileBytes = event.file.getBytes()
	
	system.db.runPrepUpdate(
		"INSERT INTO pdfs (Name,Address,Date,File) VALUES (?,?,?,?)",
		[filename,'Files',system.date.now(),fileBytes],'QualityFiles')
	
	self.getSibling("Table").refreshBinding("props.data")

The error is:

com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:runAction>", line 7, in runAction
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO pdfs (Name,Address,Date,File) VALUES (?,?,?,?), [8C400216 Packaging Instructions.pdf, Files, Fri Apr 09 17:25:31 EDT 2021, [B@627e6452], QualityFiles, , false, false)

	caused by org.python.core.PyException
Traceback (most recent call last):
  File "<function:runAction>", line 7, in runAction
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO pdfs (Name,Address,Date,File) VALUES (?,?,?,?), [8C400216 Packaging Instructions.pdf, Files, Fri Apr 09 17:25:31 EDT 2021, [B@627e6452], QualityFiles, , false, false)

	caused by Exception: Error executing system.db.runPrepUpdate(INSERT INTO pdfs (Name,Address,Date,File) VALUES (?,?,?,?), [8C400216 Packaging Instructions.pdf, Files, Fri Apr 09 17:25:31 EDT 2021, [B@627e6452], QualityFiles, , false, false)
	caused by SQLServerException: Incorrect syntax near the keyword 'File'.

Ignition v8.1.4 (b2021040109)
Java: Azul Systems, Inc. 11.0.9

FILE is a T-SQL keyword. Don’t use it for a column name.

The new code:

filename = event.file.name
fileBytes = event.file.getBytes()

system.db.runPrepUpdate(
	"INSERT INTO FILES (file_name,Upload_Date, DOC) VALUES (?,?,?)",
		[filename,system.date.now(),fileBytes]
	)

self.getSibling("Table").refreshBinding("props.data")

The new error:
com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
File “function:runAction”, line 7, in runAction
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258)
at jdk.internal.reflect.GeneratedMethodAccessor171.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO FILES (file_name,Upload_Date, DOC) VALUES (?,?,?), [8C400216 Packaging Instructions.pdf, Mon Apr 12 10:47:34 EDT 2021, [B@5fc101f8], GeneralConfiguration, , false, false)

at org.python.core.Py.JavaError(Py.java:552)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258)
at jdk.internal.reflect.GeneratedMethodAccessor171.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:538)
at org.python.core.PyObject.__call__(PyObject.java:497)
at org.python.core.PyObject.__call__(PyObject.java:501)
at org.python.pycode._pyx9537043.runAction$1(<function:runAction>:12)
at org.python.pycode._pyx9537043.call_function(<function:runAction>)
at org.python.core.PyTableCode.call(PyTableCode.java:171)
at org.python.core.PyBaseCode.call(PyBaseCode.java:308)
at org.python.core.PyFunction.function___call__(PyFunction.java:471)
at org.python.core.PyFunction.__call__(PyFunction.java:466)
at org.python.core.PyFunction.__call__(PyFunction.java:461)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:836)
at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:820)
at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:687)
at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:986)
at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:752)
at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:106)
at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:71)
at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.lambda$call$0(ActionCollection.java:263)
at com.inductiveautomation.perspective.gateway.api.LoggingContext.mdc(LoggingContext.java:54)
at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:252)
at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:221)
at com.inductiveautomation.perspective.gateway.threading.BlockingTaskQueue$TaskWrapper.run(BlockingTaskQueue.java:154)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException
Traceback (most recent call last):
File “function:runAction”, line 7, in runAction
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:362)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:258)
at jdk.internal.reflect.GeneratedMethodAccessor171.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO FILES (file_name,Upload_Date, DOC) VALUES (?,?,?), [8C400216 Packaging Instructions.pdf, Mon Apr 12 10:47:34 EDT 2021, [B@5fc101f8], GeneralConfiguration, , false, false)

... 34 more

Caused by: java.lang.Exception
com.inductiveautomation.ignition.common.GenericTransferrableException: Error executing system.db.runPrepUpdate(INSERT INTO FILES (file_name,Upload_Date, DOC) VALUES (?,?,?), [8C400216 Packaging Instructions.pdf, Mon Apr 12 10:47:34 EDT 2021, [B@5fc101f8], GeneralConfiguration, , false, false)
… 33 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException
com.inductiveautomation.ignition.common.GenericTransferrableException: Invalid column name ‘file_name’.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:256)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1621)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:592)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:522)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7194)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2930)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:248)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:223)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:471)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper$SRPreparedStatement.executeUpdate(SRConnectionWrapper.java:1016)
at com.inductiveautomation.ignition.gateway.datasource.SRConnectionWrapper.runPrepUpdate(SRConnectionWrapper.java:183)
at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities._runPrepStmt(GatewayDBUtilities.java:186)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runPrepUpdate(AbstractDBUtilities.java:256)
… 31 more

Ignition v8.1.4 (b2021040109)
Java: Azul Systems, Inc. 11.0.9

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException
com.inductiveautomation.ignition.common.GenericTransferrableException: Invalid column name ‘file_name’.

Seems like you don't have the column named 'file_name' in your table.

1 Like

I do:

FYI: The image shows Query, but it is Update Query

Can you post the table definition from SQL Server Management Studio?
Do you have multiple database connections defined in your gateway? Is it possible that the default database connection for this project isn’t using the correct database connection?

I suspect there are duplicate tables. One owned by dbo and one perhaps owned by the user connecting to the db.

When you have an unqualified reference to a schema object (such as a table) — one not qualified by schema name — the object reference must be resolved. Name resolution occurs by searching in the following sequence for an object of the appropriate type (table) with the specified name. The name resolves to the first match:

  • Under the default schema of the user.
  • Under the schema ‘dbo’.

Try using the full schema to your table.

1 Like

How can I change the default database?

In designer check Project → Project Properties and under General there is a Default Database. That is the database connection that the project is going to use if it isn’t specified in any database call.

It worked. I changed the default DB and it worked. I thought it would detect it automatically from the Named Queries since it is the only one I am using.

Thanks!

I’ve gotten in the habit of always specifying the db connection when using scripting, even if the default is specified in the project properties.

  • It avoids confusion as to that connection you’re using
  • It works regardless of the scope where the script is run. (Designer / Client / Gateway)
3 Likes

Yep. Helps with code readability later in life.