#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs
import sqlite3
import types
conn = sqlite3.connect("test.db" , isolation_level=None)
cur = conn.cursor()
#テーブルの初期化
conn.execute("create table list(id integer primary key,name text)")
new_list = ['a','b','c','a'] #<= 最後の'a'が重複している。
for i in new_list:
sql = "select name from list where name LIKE '"+str(i)+"';"
result = conn.execute(sql).fetchone()
#types.NoneTypeが返ってくる。 =>入力データに同じ内容が無い
if type(conn.execute(sql).fetchone()) == types.NoneType:
sql = "insert into list(name) values('"+str(i)+"');"
conn.execute(sql)
sql = "select * from list;"
print conn.execute(sql).fetchall()
>>>[(1, 'a'), (2, 'b'), (3, 'c')]
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme