<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno mar 7 apr 2020 alle ore 16:22 T Lee Davidson <<a href="mailto:t.lee.davidson@gmail.com">t.lee.davidson@gmail.com</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 4/7/20 8:10 AM, Benoît Minisini wrote:<br>
> Le 07/04/2020 à 11:27, Yeshua Rodas a écrit :<br>
>> Hello people.<br>
>><br>
>> I'm having an issue working with SQLite.<br>
>><br>
>> I have a particular function for insert a batch of data and return the number of affected rows:<br>
>><br>
[snip]>><br>
> <br>
> Only SELECT queries return a Result object. You don't have access to the number of inserted rows at the moment.<br>
> <br>
<br>
If you need the number of rows inserted, one way might be to:<br>
1. Count the number of rows in the table before the operation, then<br>
2. Count the number of rows in the table after the operation, and<br>
3. Return the difference.<br></blockquote><div><br></div><div>Maybe trying this...<br>Create e new Database application and add a module named MBase in which you paste this code:<br>-------------------------------------------<br>' Gambas module file<br><br>Public hConn As New Connection<br>Public $nTotalNumber As Long<br>Public $nNewNumber As Integer<br>Private $nRecords As Integer<br>Private $sNameDB As String = "Test.db"<br>Private $sPathDB As String = "/tmp"<br><br>Public Sub CheckDB() As Boolean<br><br> If Not Exist($sPathDB) Then Return<br> With hConn<br> .Close()<br> .Type = "sqlite3"<br> .Host = $sPathDB<br> .Open()<br> If .Databases.Exist($sNameDB) Then .Databases.Remove($sNameDB)<br> $nRecords = 99<br> .Databases.Add($sNameDB)<br> .Close()<br> .Name = $sNameDB<br> .Open()<br> MakeTable()<br> InserBasicRecords()<br> Print ("#Database successfully created!")<br> .Close()<br> End With<br>Catch<br> Message.Error(("Fatal error in ") & Error.Where & ("\nError number: ") & Error.Code & ("\nCause: ") & Error.Text, "OK")<br> CloseDB()<br> Return True<br><br>End<br><br>Public Sub OpenDB()<br><br> With hConn<br> .Close()<br> .Type = "sqlite3"<br> .Host = $sPathDB<br> .Name = $sNameDB<br> .Open()<br> End With<br>Catch<br> CloseDB()<br> Print Error.Text, Error.Code<br><br>End<br><br>Public Sub CloseDB()<br><br> hConn.Close<br><br>End<br><br>Private Sub MakeTable()<br><br> Dim hTable As Table = hConn.Tables.Add("tuser")<br><br> hTable.Fields.Add("uskey", db.Serial)<br> hTable.Fields.Add("usnam", db.String)<br> hTable.Fields.Add("ussur", db.String)<br> hTable.Fields.Add("usdat", db.Date)<br> hTable.PrimaryKey = ["uskey"]<br> hTable.Update<br>Catch<br> CloseDB()<br> Print Error.Text, Error.Code<br><br>End<br><br>Private Sub InserBasicRecords()<br><br> Dim nCasual, i, y, m, d As Integer<br> Dim sName, sSurname As String<br> Dim aFirstName As String[] = ["Binah", "Hanna", "Selassie", "Okpara", "Agustina", "Clema", "Coyan", "Eusebio", "Diego", "Alisha", "Daphne", "Wiley", "Valentine", "Lidwina", "Roxana", "Edmund", "Hildeger", "Adrianne", "Janette", "Quentin", "Sylvain", "Francesco", "Marco", "Aldina", "Ornella", "Carmencita", "Galena", "Florentino", "Rodrigo", "Benoit", "Mario"]<br> Dim aLastName As String[] = ["Smith", "Johnson", "Williams", "Brown", "Martin", "Bernard", "Dubois", "Thomas", "Robert", "Richard", "Müller", "Schmidt", "Schneider", "Fischer", "Weber", "Meyer", "Gunnarsson", "García", "González", "Rodríguez", "Fernández", "López", "Martínez", "Jensen", "Nielsen", "Cohen", "Friedman", "Rossi", "Bianchi", "Minisini", "Verdone"]<br> Dim hDate As Date<br> Dim hResult As Result<br><br> Randomize<br> hConn.Begin()<br> For i = 0 To $nRecords<br> nCasual = Rand(0, 30)<br> sName = aFirstName[nCasual]<br> nCasual = Rand(0, 30)<br> sSurname = aLastName[nCasual]<br> y = Rand(1959, 2000)<br> m = Rand(1, 12)<br> d = Rand(1, 28)<br> hDate = Date(y, m, d)<br> hResult = hConn.Create("tuser")<br> hResult!usnam = sName<br> hResult!ussur = sSurname<br> hResult!usdat = hDate<br> hResult.Update<br> Next<br> hConn.Commit()<br> $nTotalNumber = ReturnNumber()<br>Catch<br> CloseDB()<br> Print Error.Text, Error.Code<br><br>End<br><br>Public Sub InsertNewRecords(sName As String, sSurname As String, hDate As Date)<br><br> Dim hResult As Result<br> Dim iOldTotalNumber As Long = $nTotalNumber<br><br> hResult = hConn.Create("tuser")<br> hResult!usnam = sName<br> hResult!ussur = sSurname<br> hResult!usdat = hDate<br> hResult.Update<br> If ReturnNumber() Then<br> $nTotalNumber = ReturnNumber()<br> $nNewNumber += $nTotalNumber - iOldTotalNumber ' ;-) here you can also just count the steps<br> Endif<br><br>End<br><br>Private Function ReturnNumber() As Long<br><br> Dim hResult As Result<br> Dim i As Long<br><br><br> hResult = hConn.Exec("SELECT MAX( uskey ) FROM tuser;")<br> If hResult.Available Then<br> If Not IsNull(hResult[0]) Then<br> i = hResult[0]<br> Return i<br> Endif<br> Endif<br> Return Null<br><br>End<br>-----------------------------------------------------<br>This is the FMain.class code:<br><br>' Gambas class file<br><br><br>Public hButton As Button<br><br>Public Sub Form_Open()<br><br> Dim hButton As New Button(Me) As "Button1"<br> <br> With hButton<br> .X = 120<br> .Y = 120<br> .H = 100<br> .W = 200<br> .Text = "Click me"<br> End With<br> MBase.CheckDB()<br><br>End<br><br>Public Sub Button1_Click()<br><br> ' Inserts three new records<br> MBase.OpenDB()<br> MBase.hConn.Begin()<br> For i As Integer = 0 To 2<br> MBase.InsertNewRecords("Mame_" & CStr(i + 1), "Surname_" & CStr(i + 1), Date(Now))<br> Next<br> MBase.hConn.Commit()<br> ' Writes record numbers<br> Print "The total records are "; MBase.$nTotalNumber; ", of which new "; MBase.$nNewNumber<br>Catch<br> MBase.hConn.Rollback()<br><br>End<br>--------------------------------------<br><br>Regards<br>Gianluigi</div></div></div>