I’ve been working on this for a while and can’t seem to come up with a solution.
I have an instance of Postgres running on another machine, and I have been completely unsuccessful writing any data to the database using Xojo.
I am using this prepared statement:
[code]db1.SQLExecute ( “BEGIN TRANSACTION” )
Dim ps1 As PostgreSQLPreparedStatement = _
db1.Prepare ( "INSERT INTO users ( name_first, name_last, email, hash1, salt2 ) VALUES ( $1, $2, $3, $4, $5 ); " )
ps1.SQLExecute ( first, last, email, hashstr1, saltstr2 )
db1.Commit
If db1.Error Then
MsgBox ( "DB 1 Error: " + db1.ErrorMessage )
End If
[/code]
I appear to have successfully dimensioned db1 as a PostgreSQLDatabase I’m not sure if I’m missing a key step or what is happening. I’m not sure if I need to include more code in order to be helpful or not.
@Dave S I am not getting any error messages on the SQL execute, it acts as if it has written, but when I check my database using Navicat there is nothing written. I actually check for a db1.connect when I connect to the server and it returns as true with no db1.ErrorMessage.
@Kem Tekinay I did seem to omit that from my example, that is the next line I have edited my original comment to show that line.
Okay, added a trap for an error between the prepared statement and the commit and got an error code 3 : “ERROR: invalid byte sequence for encoding “UTF8”: 0xe3 0x35 0x7a”
Not sure where to go from here, I’m still ridiculously new at this. Didn’t realize a commit would clear out errors.
@Jared Feder That didn’t seem to work. It appears there’s a text formatting that Xojo is trying to pass that Postgres doesn’t like. I imagine I brought some of this on myself by trying to make my first application to talk to Postgres write hash data and plain text instead of just plain text.
You could try adding db1.SQLExecute("SET NAMES 'utf8'") after starting the transaction to ensure that all of the communication with the database is occurring with utf8 encoding.
Also, make sure that the data that you are inserting into the database is utf8 encoded.
Are you getting any of the data from an external source, or is it all generated within your Xojo application?
If it’s coming from an external source, you may need to use either DefineEncoding if you know that the string is utf8 or ConvertEncoding if it is not.
Yep, exactly. SQL commands are “atomic” meaning they either fail or succeed. They won’t fail half way. If they could, wrapping them in a transaction would make sense.