
SQLite3::exec 메서드 사용 시 에러 발생 처리는 다음과 같습니다.
1. SQL 명령이 잘못된 경우, SQLite3::Database 클래스는 SQLite3::Exception 예외를 발생시킵니다. 이 예외를 catch하여 에러를 처리할 수 있습니다.
2. 예를 들어, SQL 명령이 잘못된 경우, 다음과 같이 에러를 처리할 수 있습니다.
#hostingforum.kr
ruby
begin
db = SQLite3::Database.new('example.db')
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)')
rescue SQLite3::Exception => e
puts "에러 메시지: #{e.message}"
end
3. 에러 메시지를 출력할 때는 SQLite3::Exception 예외의 message 속성을 사용할 수 있습니다. 예를 들어, 다음과 같이 에러 메시지를 출력할 수 있습니다.
#hostingforum.kr
ruby
begin
db = SQLite3::Database.new('example.db')
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT')
rescue SQLite3::Exception => e
puts "에러 메시지: #{e.message}"
end
4. 에러 메시지를 자세히 출력하고 싶다면 SQLite3::Exception 예외의 backtrace 속성을 사용할 수 있습니다. 예를 들어, 다음과 같이 에러 메시지를 자세히 출력할 수 있습니다.
#hostingforum.kr
ruby
begin
db = SQLite3::Database.new('example.db')
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT')
rescue SQLite3::Exception => e
puts "에러 메시지: #{e.message}"
puts "에러 스택 트레이스: #{e.backtrace.join("n")}"
end
5. SQLite3::Exception 예외를 catch하여 에러를 처리하는 대신, SQLite3::Database 클래스의 exec 메서드에 블록을 전달하여 에러를 처리할 수 있습니다. 예를 들어, 다음과 같이 에러를 처리할 수 있습니다.
#hostingforum.kr
ruby
db = SQLite3::Database.new('example.db')
db.exec('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT') do |result|
if result.nil?
puts "에러 메시지: #{db.errmsg}"
else
puts "SQL 명령이 성공적으로 실행되었습니다."
end
end
2025-05-30 04:25