Erik Åsland
Erik Åsland

Reputation: 9782

SQlite error: "no such table "

I am currently building a simple bank program in Ruby. I have set it up MVC style. I am getting the following error message.

/home/devil/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `initialize': no such table: managers (SQLite3::SQLException)
from /home/devil/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `new'
from /home/devil/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:91:in `prepare'
from /home/devil/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.10/lib/sqlite3/database.rb:134:in `execute'
from /home/devil/Desktop/RubyBank-manager_model/model.rb:19:in `initialize_database'
from /home/devil/Desktop/RubyBank-manager_model/model.rb:7:in `initialize'
from controller.rb:8:in `new'
from controller.rb:8:in `initialize'
from controller.rb:124:in `new'
from controller.rb:124:in `<main>'

Earlier in the day the program was working fine. I changed a local variable's name in a controller method and it started to throw me the above error.

Things I have tried that have failed:

SEE MY GITHUB FOR PROGRAM CODE: https://github.com/erikasland/RubyBank/tree/add_manager

Upvotes: 1

Views: 1847

Answers (1)

Sculper
Sculper

Reputation: 755

It looks like the problem was created in this commit, on line 19 of model.rb. You're attempting to check if a table exists by selecting from it:

managers_exist = db.execute("SELECT * FROM managers").length > 0

This can be solved by querying against the schema, rather than the table - you've successfully done this with your accounts table:

accounts_table_exists = db.execute("SELECT 1 FROM sqlite_master WHERE 
      type='table' AND name= ?", "accounts").length > 0

Notice that you're selecting from sqlite_master, not accounts. What you want is something like this:

managers_exist= db.execute("SELECT 1 FROM sqlite_master WHERE 
      type='table' AND name= ?", "managers").length > 0

However, it seems to me that a CREATE TABLE IF NOT EXISTS approach might be the solution you're actually looking for.

Upvotes: 1

Related Questions