-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,3 @@ else | |
puts "#{i}: #{arg}" | ||
} | ||
end | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "sqlite3" | ||
|
||
db_file = "counter.db" | ||
if ! File.exists?(db_file) | ||
DB.open "sqlite3://#{db_file}" do |db| | ||
db.exec "CREATE TABLE counters (name TEXT, count INTEGER, UNIQUE(name))" | ||
end | ||
end | ||
|
||
DB.open "sqlite3://#{db_file}" do |db| | ||
if ARGV.size == 1 | ||
name = ARGV[0] | ||
count = 0 | ||
db.query "SELECT count FROM counters WHERE name=?", name do |rs| | ||
rs.each do | ||
count = rs.read(Int32) | ||
end | ||
end | ||
|
||
count += 1 | ||
puts count | ||
|
||
if count == 1 | ||
db.exec "INSERT INTO counters VALUES (?, ?)", name, count | ||
else | ||
db.exec "UPDATE counters SET count=? WHERE name=?", count, name | ||
end | ||
else | ||
db.query "SELECT name, count FROM counters ORDER BY name DESC" do |rs| | ||
puts "#{rs.column_name(0)} (#{rs.column_name(1)})" | ||
rs.each do | ||
puts "#{rs.read(String)} (#{rs.read(Int32)})" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "sqlite3" | ||
|
||
# You need to install the development package for libsqlite3 | ||
# On Ubuntu | ||
# sudo apt-get install libsqlite3-dev | ||
|
||
|
||
# Based on the example in the documentation | ||
db_file = "data.db" | ||
if File.exists?(db_file) | ||
puts "File #{db_file} already exists. Aborting" | ||
exit(1) | ||
end | ||
DB.open "sqlite3://#{db_file}" do |db| | ||
db.exec "CREATE TABLE contacts (name TEXT, age INTEGER)" | ||
db.exec "INSERT INTO contacts VALUES (?, ?)", "John Doe", 30 | ||
|
||
args = [] of DB::Any | ||
args << "Sarah" | ||
args << 33 | ||
db.exec "INSERT INTO contacts VALUES (?, ?)", args: args | ||
|
||
puts "max age:" | ||
puts db.scalar "SELECT max(age) FROM contacts" # => 33 | ||
|
||
puts "contacts:" | ||
db.query "SELECT name, age FROM contacts ORDER BY age DESC" do |rs| | ||
puts "#{rs.column_name(0)} (#{rs.column_name(1)})" | ||
# => name (age) | ||
rs.each do | ||
puts "#{rs.read(String)} (#{rs.read(Int32)})" | ||
# => Sarah (33) | ||
# => John Doe (30) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters