-
-
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
3 changed files
with
99 additions
and
5 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
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 | ||
|
||
|
||
|
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,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 |
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