0

I'm trying to access a basic SQLite database with Ruby, but keep getting a weird error. The gems install without an error and I have the proper error, but when I try to actually run the code I get this error:

/home/--/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize': near ".": syntax error (SQLite3::SQLException)
from /home/mastelj/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new' 
from /home/mastelj/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database.¦rb:91:in `prepare'
from /home/mastelj/.rvm/gems/ruby-2.0.0-p195/gems/sqlite3-1.3.7/lib/sqlite3/database. rb:134:in `execute'
^G Get Hel^O WriteOu^R Read Fi^Y Prev Pa^K Cut Tex^C Cur Pos from to_sqlite.rb:5:in `<main>'

Program

require 'sqlite3'

db = SQLite3::Database.open('test.db')
rows = db.execute( ".tabes" )

for i in 0..rows.size-1
    puts rows[i]
end

Any idea as to what could cause this?

2 Answers 2

1

Try:

db = SQLite3::Database.open('test.db')
rows = db.execute( "SELECT * FROM sqlite_master WHERE type='table';" )
# If you want just the table names do:
rows = db.execute( "SELECT name FROM sqlite_master WHERE type='table';" )

See more on the sqlite_master table here: http://www.sqlite.org/faq.html.

Sign up to request clarification or add additional context in comments.

Comments

1

What should the SQL-command .tabes do?

If you use a valid SQL, you can use db.execute:

require 'sqlite3'

db = SQLite3::Database.open('test.db')
rows = db.execute( "CREATE TABLE [test] (  [test] CHAR);" )

If you want to get the list of tables you may select with sqlite_master.

require 'sqlite3'

db = SQLite3::Database.open('test.db')
db.execute( "CREATE TABLE [test] (  [test] CHAR);" )
rows = db.execute( "SELECT * FROM sqlite_master WHERE type='table';" )
rows.each{|tab|
  p tab
}

But I would recommend a database toolkit, e.g. Sequel:

require 'sequel'
DB = Sequel.sqlite('test.db')

DB.create_table( :test ){
  String :content
}

puts DB.tables

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.