使用 SQLite3 (Ruby) 的数据库未插入
Database using SQLite3 (Ruby) is not inserting
我正在学习尝试学习连接 Ruby 数据库的基础知识的教程。目前,我有一个程序可以使用 twilio 接收短信,并根据您所说的内容发回一条消息。我正在尝试使用 SQLite3 将 phone 号码和消息的许多其他部分存储到数据库中,但是每当我 运行 代码时,数据库中都没有条目。这里是。
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 函数都可以工作,并且会收到消息并发送消息,但 none 的数据库命令实际上是在编辑文件。感谢您的帮助!
当我执行您的与数据库相关的代码时,没有出现任何错误并且数据库已填满。
如果我在这段代码中添加一个 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
.
检查实际目录
问题修改后的例子:
如果我接受你的插入命令,当变量有值时
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。只是为了给你一个印象下面的例子 sequel:
@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"}
我正在学习尝试学习连接 Ruby 数据库的基础知识的教程。目前,我有一个程序可以使用 twilio 接收短信,并根据您所说的内容发回一条消息。我正在尝试使用 SQLite3 将 phone 号码和消息的许多其他部分存储到数据库中,但是每当我 运行 代码时,数据库中都没有条目。这里是。
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 函数都可以工作,并且会收到消息并发送消息,但 none 的数据库命令实际上是在编辑文件。感谢您的帮助!
当我执行您的与数据库相关的代码时,没有出现任何错误并且数据库已填满。
如果我在这段代码中添加一个 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
.
问题修改后的例子:
如果我接受你的插入命令,当变量有值时
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。只是为了给你一个印象下面的例子 sequel:
@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"}