diff --git a/lib/db.php b/lib/db.php
index e70d66fc2ba2f8c6403aba9f8bf951ea1e1f1095..dd48c3294907cd3ddcbaa2f1ae11381778b4438e 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -344,7 +344,7 @@ class OC_DB {
 			$result = self::executeAudited('SELECT lastval() AS id');
 			$row = $result->fetchRow();
 			self::raiseExceptionOnError($row, 'fetching row for insertid failed');
-			return $row['id'];
+			return (int)$row['id'];
 		} else if( $type === 'mssql') {
 			if($table !== null) {
 				$prefix = OC_Config::getValue( "dbtableprefix", "oc_" );
@@ -368,7 +368,7 @@ class OC_DB {
 			$result = self::$connection->lastInsertId($table);
 		}
 		self::raiseExceptionOnError($result, 'insertid failed');
-		return $result;
+		return (int)$result;
 	}
 
 	/**
diff --git a/tests/lib/db.php b/tests/lib/db.php
index e817a2db5edc3dd9ca53ccdfb0d6654be639f244..79e05f30a1c129213560eb02172ece0f775358c1 100644
--- a/tests/lib/db.php
+++ b/tests/lib/db.php
@@ -71,7 +71,21 @@ class Test_DB extends PHPUnit_Framework_TestCase {
 		$result = $query->execute(array('uri_3'));
 		$this->assertTrue((bool)$result);
 	}
-
+	
+	public function testLastInsertId() {
+		$query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
+		$result1 = OC_DB::executeAudited($query, array('insertid 1','uri_1'));
+		$id1 = OC_DB::insertid('*PREFIX*'.$this->table2);
+		$this->assertInternalType('int', $id1);
+		
+		// we don't know the id we should expect, so insert another row
+		$result2 = OC_DB::executeAudited($query, array('insertid 2','uri_2'));
+		$id2 = OC_DB::insertid('*PREFIX*'.$this->table2);
+		// now we can check if the two ids are in correct order
+		$this->assertInternalType('int', $id2);
+		$this->assertGreaterThan($id1, $id2);
+	}
+	
 	public function testinsertIfNotExist() {
 		$categoryentries = array(
 				array('user' => 'test', 'type' => 'contact', 'category' => 'Family',    'expectedResult' => 1),