UNIQUE constraint failed: myt.hostName sqlite?

firstly I created my own db:

$ sqlite3 tdb
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

Then I am going to create a table:

sqlite> CREATE TABLE myt (hostName CHAR(50) PRIMARY KEY, content TEXT, checked CHAR(5));

Now I have the following script:

import sys
import requests
from bs4 import BeautifulSoup
import sqlite3 as db

headings=['title','h1','h2','h3','h4','h5','p']
hosts=['microsoft.com','stackoverflow.com','google.com','yahoo.com']
con=db.connect('tdb')
for hostName in hosts:
    cur=con.cursor()
    cur.execute('SELECT hostName FROM myt WHERE hostName=? AND checked="YES"',[hostName])
    data=cur.fetchall()
    try:
        if data[0][0]==hostName:
            continue
    except Exception, err:
        pass
    try:
        session=requests.Session()
        respons=session.get('http://%s'%hostName).content
    except KeyboardInterrupt:
        print
        sys.exit()

    try:
        soup=BeautifulSoup(respons,'lxml')
        for heading in headings:
            tags=soup.find_all(heading)
            for singleTag in tags:
                output=singleTag.text
                cur.execute('INSERT INTO myt (hostName,content,checked) VALUES (?,?,\'YES\')',[hostName,output])
        print '\n [+] Content is captured!'
    except Exception, err:
        print '\n [-] Error: %s'%err
        continue

But when I run the code for first time in my pc the following error occurs for each website:

UNIQUE constraint failed: myt.hostName

Solution 1:

Your hostName is a primary key and the problem comes when you try to insert hostname twice.

Error comes from this line:

cur.execute('INSERT INTO myt (hostName,content,checked) VALUES (?,?,\'YES\')',[hostName,output])  

The primary key must be unique