title: Python and IBM DB2: UnicodeDecodeError
body: <p>I'm getting this error message</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 38: ordinal not in range(128)</p>
  </blockquote>
  
  <p>when I try to execute any sql query in Python, like this one:</p>
  
  <pre><code>&gt;&gt;&gt; import ibm_db
  &gt;&gt;&gt; conn = ibm_db.connect("sample","root","root")
  &gt;&gt;&gt; ibm_db.exec_immediate(conn, "select * from act")
  </code></pre>
  
  <p>I checked default encoding and it seems to be 'utf8':</p>
  
  <pre><code>&gt;&gt;&gt; import sys
  &gt;&gt;&gt; sys.getdefaultencoding()
  'utf-8'
  </code></pre>
  
  <p>I also know about <a href="https://www.ibm.com/developerworks/community/wikis/home?lang=ru#!/wiki/IBM%20i%20Technology%20Updates/page/Python" rel="nofollow">this</a> thread, where people are discussing quite a similar problem. One of the advices is:</p>
  
  <blockquote>
    <p>Have you applied the required database PTFs (SI57014 and SI57015 for 7.1 and SI57146 and SI57147 for 7.2)? They are included as a distreq, so they should have been in the order with your PTFs, but won't be automatically applied. </p>
  </blockquote>
  
  <p>However, I do not know what is database PTF and how to apply it. Need help.</p>
  
  <p>PS. I'm using Windows 10.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is how I get my error message:</p>
  
  <pre><code>&gt;&gt;&gt; print(ibm_db.stmt_errormsg())
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 38:    
  ordinal not in range(128)
  </code></pre>
  
  <p>But when I run the same query "select * from act" in <code>DB2 CLP</code>, then it's ok.
  And this is driver information, whcih I got running this code in Python:</p>
  
  <pre><code>if client:
      print("DRIVER_NAME: string(%d) \"%s\"" % (len(client.DRIVER_NAME), client.DRIVER_NAME))
      print("DRIVER_VER: string(%d) \"%s\"" % (len(client.DRIVER_VER), client.DRIVER_VER))
      print("DATA_SOURCE_NAME: string(%d) \"%s\"" % (len(client.DATA_SOURCE_NAME), client.DATA_SOURCE_NAME))
      print("DRIVER_ODBC_VER: string(%d) \"%s\"" % (len(client.DRIVER_ODBC_VER), client.DRIVER_ODBC_VER))
      print("ODBC_VER: string(%d) \"%s\"" % (len(client.ODBC_VER), client.ODBC_VER))
      print("ODBC_SQL_CONFORMANCE: string(%d) \"%s\"" % (len(client.ODBC_SQL_CONFORMANCE), client.ODBC_SQL_CONFORMANCE))
      print("APPL_CODEPAGE: int(%s)" % client.APPL_CODEPAGE)
      print("CONN_CODEPAGE: int(%s)" % client.CONN_CODEPAGE)
      ibm_db.close(conn)
  else:
      print("Error.")
  </code></pre>
  
  <p>it prints:</p>
  
  <pre><code>DRIVER_NAME: string(10) "DB2CLI.DLL"
  DRIVER_VER: string(10) "10.05.0007"
  DATA_SOURCE_NAME: string(6) "SAMPLE"
  DRIVER_ODBC_VER: string(5) "03.51"
  ODBC_VER: string(10) "03.01.0000"
  ODBC_SQL_CONFORMANCE: string(8) "EXTENDED"
  APPL_CODEPAGE: int(1251)
  CONN_CODEPAGE: int(1208)
  True
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I also tried this:</p>
  
  <pre><code>&gt;&gt;&gt; cnx = ibm_db.connect("sample","root","root")
  &gt;&gt;&gt; query = "select * from act"
  &gt;&gt;&gt; query.encode('ascii')
  b'select * from act'
  &gt;&gt;&gt; ibm_db.exec_immediate(cnx, query)
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  Exception
  &gt;&gt;&gt; print(ibm_db.stmt_errormsg())
  Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 38: 
  ordinal not in range(128)
  </code></pre>
  
  <p>As you can see, in this case I also get the very same error message.</p>
  
  <p><strong>SUMMARY</strong></p>
  
  <p>Below are all my attemts:</p>
  
  <pre><code>C:\Windows\system32&gt;chcp
  Active code page: 65001
  
  C:\Windows\system32&gt;python
  Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import ibm_db
  &gt;&gt;&gt; cnx = ibm_db.connect("sample","root","root")
  &gt;&gt;&gt; ibm_db.exec_immediate(cnx, "select * from act")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  Exception
  &gt;&gt;&gt; print(ibm_db.stmt_errormsg())
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc8 in position 38: ordinal not in range(128)
  &gt;&gt;&gt; ibm_db.exec_immediate(cnx, b"select * from act")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  Exception: statement must be a string or unicode
  &gt;&gt;&gt; query = "select * from act"
  &gt;&gt;&gt; query = query.encode()
  &gt;&gt;&gt; ibm_db.exec_immediate(cnx, query)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  Exception: statement must be a string or unicode
  &gt;&gt;&gt; ibm_db.exec_immediate(cnx, "select * from act").decode('cp-1251')
  Traceback (most recent call last):
   File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  Exception
  </code></pre>