質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Q&A

解決済

1回答

1586閲覧

Ruby Sequelでのテーブルを作る際のエラー

Nozo

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

0グッド

0クリップ

投稿2017/07/15 08:33

編集2017/07/15 08:51

ご教授よろしくお願いします

###前提・実現したいこと
Yahooの形態素解析APIを叩いて、帰ってきた値をSqlite3にいれるスクリプトを書いています。
使用言語はRubyです

###発生している問題・エラーメッセージ
keitaiso.sqlite3ファイルを作る前はエラー吐かないのですが、ファイルを作るとエラーを吐きます

bundle exec ruby yahoo_db.rb /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `step': SQLite3::ConstraintException: UNIQUE constr aint failed: adjective.surface, adjective.reading (Sequel::UniqueConstraintViolation) from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108:in `block in each' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `loop' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:107:in `each' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `to_a' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:156:in `block in execute' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:95:in `prepare' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:197:in `block (2 levels) in _execute' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/database/logging.rb:45:in `log_connection_yield' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:197:in `block in _execute' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `block in synchronize' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/connection_pool/threaded.rb:107:in `hold' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `synchronize' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:188:in `_execute' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:154:in `execute_insert' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:1097:in `execute_insert' from /Users/nozomi/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:402:in `insert' from yahoo_db.rb:114:in `input_db' from yahoo_db.rb:142:in `<main>'

###該当のソースコード

Ruby

1require "sequel" 2require "sqlite3" 3DB = Sequel.sqlite('keitaiso.sqlite3') 4class Keitaiso 5 def initialize 6 # 形容詞 7 DB.create_table? :adjective do 8 primary_key :id 9 String :surface 10 String :reading 11 unique [:surface, :reading] 12 end 13 # 形容動詞 14 DB.create_table? :adjective_verb do 15 primary_key :id 16 String :surface 17 String :reading 18 unique [:surface, :reading] 19 end 20 # 感嘆詞 21 DB.create_table? :exclamations do 22 primary_key :id 23 String :surface 24 String :reading 25 unique [:surface, :reading] 26 end 27 # 副詞 28 DB.create_table? :adverb do 29 primary_key :id 30 String :surface 31 String :reading 32 unique [:surface, :reading] 33 end 34 # 連体詞 35 DB.create_table? :subjective do 36 primary_key :id 37 String :surface 38 String :reading 39 unique [:surface, :reading] 40 end 41 42 # 接続詞 43 DB.create_table? :conjunction do 44 primary_key :id 45 String :surface 46 String :reading 47 unique [:surface, :reading] 48 end 49 50 # 接頭辞 51 DB.create_table? :prefix do 52 primary_key :id 53 String :surface 54 String :reading 55 unique [:surface, :reading] 56 end 57 58 # 接尾辞 59 DB.create_table? :suffix do 60 primary_key :id 61 String :surface 62 String :reading 63 unique [:surface, :reading] 64 end 65 66 # 名詞 67 DB.create_table? :nouns do 68 primary_key :id 69 String :surface 70 String :reading 71 unique [:surface, :reading] 72 end 73 74 # 動詞 75 DB.create_table? :verb do 76 primary_key :id 77 String :surface 78 String :reading 79 unique [:surface, :reading] 80 end 81 82 # 助詞 83 DB.create_table? :particle do 84 primary_key :id 85 String :surface 86 String :reading 87 unique [:surface, :reading] 88 end 89 90 # 助動詞 91 DB.create_table? :auxiliary_verb do 92 primary_key :id 93 String :surface 94 String :reading 95 unique [:surface, :reading] 96 end 97 98 @adjective = DB[:adjective] 99 @adjective_verb = DB[:adjective_verb] 100 @exclamations = DB[:exclamations] 101 @adverb = DB[:adverb] 102 @subjective = DB[:subjective] 103 @conjunction = DB[:conjunction] 104 @prefix = DB[:prefix] 105 @suffix = DB[:suffix] 106 @nouns = DB[:nouns] 107 @verb = DB[:verb] 108 @particle = DB[:particle] 109 @auxiliary_verb = DB[:auxiliary_verb] 110 end 111 def input_db(t) 112 case t[0] 113 when "形容詞" 114 @adjective.insert(:surface => t[1], :reading => t[2]) 115 when "形容動詞" 116 @adjective_verb.insert(:surface => t[1], :reading => t[2]) 117 when "感動詞" 118 @exclamations.insert(:surface => t[1], :reading => t[2]) 119 when "副詞" 120 @adverb.insert(:surface => t[1], :reading => t[2]) 121 when "連体詞" 122 @subjective.insert(:surface => t[1], :reading => t[2]) 123 when "接続詞" 124 @conjunction.insert(:surface => t[1], :reading => t[2]) 125 when "接頭辞" 126 @prefix.insert(:surface => t[1], :reading => t[2]) 127 when "接尾辞" 128 @suffix.insert(:surface => t[1], :reading => t[2]) 129 when "名詞" 130 @nouns.insert(:surface => t[1], :reading => t[2]) 131 when "動詞" 132 @verb.insert(:surface => t[1], :reading => t[2]) 133 when "助詞" 134 @particle.insert(:surface => t[1], :reading => t[2]) 135 when "助動詞" 136 @auxiliary_verb.insert(:surface => t[1], :reading => t[2]) 137 end 138 end 139end 140 141keitaiso = Keitaiso.new() 142keitaiso.input_db(["形容詞", "可愛い", "かわいい"]) 143

###試したこと

Ruby

1DB.create_table? :adjective do 2 primary_key :id 3 String :surface 4 String :reading 5 unique [:surface, :reading] 6end

の部分でエラーを吐かれていることは確認しました。
ただ、
###補足情報(言語/FW/ツール等のバージョンなど)
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]

Gemfile

Ruby

1#frozen_string_literal: true 2source "https://rubygems.org" 3 4gem "twitter" 5gem "sequel" 6gem "sqlite3" 7

よろしくお願いします

追記

REPL 出た目したところ
同じ値をSqlite3に代入しようとしたところ、UNIQUEなためエラーが履かれています
今回やりたいのは:surface :reading両方がUNIQUEのときのものを排除したいのですが、よいやりかたはありませんか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

例外処理を書くことで解決しました

以下変更後のコードです

Ruby

1def input_db(t) 2 begin 3 case t[0] 4 when "形容詞" 5 @adjective.insert(:surface => t[1], :reading => t[2]) 6 when "形容動詞" 7 @adjective_verb.insert(:surface => t[1], :reading => t[2]) 8 when "感動詞" 9 @exclamations.insert(:surface => t[1], :reading => t[2]) 10 when "副詞" 11 @adverb.insert(:surface => t[1], :reading => t[2]) 12 when "連体詞" 13 @subjective.insert(:surface => t[1], :reading => t[2]) 14 when "接続詞" 15 @conjunction.insert(:surface => t[1], :reading => t[2]) 16 when "接頭辞" 17 @prefix.insert(:surface => t[1], :reading => t[2]) 18 when "接尾辞" 19 @suffix.insert(:surface => t[1], :reading => t[2]) 20 when "名詞" 21 @nouns.insert(:surface => t[1], :reading => t[2]) 22 when "動詞" 23 @verb.insert(:surface => t[1], :reading => t[2]) 24 when "助詞" 25 @particle.insert(:surface => t[1], :reading => t[2]) 26 when "助動詞" 27 @auxiliary_verb.insert(:surface => t[1], :reading => t[2]) 28 end 29 rescue Sequel::UniqueConstraintViolation => e 30 end 31 end

投稿2017/07/15 10:57

Nozo

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問