diff --git a/lib/dbi/handles/database.rb b/lib/dbi/handles/database.rb
index afd3c98..bbf9782 100644
--- a/lib/dbi/handles/database.rb
+++ b/lib/dbi/handles/database.rb
@@ -54,6 +54,7 @@ module DBI
             sth = StatementHandle.new(@handle.prepare(stmt), false, true, @convert_types)
             # FIXME trace sth.trace(@trace_mode, @trace_output)
             sth.dbh = self
+            sth.stmt = stmt
             sth.raise_error = raise_error
 
             if block_given?
@@ -81,6 +82,7 @@ module DBI
             sth = StatementHandle.new(@handle.execute(stmt, *bindvars), true, true, @convert_types, true)
             # FIXME trace sth.trace(@trace_mode, @trace_output)
             sth.dbh = self
+            sth.stmt = stmt
             sth.raise_error = raise_error
 
             if block_given?
diff --git a/lib/dbi/handles/statement.rb b/lib/dbi/handles/statement.rb
index 8a96ce9..b1c49ea 100644
--- a/lib/dbi/handles/statement.rb
+++ b/lib/dbi/handles/statement.rb
@@ -12,6 +12,7 @@ module DBI
         include Enumerable
 
         attr_accessor :dbh
+        attr_accessor :stmt
         attr_accessor :raise_error
 
         def initialize(handle, fetchable=false, prepared=true, convert_types=true, executed=false)
@@ -71,7 +72,7 @@ module DBI
         #
         def bind_coltype(pos, type)
             sanity_check({:prepared => true, :executed => true})
-            
+
             coltypes = column_types
 
             if (pos - 1) < 1
@@ -105,7 +106,7 @@ module DBI
         #
         # If arguments are supplied, these are fed to #bind_param.
         def execute(*bindvars)
-            cancel     # cancel before 
+            cancel     # cancel before
             sanity_check({:prepared => true })
 
             if @convert_types
@@ -113,6 +114,9 @@ module DBI
             end
 
             @handle.bind_params(*bindvars)
+            # Shhh - we know something about our parent's internal structure
+            # that others don't.
+            dbh.instance_variable_set(:@last_statement, @stmt)
             @handle.execute
             @fetchable = true
             @executed = true
diff --git a/test/dbd/general/test_database.rb b/test/dbd/general/test_database.rb
index 0d2b616..c3cb835 100644
--- a/test/dbd/general/test_database.rb
+++ b/test/dbd/general/test_database.rb
@@ -8,9 +8,44 @@
         @sth = @dbh.execute("select * from names")
         @sth.finish
         assert_equal "select * from names", @dbh.last_statement
-         
+
         @dbh.do("select * from names")
         assert_equal "select * from names", @dbh.last_statement
+
+        # statement execution sets .last_statement
+        @dbh.prepare("SELECT age FROM names") do |sth|
+          assert_equal "SELECT age FROM names", @dbh.last_statement
+
+          @dbh.execute("select * from names")
+          assert_equal "select * from names", @dbh.last_statement
+
+          sth.execute()
+        end
+        assert_equal "SELECT age FROM names", @dbh.last_statement
+
+        sth_name = @dbh.prepare('SELECT name FROM names')
+        sth_age  = @dbh.prepare('SELECT age FROM names')
+
+        sth_name.execute()
+        assert_equal "SELECT name FROM names", @dbh.last_statement
+
+        sth_age.execute()
+        assert_equal "SELECT age FROM names", @dbh.last_statement
+    end
+
+    def test_last_statement_error
+        # bogus table
+        begin
+            @dbh.prepare("SELECT * FROM this_table_does_not_exist") { |sth|
+              sth.execute
+            }
+        rescue
+        end
+        assert_equal "SELECT * FROM this_table_does_not_exist", @dbh.last_statement
+
+        # missing parameter
+        @dbh.execute("SELECT age FROM names WHERE 1=?") rescue nil
+        assert_equal "SELECT age FROM names", @dbh.last_statement
     end
 
     def test_empty_query
