| 1 |
<!DOCTYPE html> |
| 2 |
<html><head> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 4 |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> |
| 5 |
<link href="../sqlite.css" rel="stylesheet"> |
| 6 |
<title>Last Insert Rowid</title> |
| 7 |
<!-- path=../ --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="../index.html"> |
| 12 |
<img class="logo" src="../images/sqlite370_banner.svg" alt="SQLite" border="0"> |
| 13 |
</a> |
| 14 |
<div><!-- IE hack to prevent disappearing logo --></div> |
| 15 |
<div class="tagline desktoponly"> |
| 16 |
Small. Fast. Reliable.<br>Choose any three. |
| 17 |
</div> |
| 18 |
<div class="menu mainmenu"> |
| 19 |
<ul> |
| 20 |
<li><a href="../index.html">Home</a> |
| 21 |
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a> |
| 22 |
<li class='wideonly'><a href='../about.html'>About</a> |
| 23 |
<li class='desktoponly'><a href="../docs.html">Documentation</a> |
| 24 |
<li class='desktoponly'><a href="../download.html">Download</a> |
| 25 |
<li class='wideonly'><a href='../copyright.html'>License</a> |
| 26 |
<li class='desktoponly'><a href="../support.html">Support</a> |
| 27 |
<li class='desktoponly'><a href="../prosupport.html">Purchase</a> |
| 28 |
<li class='search' id='search_menubutton'> |
| 29 |
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a> |
| 30 |
</ul> |
| 31 |
</div> |
| 32 |
<div class="menu submenu" id="submenu"> |
| 33 |
<ul> |
| 34 |
<li><a href='../about.html'>About</a> |
| 35 |
<li><a href='../docs.html'>Documentation</a> |
| 36 |
<li><a href='../download.html'>Download</a> |
| 37 |
<li><a href='../support.html'>Support</a> |
| 38 |
<li><a href='../prosupport.html'>Purchase</a> |
| 39 |
</ul> |
| 40 |
</div> |
| 41 |
<div class="searchmenu" id="searchmenu"> |
| 42 |
<form method="GET" action="../search"> |
| 43 |
<select name="s" id="searchtype"> |
| 44 |
<option value="d">Search Documentation</option> |
| 45 |
<option value="c">Search Changelog</option> |
| 46 |
</select> |
| 47 |
<input type="text" name="q" id="searchbox" value=""> |
| 48 |
<input type="submit" value="Go"> |
| 49 |
</form> |
| 50 |
</div> |
| 51 |
</div> |
| 52 |
<script> |
| 53 |
function toggle_div(nm) { |
| 54 |
var w = document.getElementById(nm); |
| 55 |
if( w.style.display=="block" ){ |
| 56 |
w.style.display = "none"; |
| 57 |
}else{ |
| 58 |
w.style.display = "block"; |
| 59 |
} |
| 60 |
} |
| 61 |
function toggle_search() { |
| 62 |
var w = document.getElementById("searchmenu"); |
| 63 |
if( w.style.display=="block" ){ |
| 64 |
w.style.display = "none"; |
| 65 |
} else { |
| 66 |
w.style.display = "block"; |
| 67 |
setTimeout(function(){ |
| 68 |
document.getElementById("searchbox").focus() |
| 69 |
}, 30); |
| 70 |
} |
| 71 |
} |
| 72 |
function div_off(nm){document.getElementById(nm).style.display="none";} |
| 73 |
window.onbeforeunload = function(e){div_off("submenu");} |
| 74 |
/* Disable the Search feature if we are not operating from CGI, since */ |
| 75 |
/* Search is accomplished using CGI and will not work without it. */ |
| 76 |
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){ |
| 77 |
document.getElementById("search_menubutton").style.display = "none"; |
| 78 |
} |
| 79 |
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */ |
| 80 |
function hideorshow(btn,obj){ |
| 81 |
var x = document.getElementById(obj); |
| 82 |
var b = document.getElementById(btn); |
| 83 |
if( x.style.display!='none' ){ |
| 84 |
x.style.display = 'none'; |
| 85 |
b.innerHTML='show'; |
| 86 |
}else{ |
| 87 |
x.style.display = ''; |
| 88 |
b.innerHTML='hide'; |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
var antiRobot = 0; |
| 93 |
function antiRobotGo(){ |
| 94 |
if( antiRobot!=3 ) return; |
| 95 |
antiRobot = 7; |
| 96 |
var j = document.getElementById("mtimelink"); |
| 97 |
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href"); |
| 98 |
} |
| 99 |
function antiRobotDefense(){ |
| 100 |
document.body.onmousedown=function(){ |
| 101 |
antiRobot |= 2; |
| 102 |
antiRobotGo(); |
| 103 |
document.body.onmousedown=null; |
| 104 |
} |
| 105 |
document.body.onmousemove=function(){ |
| 106 |
antiRobot |= 2; |
| 107 |
antiRobotGo(); |
| 108 |
document.body.onmousemove=null; |
| 109 |
} |
| 110 |
setTimeout(function(){ |
| 111 |
antiRobot |= 1; |
| 112 |
antiRobotGo(); |
| 113 |
}, 100) |
| 114 |
antiRobotGo(); |
| 115 |
} |
| 116 |
antiRobotDefense(); |
| 117 |
</script> |
| 118 |
<!-- keywords: sqlite3_last_insert_rowid --> |
| 119 |
<div class=nosearch> |
| 120 |
<a href="../c3ref/intro.html"><h2>SQLite C Interface</h2></a> |
| 121 |
<h2>Last Insert Rowid</h2> |
| 122 |
</div> |
| 123 |
<blockquote><pre> |
| 124 |
sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); |
| 125 |
</pre></blockquote> |
| 126 |
<p> |
| 127 |
Each entry in most SQLite tables (except for <a href="../withoutrowid.html">WITHOUT ROWID</a> tables) |
| 128 |
has a unique 64-bit signed |
| 129 |
integer key called the <a href="../lang_createtable.html#rowid">"rowid"</a>. The rowid is always available |
| 130 |
as an undeclared column named ROWID, OID, or _ROWID_ as long as those |
| 131 |
names are not also used by explicitly declared columns. If |
| 132 |
the table has a column of type <a href="../lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> then that column |
| 133 |
is another alias for the rowid.</p> |
| 134 |
|
| 135 |
<p>The sqlite3_last_insert_rowid(D) interface usually returns the <a href="../lang_createtable.html#rowid">rowid</a> of |
| 136 |
the most recent successful <a href="../lang_insert.html">INSERT</a> into a rowid table or <a href="../vtab.html">virtual table</a> |
| 137 |
on database connection D. Inserts into <a href="../withoutrowid.html">WITHOUT ROWID</a> tables are not |
| 138 |
recorded. If no successful <a href="../lang_insert.html">INSERT</a>s into rowid tables have ever occurred |
| 139 |
on the database connection D, then sqlite3_last_insert_rowid(D) returns |
| 140 |
zero.</p> |
| 141 |
|
| 142 |
<p>As well as being set automatically as rows are inserted into database |
| 143 |
tables, the value returned by this function may be set explicitly by |
| 144 |
<a href="../c3ref/set_last_insert_rowid.html">sqlite3_set_last_insert_rowid()</a></p> |
| 145 |
|
| 146 |
<p>Some virtual table implementations may INSERT rows into rowid tables as |
| 147 |
part of committing a transaction (e.g. to flush data accumulated in memory |
| 148 |
to disk). In this case subsequent calls to this function return the rowid |
| 149 |
associated with these internal INSERT operations, which leads to |
| 150 |
unintuitive results. Virtual table implementations that do write to rowid |
| 151 |
tables in this way can avoid this problem by restoring the original |
| 152 |
rowid value using <a href="../c3ref/set_last_insert_rowid.html">sqlite3_set_last_insert_rowid()</a> before returning |
| 153 |
control to the user.</p> |
| 154 |
|
| 155 |
<p>If an <a href="../lang_insert.html">INSERT</a> occurs within a trigger then this routine will |
| 156 |
return the <a href="../lang_createtable.html#rowid">rowid</a> of the inserted row as long as the trigger is |
| 157 |
running. Once the trigger program ends, the value returned |
| 158 |
by this routine reverts to what it was before the trigger was fired.</p> |
| 159 |
|
| 160 |
<p>An <a href="../lang_insert.html">INSERT</a> that fails due to a constraint violation is not a |
| 161 |
successful <a href="../lang_insert.html">INSERT</a> and does not change the value returned by this |
| 162 |
routine. Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, |
| 163 |
and INSERT OR ABORT make no changes to the return value of this |
| 164 |
routine when their insertion fails. When INSERT OR REPLACE |
| 165 |
encounters a constraint violation, it does not fail. The |
| 166 |
INSERT continues to completion after deleting rows that caused |
| 167 |
the constraint problem so INSERT OR REPLACE will always change |
| 168 |
the return value of this interface.</p> |
| 169 |
|
| 170 |
<p>For the purposes of this routine, an <a href="../lang_insert.html">INSERT</a> is considered to |
| 171 |
be successful even if it is subsequently rolled back.</p> |
| 172 |
|
| 173 |
<p>This function is accessible to SQL statements via the |
| 174 |
<a href="../lang_corefunc.html#last_insert_rowid">last_insert_rowid() SQL function</a>.</p> |
| 175 |
|
| 176 |
<p>If a separate thread performs a new <a href="../lang_insert.html">INSERT</a> on the same |
| 177 |
database connection while the <a href="../c3ref/last_insert_rowid.html">sqlite3_last_insert_rowid()</a> |
| 178 |
function is running and thus changes the last insert <a href="../lang_createtable.html#rowid">rowid</a>, |
| 179 |
then the value returned by <a href="../c3ref/last_insert_rowid.html">sqlite3_last_insert_rowid()</a> is |
| 180 |
unpredictable and might not equal either the old or the new |
| 181 |
last insert <a href="../lang_createtable.html#rowid">rowid</a>. |
| 182 |
</p><p>See also lists of |
| 183 |
<a href="../c3ref/objlist.html">Objects</a>, |
| 184 |
<a href="../c3ref/constlist.html">Constants</a>, and |
| 185 |
<a href="../c3ref/funclist.html">Functions</a>.</p> |
| 186 |
|