require 'sqlite3'
db = SQLite3::Database.new "data.db"
db.execute <<-SQL
create table foobar (
name varchar(30),
val int
);
SQL
#=> fill the database table with this stuff -->
#=> ["foo", 1]["bar", 2]["baz", 3]
command , -> delete *.db from file exclude pattern
execute 2 will return the field names as the first result of your query
db.execute2("select * from foobar")[0] #=> ["name", "val"]
query = db.prepare("select * from foobar")
query.execute do |result|
result.each{|row| print row}
end
#=> ["foo", 1]["bar", 2]["baz", 3]
Three different options, ?, ?n, :foo
query = db.prepare("select * from numbers where name = ?")
query.execute "foo" do |result|
result.each{|row| print row}
end
#=> ["foo", 1]
query = db.prepare("select * from numbers where name = ?2")
query.execute "foo", "bar" do |result|
result.each{|row| print row}
end
#=> ["bar", 2]
query = db.prepare("select * from numbers where name = :name")
query.execute "name" => "baz" do |result|
result.each{|row| print row}
end
#=> ["baz", 3]