Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read BLOB type as Bytes #42

Open
Blacksmoke16 opened this issue Jun 20, 2019 · 1 comment
Open

Can't read BLOB type as Bytes #42

Blacksmoke16 opened this issue Jun 20, 2019 · 1 comment

Comments

@Blacksmoke16
Copy link
Member

Blacksmoke16 commented Jun 20, 2019

A column with a BLOB type, reads as whatever the type of the value is, as opposed to Bytes, which would make more sense.

DB.open "sqlite3://./data.db" do |db|
  db.exec "create table contacts (name text, data BLOG)"
  db.exec "insert into contacts values (?, ?)", "John Doe", 30

  db.query "select name, data from contacts" do |rs|
    rs.each do
      puts "#{rs.read(String)} (#{rs.read(Bytes)})"
    end
  end
end

SQLite3::ResultSet#read returned a Int64. A Slice(UInt8) was expected. (Exception)

Changing the 30 to "foo"

SQLite3::ResultSet#read returned a String. A Slice(UInt8) was expected. (Exception)

@tenebrousedge
Copy link

Sqlite3 is dynamically typed, and the type is associated with the data, not the column. If you want to read bytes, then you need to write bytes:

require "db"
require "sqlite3"

DB.open "sqlite3://./data.db" do |db|
  db.exec "create table contacts (name text, data blob)"
  db.exec "insert into contacts values (?, ?)", "John Doe", Bytes.new(6,8)

  db.query "select name, data from contacts" do |rs|
    rs.each do
      puts "#{rs.read(String)} (#{rs.read(Bytes)})"
    end
  end
end
#=> John Doe (Bytes[8, 8, 8, 8, 8, 8])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants