I use the below code to insert records from a SQLite table to a remote MySQL table which has a primary key. I also use Erel 's php script exactly as shown on his post.
1. It does not insert all records. If I have 10 records in SQLite, it may insert 5 of them even without duplicates. How do I get it to insert all 10?
2. If there are already records in the MySQL table, exactly the same as the SQlite records, the INSERT crashes from the start and no records are transferred. How do I bypass the duplicate records in the MySQL and jump to the next record and avoid the crash?
I hope someone can help with this problem. Thank you very much.
1. It does not insert all records. If I have 10 records in SQLite, it may insert 5 of them even without duplicates. How do I get it to insert all 10?
2. If there are already records in the MySQL table, exactly the same as the SQlite records, the INSERT crashes from the start and no records are transferred. How do I bypass the duplicate records in the MySQL and jump to the next record and avoid the crash?
I hope someone can help with this problem. Thank you very much.
B4X:
Sub btnExportToMySQL_Click
Dim F(4), D(4) As String 'F is field names, D is data content
Dim i, j As Int
Cursor1.Position = 0
For j=0 To 4-1
F(j)=Cursor1.GetColumnName(j)
Next
For i=0 To Cursor1.RowCount-1
Cursor1.Position=i
For j=0 To 4-1
D(j)=Cursor1.GetString(F(j))
Next
ExecuteRemoteQuery("INSERT INTO mysqlremotetable (Field1, Field2, Field3, Field4" _
& "VALUES ('" &D(0)& "','" & D(1)& "','" & D(2) _
& "','" &D(3)& "')",i)
Next 'go to next SQLite table record
End Sub
Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
Dim req As HttpRequest
req.InitializePost2("http://www.website.com/phpfilename.php", Query.GetBytes("UTF8"))
hc.Execute(req, TaskId)
End Sub
Sub hc_ResponseError (RemoteResponse As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Log("Error: " & Reason & ", StatusCode: " & StatusCode)
Msgbox("Possible duplicate record, bad data or no internet." & CRLF & " Error: "& Reason & ", StatusCode: " & StatusCode,"Record NOT inserted".ToUpperCase)
End Sub
Sub hc_ResponseSuccess (RemoteResponse As HttpResponse, TaskId As Int)
Msgbox("Record was saved to remote server successfully.".ToUpperCase,"SUCCESS")
End Sub