前提・実現したいこと
Ruby、SQLite、Sequelを使用しています。
データベースに情報を書き込むプログラムを作ろうとしていますが、あるはずのテーブルがないといわれます。
Rubyのプログラムを2つに分けてこれを実行しようとしていますが、1つにしてもやはり、ダメでした(下記に記載)。
なぜうまくいかないのか、わかる人がいればよろしくお願い申し上げます。
発生している問題・エラーメッセージ
user@usernoMacBook-Air ruby % ruby test.rb /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': SQLite3::SQLException: no such table: article (Sequel::DatabaseError) from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `new' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `prepare' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:193:in `execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block (2 levels) in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/logging.rb:38:in `log_connection_yield' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/connection_pool/threaded.rb:92:in `hold' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/connecting.rb:269:in `synchronize' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:193:in `_execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:159:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:1109:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:401:in `insert' from /Users/user/ruby/test2.rb:8:in `putdb' from test.rb:4:in `<main>' /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': no such table: article (SQLite3::SQLException) from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `new' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `prepare' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:193:in `execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block (2 levels) in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/logging.rb:38:in `log_connection_yield' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/connection_pool/threaded.rb:92:in `hold' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/connecting.rb:269:in `synchronize' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:193:in `_execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:159:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:1109:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:401:in `insert' from /Users/user/ruby/test2.rb:8:in `putdb' from test.rb:4:in `<main>'
該当のソースコード
test.rb
Ruby
1require './test2.rb' 2 3putdb(20211028111, "SQLiteについて", "https://www.google.com/")
test2.rb
Ruby
1require 'sqlite3' 2require 'sequel' 3 4def putdb(id, midashi, url) 5 6 db = Sequel.connect('sqlite:./test.db') 7 db[:article].insert(fugou: id, midashi: midashi, url: url) 8end
test.sql
SQLite
1CREATE TABLE IF NOT EXISTS article( 2 fugou INTEGER PRIMARY KEY, -- id 一意の値 3 midashi TEXT NOT NULL, -- 見出し 文字列 4 url TEXT UNIQUE NOT NULL -- URL 文字列 5); 6SELECT * FROM article; 7.tables 8.schema
試したこと
articleテーブルが存在している証明
user@usernoMacBook-Air ruby % sqlite3 test.db < test.sql article CREATE TABLE article( fugou INTEGER PRIMARY KEY, -- id 一意の値 midashi TEXT NOT NULL, -- 見出し 文字列 url TEXT UNIQUE NOT NULL -- URL 文字列 );
test3.rb(test.rbとtest2.rbを統合したもの)
Ruby
1#! /usr/bin/env ruby 2 3db = Sequel.connect('sqlite:./test.db') 4 5db[:article].insert(20211028111, "SQLiteについて", "https://www.google.com/") 6# putdb(20211028111, "SQLiteについて", "https://www.google.com/")
user@usernoMacBook-Air ruby % ruby test3.rb /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': SQLite3::SQLException: no such table: article (Sequel::DatabaseError) from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `new' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `prepare' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:193:in `execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block (2 levels) in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/logging.rb:38:in `log_connection_yield' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/connection_pool/threaded.rb:92:in `hold' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/connecting.rb:269:in `synchronize' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:193:in `_execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:159:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:1109:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:401:in `insert' from test3.rb:6:in `<main>' /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `initialize': no such table: article (SQLite3::SQLException) from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `new' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:147:in `prepare' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:193:in `execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block (2 levels) in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/logging.rb:38:in `log_connection_yield' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:202:in `block in _execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/connection_pool/threaded.rb:92:in `hold' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/database/connecting.rb:269:in `synchronize' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:193:in `_execute' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/adapters/sqlite.rb:159:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:1109:in `execute_insert' from /Users/user/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/sequel-5.49.0/lib/sequel/dataset/actions.rb:401:in `insert' from test3.rb:6:in `<main>'
補足情報(FW/ツールのバージョンなど)
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
SQLite version 3.36.0 2021-06-18 18:58:49
sequel (5.49.0)
macOS 12.0.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/28 13:48