在ruby中删除sqlite3中的表

时间:2014-07-15 15:09:10

标签: ruby sqlite

我正在尝试从数据库中删除表。但是我不断收到错误。代码和错误消息如下。感谢您的帮助。表csv_07-15-2014_10-00存在于DB中。

require 'win32ole'
require 'sqlite3'
DB_NAME = 'excel.db'

db = SQLite3::Database.new(DB_NAME)

sqlQuery =  "SELECT * FROM sqlite_master  WHERE type = 'table'"
puts db.execute(sqlQuery )

sqlQuery =  "DROP TABLE csv_07-15-2014_10-00"

puts sqlQuery
puts db.execute(sqlQuery)

错误信息如下:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/sqlite3/databa
se.rb:91:in `initialize': near "-": syntax error (SQLite3::SQLException)
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:91:in `new'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:91:in `prepare'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:134:in `execute'
        from sqldroptable.rb:19:in `<main>'

PUT语句输出如下:

table
csv_07-15-2014_10-00
csv_07-15-2014_10-00
2
CREATE TABLE [csv_07-15-2014_10-00] 
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )

table
csv_07-14-2014_22-30
csv_07-14-2014_22-30
8
CREATE TABLE [csv_07-14-2014_22-30] 
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )

table
csv_07-14-2014_22-29
csv_07-14-2014_22-29
9
CREATE TABLE [csv_07-14-2014_22-29]         
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )

DROP TABLE csv_07-15-2014_10-00 FROM sqlite_maste

[R

2 个答案:

答案 0 :(得分:0)

你有SQL异常我认为转义查询中的问题试试这个:

sqlQuery =  "DROP TABLE \"csv_07-15-2014_10-00\""

答案 1 :(得分:0)

在SQL中,-是减法运算符,在DROP TABLE语句中没有意义。

如果您在标识符中有特殊字符,则必须引用它:

sqlQuery = "DROP TABLE [csv_07-15-2014_10-00]"

sqlQuery = "DROP TABLE \"csv_07-15-2014_10-00\""

sqlQuery = "DROP TABLE `csv_07-15-2014_10-00`"