0

I am working on a simple program where I need to open my Sqlite3 database by writing Ruby code but I am getting the following error on executing it: 'initialize': unable to open database file(Sqlite£:CantOpenException) from first.rb:4:in 'new' from first.rb:4:in ''

The code is here:

require "sqlite3"

# Open a database
db = SQLite3::Database.open "C:\Users\aroraku\Desktop\SQL\adaptive.db"


# Find a few rows
db.execute( "
Select count(uuid) as RegistredPeopleNo, strftime('%Y-%m',created_at) AS Month
from users
group by strftime('%Y-%m',created_at)
order by Month;" ) do |row|
  p row
end

Can someone please help me with this problem. Thanks in advance

1 Answer 1

1

I'd guess that your problem is that a \ inside a double quoted string is used to begin an escape sequence. For example:

> puts "C:\Users\aroraku\Desktop\SQL\adaptive.db"
C:UsersrorakuDesktopSQLdaptive.db

There were also a couple beeps from my terminal that I can't copy'n'paste that well.

You'll need to double your backslashes:

"C:\\Users\\aroraku\\Desktop\\SQL\\adaptive.db"

or use forward slashes instead:

"C:/Users/aroraku/Desktop/SQL/adaptive.db"

Windows has historically allowed either forward or back slashes in path names, I'm not sure if this is still true so the forward slash version may not work.

Single quotes (as noted by Andrew Marshall) are also an option:

'C:\Users\aroraku\Desktop\SQL\adaptive.db'

but you can still run into issues with certain odd filenames as a backslash still has meaning in some narrow cases (such as '\\' and '\'') inside single-quoted strings.

Sign up to request clarification or add additional context in comments.

2 Comments

Or use single quotes.
@AndrewMarshall True, but you still have to worry about some incredibly rare cases and Ruby people seem to be allergic to single quotes. I use single quotes unless I specifically need something that only double quotes provide but that seems to be very rare.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.