stablekernel / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://www.dartdocs.org/documentation/postgres/latest
BSD 3-Clause "New" or "Revised" License
129 stars 32 forks source link

Trouble to get bytearray from database #126

Closed agavrel closed 4 years ago

agavrel commented 4 years ago

I am wondering what I didn't get right (I query a database with id as a string and a Bytea as the value associated with the id

// correctly filled but for obvious reasons I removed the values
Map credentials = {'PSQL_ADDRESS' : '',
    'PSQL_PORT' : , 
    'PSQL_USERNAME' : ''
    'PSQL_PASSWORD' : ''
    'PSQL_DBNAME' : ''
  };

Future<Uint8List> GetPdfFromPsqlDB(final String id) async {
    print('query bytes');

    var connection = new PostgreSQLConnection(
        credentials['PSQL_ADDRESS'], credentials['PSQL_PORT'],
        credentials['PSQL_DBNAME'], username: credentials['PSQL_USERNAME'],
        password: credentials['PSQL_PASSWORD']);
    await connection.open();

    var sqlQuery = "SELECT * FROM pdfs WHERE id = @id";
    List<List<dynamic>> results = await connection.query(sqlQuery, substitutionValues: {
        "id" : id
    });
    print(results[0][1]);
    return results[0][1];
}

results[0][1] is a unique result with bytearray value.

This function is not working and I did not figure out why. Are Uint8List and ByteA compatible? Is it because id is a String and not an int ?

Any help strongly appreciated!!

isoos commented 4 years ago

What's the SQL type of pdfs.id? What's the exception you are getting?

agavrel commented 4 years ago

pdfs.id is of type varchar(32) I do not get any exception, would you mind providing a code snippet with my code to get the Exception?

agavrel commented 4 years ago

I use

GetPdfFromPsqlDB(id).catchError((e) {
      print("Got error: ${e.error}");  

but nothing is displayed

isoos commented 4 years ago

I do not get any exception

In that case what's the issue? You don't get any result back?

agavrel commented 4 years ago

no there is definitely some error because the print statement that follow my query is not executed

I added

print('succesfully retrieved data');

right after the request

isoos commented 4 years ago

Just to test the connection is open, execute the following statement right after open:

final rs = await connection.query("SELECT 1");
print('got: $rs');

also add a small, 5-seconds timeout to the query, just in case...

agavrel commented 4 years ago

Ok thank you very much, I was able to fix it thanks to your hints (was calling the wrong db_name and using encode(pdf, 'hex') while data was already in bytes)