函数参考


_SQLite_LastInsertRowID

返回在本次连接中数据库中最新插入的列 ID

#include <SQLite.au3>
_SQLite_LastInsertRowID ( [ $hDB ] )

参数

$hDB [可选参数] 打开的数据库,如为 -1, 则使用最后打开的数据库

返回值

成功: 返回列 ID
失败: 返回 0
@error: 1 - 错误调用 SQLite API 'sqlite3_last_insert_rowid'
2 - 调用被安全模式阻止

注意/说明

None.

相关

没有.

示例/演示


#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <Array.au3>

Local $aResult, $iRows, $iColumns, $iRval

_SQLite_Startup()
If @error Then
    MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")
    Exit -1
EndIf
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)
_SQLite_Open() ; 打开 :memory: 数据库
If @error Then
    MsgBox(16, "SQLite Error", "Can't Load Database!")
    Exit -1
EndIf

;示例表
;   名字        | 年龄
;   -----------------------
;   Alice       | 43
;   Bob         | 28
;   Cindy       | 21

; _SQLite_Exec() 和 _SQLite_Execute() 十分类似
If Not _SQLite_Exec(-1, "CREATE TEMP TABLE persons (Name, Age);") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Alice','43');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Bob','28');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Cindy','21');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())

; _SQLite_LastInsertRowID() 会告知我们 Cindy 所在的行
MsgBox(4096, "_SQLite_LastInsertRowID()", _SQLite_LastInsertRowID())

; 查询
$iRval = _SQLite_GetTable(-1, "SELECT * FROM persons;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then
;~  $aResult Looks Like this:
;~      [0]    = 8
;~      [1]    = Name
;~      [2]    = Age
;~      [3]    = Alice
;~      [4]    = 43
;~      [5]    = Bob
;~      [6]    = 28
;~      [7]    = Cindy
;~      [8]    = 21
    _ArrayDisplay($aResult, "Query Result")
Else
    MsgBox(16, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf

_SQLite_Close()
_SQLite_Shutdown()