0

我正在尝试使用 Ruby 访问基本的 SQLite 数据库,但不断收到一个奇怪的错误。gems 安装没有错误,我有正确的错误,但是当我尝试实际运行代码时,我得到了这个错误:

/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

关于可能导致这种情况的任何想法?

4

2 回答 2

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 。

于 2013-07-26T21:29:10.387 回答
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
}

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

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

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

puts DB.tables
于 2013-07-26T21:32:18.947 回答