title: Sqlite behaves oddly in frozen Python script
body: <p>Using sqlite in Python3.6 and py2exe in Windows 10.</p>
  <p>My python file which uses sqlite runs fine normally.
  But when I convert to exe with py2exe I get a strange error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;dsn2bom.py&quot;, line 1026, in &lt;module&gt;
  File &quot;dsn2bom.py&quot;, line 957, in doall
  File &quot;dsn2bom.py&quot;, line 705, in insertVariantBOM
  sqlite3.OperationalError: near &quot;(&quot;: syntax error`
  </code></pre>
  <p>The python code immediately prior to this is:</p>
  <pre><code>qstr = f'CREATE TABLE &quot;{varname}&quot; AS {sqlstr}'
  </code></pre>
  <p>where sqlstr is a big <code>SELECT</code> statement, shown at the end of this question.</p>
  <p>So the error is somewhere in the SELECT statement.  Fine I guess, but why is this different between the regular .py file and the version embedded in the .exe after running through py2exe?</p>
  <p>Most of the debugging techniques in the sqlite page are about interactive debugging from the sqlite command line, which is not applicable here.</p>
  <p>Full create table command.  I left all the comments in as this smells like some weird parsing error, character encoding issue, etc., that maybe happens with py2exe.  I don't think there is inherently anything wrong with the sql.</p>
  <pre><code>CREATE TABLE &quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; AS SELECT
         ROW_NUMBER() OVER (ORDER BY REFDES COLLATE natsort) Item,
         GROUP_CONCAT(REFDES) REFDES,
      CASE -- show PART_NUMBER if MPN available, else non-MPN-OPN, etc.
          WHEN CIS.CISMPN NOT NULL THEN CIS.PART_NUMBER
          --WHEN CIS.Description NOT NULL THEN 'Non-MPN Part Number'
          WHEN CLEANNLB.PART_NUMBER = 'DNP' THEN 'Do Not Populate'
          --ELSE CLEANNLB.PART_NUMBER --|| ' INVALID'
          ELSE ' INTERNAL ERROR'
      END PART_NUMBER,
      COUNT(CLEANNLB.PART_NUMBER) QTY,
      CASE
          WHEN CIS.CISMPN NOT NULL THEN CIS.Manufacturer
          --WHEN CIS.Description NOT NULL THEN CIS.Manufacturer
          WHEN CLEANNLB.PART_NUMBER = 'DNP' THEN 'Do Not Populate'
          --ELSE 'INVALID PART NUMBER'
          ELSE 'INTERNAL ERROR'
      END Manufacturer,
      CASE
          WHEN CIS.CISMPN NOT NULL THEN CIS.CISMPN
          --WHEN CIS.Description NOT NULL THEN CIS.CISMPN
          WHEN CLEANNLB.PART_NUMBER = 'DNP' THEN 'Do Not Populate'
          --ELSE 'INVALID PART NUMBER'
          ELSE 'INTERNAL ERROR'
      END &quot;Manufacturer PN&quot;,
      CASE
          WHEN CIS.CISMPN NOT NULL THEN CIS.Description
          --WHEN CIS.Description NOT NULL THEN CIS.Description
          WHEN CLEANNLB.PART_NUMBER = 'DNP' THEN 'Do Not Populate'
          --ELSE 'INVALID PART NUMBER'
          ELSE 'INTERNAL ERROR'
      END Description,
      CASE
          WHEN CIS.CISMPN NOT NULL THEN CIS.Value
          --WHEN CIS.Description NOT NULL THEN NULL
          WHEN CLEANNLB.PART_NUMBER = 'DNP' THEN 'Do Not Populate'
          --ELSE 'INVALID PART NUMBER'
          ELSE 'INTERNAL ERROR'
      END Value
      FROM
      (
      -- This is the main query.  Get all PART_NUMBERs that will appear
      -- in output, even if DNP
      -- Start with original bom
      SELECT NETLISTBOM.REFDES, NETLISTBOM.PART_NUMBER FROM NETLISTBOM
          EXCEPT -- Remove parts that will have its OPN replaced by variant
              SELECT REFDES, PART_NUMBER FROM NETLISTBOM
              JOIN VARIANTS USING(REFDES)
              WHERE VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; != 'DNP' AND
                    VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; != 'POPULATE'
          EXCEPT -- Remove parts that will be DNP from variant
              SELECT REFDES, PART_NUMBER FROM NETLISTBOM
              JOIN VARIANTS USING(REFDES)
              WHERE VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; = 'DNP'
          EXCEPT -- remove primitive OPNs that are DNI
              SELECT REFDES, PART_NUMBER FROM NETLISTBOM
              JOIN DNI_PRIMITIVES USING(REFDES)
          EXCEPT -- remove instance OPNs that are DNI
              SELECT REFDES, PART_NUMBER FROM NETLISTBOM
              JOIN DNI_INSTANCES USING(REFDES)
          UNION -- Add variant parts back in using variant OPN value instead
              SELECT REFDES, VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; FROM NETLISTBOM
              JOIN VARIANTS USING(REFDES)
              WHERE VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; != 'DNP' AND
                    VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; != 'POPULATE'
          UNION -- add DNI instances back in as either DNP, original OPN, or new OPN
                -- if variant for this entry is NULL, then entry is present in BOM output as DNP
                -- if variant for this entry is anything else, then entry is present in BOM output
              SELECT REFDES,
              CASE
                  WHEN VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot;='POPULATE' THEN NETLISTBOM.PART_NUMBER
                  WHEN VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot;='DNP' THEN 'DNP'
                  WHEN VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; IS NOT NULL THEN VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot;
                  ELSE 'DNP'
              END PART_NUMBER
              FROM NETLISTBOM
              LEFT JOIN VARIANTS USING(REFDES)
              JOIN DNI_INSTANCES USING(REFDES)
          UNION -- add DNI primitives back in if POPULATE
              SELECT REFDES, PART_NUMBER
              FROM NETLISTBOM
              JOIN VARIANTS USING(REFDES)
              JOIN DNI_PRIMITIVES USING(REFDES)
              WHERE VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; = 'POPULATE'
          UNION -- Add DNP parts from variant back in with 'DNP'
              SELECT REFDES, 'DNP' FROM NETLISTBOM
              JOIN VARIANTS USING(REFDES)
              WHERE VARIANTS.&quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; = 'DNP'
          UNION -- Add + parts
              SELECT SUBSTR(REFDES,2), &quot;845-02368-01 PCBA, LEVEL SHIFTER, PROX&quot; FROM VARIANTS
              WHERE VARIANTS.REFDES LIKE '+%'
       ) CLEANNLB
   LEFT JOIN CIS USING(PART_NUMBER)
   WHERE PART_NUMBER NOT NULL AND
         ((PART_NUMBER = 'DNP') OR
          (CIS.CISMPN NOT NULL AND CIS.Description NOT NULL))
   GROUP BY REFDES
  </code></pre>