Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 12, 2021
1 parent ed07650 commit 217a4c8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
51 changes: 51 additions & 0 deletions crystal/examples/sqlite/all.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "sqlite3"
db_file = "data.db"

create_db(db_file)
insert_data(db_file, "Foo", true, 19)

def create_db(db_file)
if File.exists?(db_file)
return
end
begin
DB.open "sqlite3://#{db_file}" do |db|
db.exec "CREATE TABLE information (
id INTEGER PRIMARY KEY,
name TEXT,
isit BOOLEAN,
number INTEGER
)"
end
rescue err
puts "Exception #{err}"
end
end

def insert_data(db_file, name, isit, number)
args = [name, isit, number]
begin
DB.open "sqlite3://#{db_file}" do |db|
db.exec "INSERT INTO information
(name, isit, number)
VALUES (?, ?, ?)", args: args
end
rescue err
puts "Exception #{err}"
end
end

def get_max
return db.scalar "SELECT max(number) FROM information"
end

def get_data(db_file)
DB.open "sqlite3://#{db_file}" do |db|
db.exec "SELECT * FROM information"
end
rescue err
puts "Exception #{err}"
end



32 changes: 32 additions & 0 deletions crystal/examples/sqlite/in_memory.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "sqlite3"

DB.open "sqlite3://%3Amemory%3A" do |db|
db.exec "CREATE TABLE data (
id INTEGER PRIMARY KEY,
name TEXT,
yesno BOOLEAN,
number INTEGER,
start DATETIME
)"
db.exec "INSERT INTO data (name, yesno, number, start) VALUES (?, ?, ?, ?)",
"Foo", true, 42, Time.utc
name = db.scalar "SELECT name FROM data"
puts name
puts typeof(name)
puts name.to_s + " and Bar"

number = db.scalar "SELECT number FROM data"
puts number
puts typeof(number)
puts number.to_s.to_i + 1

db.exec "INSERT INTO data (name, yesno, number, start) VALUES (?, ?, ?, ?)",
"Bar", false, 23, Time.utc
name = db.scalar "SELECT name FROM data"
puts name

count = db.scalar "SELECT COUNT(*) FROM data"
puts count
puts typeof(count)

end
21 changes: 16 additions & 5 deletions crystal/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{id: sqlite}

## Try SQLite
{id: try-sqlite}
{id: sqlite-try}
{i: SQLite}

* You need to install the development package for libsqlite3
Expand All @@ -16,29 +16,40 @@ sudo apt-get install libsqlite3-dev


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

![](examples/sqlite/multi_counter_sqlite.cr)

Unhandled exception issue: https://github.com/crystal-lang/crystal-sqlite3/issues/52

## SQLite last_id last_insert_id
{id: last-insert-id}
{id: sqlite-last-insert-id}
{i: last_id}
{i: last_insert_id}

![](examples/sqlite/last_id.cr)

## SQLite UPDATE row_affected
{id: update-row-affected}
{id: sqlite-update-row-affected}

![](examples/sqlite/rows_affected.cr)

## SQLite exception handling (during INSERT)
{id: exception-handling}
{id: sqlite-exception-handling}

* The internal exception handling should be enough, but apparently it is not
* See [this](https://github.com/crystal-lang/crystal-sqlite3/issues/52) report

![](examples/sqlite/insert_exception.cr)

## SQLite all
{id: sqlite-all}

![](examples/sqlite/all.cr)

## SQLite in memory
{id: sqlite-in-memory}

![](examples/sqlite/in_memory.cr)

0 comments on commit 217a4c8

Please sign in to comment.