mysql1_dart icon indicating copy to clipboard operation
mysql1_dart copied to clipboard

Bad state exception when attempting to query db with SELECT statement

Open relf108 opened this issue 4 years ago • 1 comments

I can connect to my database and create tables with this init method.

  static Future<void> initDB() async {
    final conn = await getConnection();
    await conn.query(
        'CREATE TABLE IF NOT EXISTS tbl_user (userID int NOT NULL AUTO_INCREMENT PRIMARY KEY, googleUserID varchar(255), givenName varchar(255), familyName varchar(255), email varchar(255), password varchar(255), handicapped boolean)');
    await conn.query(
        'CREATE TABLE IF NOT EXISTS tbl_booking (bookingID int NOT NULL AUTO_INCREMENT PRIMARY KEY, bayID varchar(255), createdDate DATETIME, startDate DATETIME, endDate DATETIME, ownerFK int, FOREIGN KEY (ownerFK) REFERENCES tbl_user(userID))');
    await conn.query(
        'CREATE TABLE IF NOT EXISTS tbl_tokens (tokenID int NOT NULL AUTO_INCREMENT PRIMARY KEY, tokenValue VARCHAR(255), ownerFK int, FOREIGN KEY (ownerFK) REFERENCES tbl_user(userID))');
    await conn.close();
  }

But the following query made via this method fails with a Bad state exception despite the database being accessible from the cli and the aforementioned init method (error message below).

  static Future<Results> search(
      {required String? table,
      required Map<String, String>? searchTermVal}) async {
    final conn = await getConnection();
    final result = await conn.query('select * from ? where ? = ?', [
      table,
      searchTermVal!.entries.first.key,
      searchTermVal.entries.first.value
    ]);
    await conn.close();
    return result;
  }

error message:

Bad state: Cannot write to socket, it is closed
BufferedSocket.writeBufferPart
package:mysql1/src/buffered_socket.dart:133
ReqRespConnection._sendBufferPart
package:mysql1/src/single_connection.dart:429
===== asynchronous gap ===========================
ReqRespConnection._processHandler
package:mysql1/src/single_connection.dart:461
===== asynchronous gap ===========================
ReqRespConnection.processHandler.<fn>
package:mysql1/src/single_connection.dart:474
===== asynchronous gap ===========================
Pool.withResource
package:pool/pool.dart:127
===== asynchronous gap ===========================
RangeError (byteOffset): Invalid value: Not in inclusive range 0..5: 7
dart:typed_data                                                      _ByteDataView.getUint16
Buffer.readUint16
package:mysql1/src/buffer.dart:240
new PrepareOkPacket
package:mysql1/…/prepared_statements/prepare_ok_packet.dart:20
Handler.checkResponse
package:mysql1/…/handlers/handler.dart:68
PrepareHandler.processResponse
package:mysql1/…/prepared_statements/prepare_handler.dart:45
ReqRespConnection._handleData
package:mysql1/src/single_connection.dart:349
ReqRespConnection._handleHeader
package:mysql1/src/single_connection.dart:318

Am I using the package wrong or is this a bug on your side? Thanks in advance :)

relf108 avatar Aug 05 '21 17:08 relf108

https://github.com/adamlofts/mysql1_dart/issues/88#issuecomment-894639665 this is a bug but solved

wustrong avatar Feb 17 '22 14:02 wustrong