2

please help! I am using sqlite3 in ipython notebook to create an SQL database. I think I have successfully created the database, but when I go to look at it I am receiving an encoding UTF8 error. Here is my code:

import sqlite3

conn=sqlite3.connect('example.db')
c=conn.cursor()
c.execute('''DROP TABLE PROFILE''')
c.execute('''CREATE TABLE PROFILE
        ( FIRSTNAME TEXT PRIMARY KEY unique   NOT NULL,
         LASTNAME        TEXT    NOT NULL,
         EMAILADDRESS    TEXT    NOT NULL,
         PASSWORD        TEXT    NOT NULL,
         CURRENTJOBTITLE    TEXT    NOT NULL,
         EDUCATION      TEXT    NOT NULL);''')
conn.close()


conn = sqlite3.connect('example.db')
c = conn.cursor()
conn.execute("INSERT INTO PROFILE (FIRSTNAME, LASTNAME, EMAILADDRESS, PASSWORD, CURRENTJOBTITLE, EDUCATION) \
  VALUES ('SALLYSUE','SUE','SALLYSUE@YAHOO.COM','ABC', 'STUDENT', 'GRAD')");
conn.commit()
conn.close()

This is the end of my Code. When I go to look at the file created 'example.db' this is where I see the below error:

Error! C:\Users\Lastname\CSE801\example.db is not UTF-8 encoded
Saving disabled.
See Console for more details

So I am not able to see the table I am creating in SQL.

I googled this and found that people said to put this in my code

import sys
reload(sys)
sys.setdefaultencoding()

Well once I do this and try to rerun my code the code produces nothing. It won't run the code at all. Does anyone have any suggestions? Thanks!

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
jessMSUgrad
  • 29
  • 1
  • 1
  • 5
  • Works for me if I remove the `DROP TABLE`. Are you creating the DB outside of Python? – Alastair McCormack Mar 11 '16 at 21:38
  • I think you're trying to open the database in Jupyter's built in text editor. That won't work - SQLite databases are binary files, and you'll need a special application to inspect them. – Thomas K Apr 22 '16 at 07:24

4 Answers4

6

I am a complete newbie in Python and faced the similar issue. But i soon realized i was trying to access a zip folder from Jupyter and hence the error!! Unzip and access the actual file was to be done in the first place.

This may sound very basic, but it might happen to new coders.

Akash
  • 63
  • 1
  • 5
1

The encoding of file itself is not utf-8.

You can change file character encoding by following steps in these links: http://mindspill.net/computing/linux-notes/determine-and-change-file-character-encoding/

Get encoding of a file in Windows

Best way to convert text files between character sets?

Community
  • 1
  • 1
Ferro Fang
  • 113
  • 1
  • 7
1

This may be a known Jupyter issue, specifically: https://github.com/jupyterhub/jupyterhub/issues/1572

I experienced the same problem with ReportLab. I created a PDF file, and tried to open it from within the Jupyter web interface (on the "tree" page). I received much the same error message.

The first thing to do is verify outside of Jupyter that the file was created successfully.

The only thing I can add to the documentation on the GitHub issue is that although the output from using "view" in the URL instead of "edit" may look blank, view source will show that Jupyter served a correctly-formed HTML page with a reference to the correctly-formed file, but that something is preventing the file from getting to a PDF renderer (or whatever you need for your DB file).

I am still working on the complete solution, which involves some settings for the Jupyter server that we haven't figured out yet. I'll come back and edit this post when the solution is found. However, be assured that UTF-8 encoding is not the root cause here, at least as far as the file itself is concerned.

user5920660
  • 129
  • 5
0

jupyter labextension install jupyterlab-spreadsheet

Rahul Verma
  • 2,988
  • 2
  • 11
  • 26