使用SQLite3(Ruby)的数据库没有插入

时间:2016-10-03 03:06:28

标签: sql ruby database sqlite sqlite3-ruby

我正在学习一本教程,试图学习将Ruby连接到数据库的基础知识。目前,我有一个程序,使用twilio接收短信,并根据你说的发回一条消息。我试图使用SQLite3将电话号码和消息的许多其他部分存储到数据库,但每当我运行代码时,数据库都没有条目。这里是。

require 'twilio-ruby'
require 'google_places'
require 'sinatra'
require 'dotenv'
require 'sqlite3'

begin
    db = SQLite3::Database.open "test.db"
    db.execute "CREATE TABLE IF NOT EXISTS Entries(Id INTEGER PRIMARY KEY AUTOINCREMENT, Searched TEXT, Place TEXT, Number BLOB)"
    begin
db.execute "CREATE TABLE IF NOT EXISTS Entries(Id INTEGER PRIMARY KEY AUTOINCREMENT, Searched TEXT, Place TEXT, Number BLOB)"
db.execute("INSERT INTO Entries (Searched, Place, Number) VALUES(?,?,?)", @incoming, @best_place_fmt, @phonenumber)
rescue SQLite3::Exception => e 
    puts "Exception occurred"
    puts e
#ensure
#    stm.close if stm
#   db.close if db
end

所有twilio函数都有效,并且收到消息并发送消息,但没有数据库命令实际编辑该文件。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

当我执行与数据库相关的代码时,我没有收到错误,数据库已填满。

如果我在此代码中添加select,请执行以下操作:

require 'sqlite3'

begin
    db = SQLite3::Database.open "test.db"
    db.execute "CREATE TABLE IF NOT EXISTS Entries(Id INTEGER PRIMARY KEY AUTOINCREMENT, Searched TEXT, Place TEXT, Number BLOB)"
    db.execute "INSERT INTO Entries(Searched) VALUES ('Is')"
    db.execute "INSERT INTO Entries(Place) VALUES ('This')"
    db.execute "INSERT INTO Entries(Number) VALUES ('Working')"
    (db.execute "Select * from Entries").each{|dataset|
      p dataset
    }
rescue SQLite3::Exception => e 
    puts "Exception occurred"
    puts e
#ensure
#    stm.close if stm
#   db.close if db
end

然后我得到:

[1, "Is", nil, nil]
[2, nil, "This", nil]
[3, nil, nil, "Working"]

你是怎么看到数据库中没有条目的?

您确定选中了正确的test.db吗?您可以使用p Dir.pwd检查实际目录。

修改问题后的示例:

如果我使用insert-command工作,那么当变量具有值

require 'sqlite3'
File.delete('test.db')  #Delete previous result and start with initial example.
@incoming = 'in'
@best_place_fmt = 'fm'
@phonenumber = 123456789
begin
    db = SQLite3::Database.open "test.db"
    db.execute "CREATE TABLE IF NOT EXISTS Entries(Id INTEGER PRIMARY KEY AUTOINCREMENT, Searched TEXT, Place TEXT, Number BLOB)"
    db.execute("INSERT INTO Entries (Searched, Place, Number) VALUES(?,?,?)", @incoming, @best_place_fmt, @phonenumber)
    (db.execute "Select * from Entries").each{|dataset|
      p dataset
    }
rescue SQLite3::Exception => e 
    puts "Exception occurred"
    puts e
#ensure
#    stm.close if stm
#   db.close if db

我的结果:

 [1, "in", "fm", 123456789]

我在评论中询问您是否对ORM的使用情况进行了评论。只是为了给你留下一个续集的例子的印象:

@incoming = 'in'
@best_place_fmt = 'fm'
@phonenumber = 123456789

require 'sequel'
db = Sequel.sqlite("test.db") #Here you could use also another DB
db.tables.each{|table| db.drop_table(table)}
db.create_table :entries2 do
  primary_key :id
  field :searched, :type => :text
  field :place, :type => :text
  field :number, :type => :blob
end
db[:entries2].insert(nil,@incoming, @best_place_fmt, @phonenumber)
puts db[:entries2].all #{:id=>1, :searched=>"in", :place=>"fm", :number=>"123456789"}