2013-07-26 57 views
0

我试图用Ruby访问基本的SQLite数据库,但不断收到一个奇怪的错误。宝石安装没有一个错误,我有适当的错误,但是当我尝试实际运行的代码,我得到这个错误:Ruby SQLite数据库初始化

/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>' 

计划

require 'sqlite3' 

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

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

任何想法,以什么可能导致此?

回答

1

尝试:

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';") 

详见上表SQLITE_MASTER这里:http://www.sqlite.org/faq.html

1

SQL命令.tabes应该做什么?

如果你使用一个有效的SQL,您可以使用db.execute

require 'sqlite3' 

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

如果你想获得你可能会选择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 
} 

但我会推荐一个数据库工具包,例如,续集:

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

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

puts DB.tables