user1932914
user1932914

Reputation: 186

Not able to open SQLite3 database using Ruby program

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

Upvotes: 0

Views: 342

Answers (1)

mu is too short
mu is too short

Reputation: 434635

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.

Upvotes: 1

Related Questions