Skip to content

Commit

Permalink
sqlite examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed May 21, 2021
1 parent 4c30221 commit 778e489
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ python/examples/pytest/coverage/.coverage
crystal/bin
crystal/lib
crystal/counter.yaml
crystal/counter.json
crystal/counter.db

1 change: 0 additions & 1 deletion crystal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* Classes


* Read YAML file in general
* Read shard.yml file in particular
* Web application development?
* Access to SQLite?
Expand Down
4 changes: 0 additions & 4 deletions crystal/examples/files/multi_counter_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ else
puts "#{i}: #{arg}"
}
end




36 changes: 36 additions & 0 deletions crystal/examples/multi_counter_sqlite.cr
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
36 changes: 36 additions & 0 deletions crystal/examples/try_sqlite.cr
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
12 changes: 11 additions & 1 deletion crystal/examples/use_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ data = File.open("examples/crystal.yml") do |file|
YAML.parse(file)
end
puts data
puts data["language"]
puts typeof(data) # YAML::Any
puts data["language"]
puts data["language"]["first_year"]
puts typeof(data["language"]["first_year"].to_s.to_i)
# [YAML::Any](https://crystal-lang.org/api/YAML/Any.html)
puts data.as_h.keys
data.as_h.keys.each {|main_key|
data[main_key].as_h.keys.each {|sub_key|
puts data[main_key][sub_key]
}
}
2 changes: 2 additions & 0 deletions crystal/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@
{i: from_yaml}
{i: to_yaml}

* [YAML](https://crystal-lang.org/api/YAML.html)

![](examples/files/multi_counter_yaml.cr)
12 changes: 12 additions & 0 deletions crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,19 @@ shards install
{id: yaml}
{i: YAML}

* [YAML](https://crystal-lang.org/api/YAML.html)

![](examples/crystal.yml)
![](examples/use_yaml.cr)

## SQLite
{id: sqlite}
{i: SQLite}

![](examples/try_sqlite.cr)

## Multi-counter with SQLite
{id: multi-counter-sqlite}
{i: SQLite}

![](examples/multi_counter_sqlite.cr)
8 changes: 8 additions & 0 deletions crystal/shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ shards:
git: https://github.com/crystal-ameba/ameba.git
version: 0.14.3

db:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.10.1

sqlite3:
git: https://github.com/crystal-lang/crystal-sqlite3.git
version: 0.18.0

2 changes: 2 additions & 0 deletions crystal/shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description: |
dependencies:
ameba:
github: crystal-ameba/ameba
sqlite3:
github: crystal-lang/crystal-sqlite3


#development_dependencies:
Expand Down

0 comments on commit 778e489

Please sign in to comment.