# Utf-8 script error

**URL:** <https://forum.inductiveautomation.com/t/utf-8-script-error/12634>\
**Category:** Ignition\
**Created:** [February 2, 2017, 11:22am UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634 "2017-02-02T11:22:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adriangarridob](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@adriangarridob](https://forum.inductiveautomation.com/u/adriangarridob)\
**Post date:** [February 2, 2017, 11:22am UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634/1 "2017-02-02T11:22:55Z")

</div>

Hi, I have a problem with a script. I always get the same error with the last defined variable, as shown below.

//error  
Traceback (most recent call last):

File “event:actionPerformed”, line 25, in

File “C:\Users\adria.ignition\cache\gwlocalhost\_8088\_8043\_main\C1\pylib\encodings\utf\_8.py”, line 16, in decode

```
return codecs.utf_8_decode(input, errors, True)

```

UnicodeDecodeError: ‘utf-8’ codec can’t decode bytes in position 10-12: invalid data

//code  
import time  
import java  
from [java.io](http://java.io) import File  
from java.lang import \*

pdf=event.source.parent.getComponent(‘pdf’).text  
archivo = open(pdf, “rb”)  
data = archivo.read()

if len(event.source.parent.parent.getComponent(‘incidencia’).text) \<1:  
event.source.parent.getComponent(‘asterisco’).visible=1  
event.source.parent.getComponent(‘errorRojo’).visible=1  
else:  
event.source.parent.getComponent(‘asterisco’).visible=0  
event.source.parent.getComponent(‘errorRojo’).visible=0  
incidencia=event.source.parent.parent.getComponent(‘incidencia’).text  
t\_error=event.source.parent.getComponent(‘error’).selectedStringValue  
f\_inicio=event.source.parent.getComponent(‘f\_inicio’).formattedDate  
observaciones=event.source.parent.getComponent(‘observaciones’).text  
f\_sistema=time.strftime("%d/%m/%y %H:%M:%S")  
event.source.parent.getComponent(‘observaciones’).text=""  
event.source.parent.getComponent(‘pdf’).text=""  
event.source.parent.parent.getComponent(‘incidencia’).text=""  
DNI=event.source.parent.parent.DNI  
query=“INSERT INTO incidencias (tipo\_error, tipo\_incidencia, observaciones,f\_inicio, f\_sistema, dni\_operario, solucionado, doc\_pdf) VALUES (’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’)” %(t\_error, incidencia, observaciones, f\_inicio, f\_sistema,DNI ,“No”,data)  
results=system.db.runPrepUpdate(query,[],‘DB’)

archivo.close()

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [February 2, 2017, 1:48pm UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634/2 "2017-02-02T13:48:24Z")

</div>

runPrepUpdate requires a list of arguments. Putting all of your data into the query string defeats the purpose of using runPrepUpdate.

Try this:

[code]   
query = “INSERT INTO incidencias (tipo\_error, tipo\_incidencia, observaciones,f\_inicio, f\_sistema, dni\_operario, solucionado, doc\_pdf) VALUES (?, ?, ?, ?, ?, ?, ?, ?)”

results=system.db.runPrepUpdate(query,[t\_error, incidencia, observaciones, f\_inicio, f\_sistema, DNI ,“No”, data],‘DB’)[/code]

Your data is now

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [February 3, 2017, 1:27pm UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634/3 "2017-02-03T13:27:27Z")

</div>

A PDF file is a binary file (or at least has binary parts), where random bit patterns can occur.

UTF-8 on the other hand (and quite a few other text encodings) have invalid patterns. See [en.wikipedia.org/wiki/UTF-8#Invalid\_code\_points](https://en.wikipedia.org/wiki/UTF-8#Invalid_code_points) . Bit sequences that cannot be used in text to keep it decodable and keep compatibility with other standards.

Thus treating a PDF file as text shouldn’t be done. Even when parametrised, I guess the DB schema still treat it as being text, and it won’t work as expected.

There are ways to convert binary data to clean text. Like a [base64 encoding](https://en.wikipedia.org/wiki/Base64), which uses 64 safe characters to encode groups of 6 bits (2^6 = 64). Though these do tend to waist a bit of space (a safe character uses 8 bits of space in utf-8, while it’s only encoding 6 bits), it’s a good option when only plain-text communication or storage is available.

But I guess it would be a better option to not store raw PDF documents in a database at all. Store them in a structured way on the HDD, and only keep a reference to it in your database (like the file path).

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 3, 2017, 2:31pm UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634/4 "2017-02-03T14:31:03Z")

</div>

[quote=“Sanderd17”]But I guess it would be a better option to not store raw PDF documents in a database at all. Store them in a structured way on the HDD, and only keep a reference to it in your database (like the file path).[/quote]Storing true binary data in modern databases is perfectly fine as long as you actually use a BLOB or BINARY column type. And properly use ‘?’ substitution to push the binary content to the database.

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [February 3, 2017, 3:34pm UTC](https://forum.inductiveautomation.com/t/utf-8-script-error/12634/5 "2017-02-03T15:34:59Z")

</div>

What Phil said. I’ve stored literally millions of images and PDFs in databases and had zero problems. But I always store them as a blob.
