0

Hi I save a byte array using nodejs mysql library. My issue is, the byte array I have is of length 9459. But when I insert into mysql and read it back , it is giving me an array of size 32504.

Below is my SQL query which I am executing from nodejs

 const query = "INSERT INTO spreaddata.workbook  SET `user_email`='" + userEmail + "', `workbook_data`='" + workbook_data + "'";

Below is my Select query

const selectQury = "SELECT * from spreaddata.workbook WHERE user_email='name@company.com';

Here workbook_data is the column of type blob in mysql. When I inspect the byte array recieved it is having a length of 32504. Why the length is getting increased.

I am resubmitting this question because clearly this is not a duplicate question

Dattatreya Kugve
  • 320
  • 1
  • 4
  • 21
  • 1
    Don't use string concatenation to create SQL, use a prepared statement with parameters. – Barmar Jun 27 '23 at 16:11
  • Question got closed but the link given pertains to SQL injecttion. My query was not at all related to preventing SQL injection. Not sure how this question is duplicate – Dattatreya Kugve Jun 27 '23 at 16:17
  • 1
    The solution is the same, use a prepared statement to substitute variables into SQL. – Barmar Jun 27 '23 at 16:18
  • const query = 'INSERT INTO spreaddata.workbook (user_email,workbook_data) VALUES(?,?) '; sql.query(query, [jsonData.userEmail, '['+jsonData.workbook_data+']']) I modified the query but still same issue – Dattatreya Kugve Jun 27 '23 at 17:12
  • There's no need for `'[' + `. Just use `[jsonData.userEmail, jsonData.workdbook_data]` – Barmar Jun 27 '23 at 17:14
  • The whole problem is that you're converting the byte array to a string, so you're getting its printed representation instead of the byte data. – Barmar Jun 27 '23 at 17:14
  • code: 'ER_WRONG_VALUE_COUNT_ON_ROW', errno: 1136, sqlState: '21S01', sqlMessage: "Column count doesn't match value count at row 1", sql: "INSERT INTO spreaddata.workbook (user_email,workbook_data) VALUES('name@company.com',80, 75, 3, 4 – Dattatreya Kugve Jun 27 '23 at 17:21
  • I get above error when I run the query you suggested – Dattatreya Kugve Jun 27 '23 at 17:21
  • Does this help: [convert byte array to blob](https://stackoverflow.com/questions/38399544/is-there-any-way-to-convert-byte-array-to-blob-object-on-client-side-in-node-js)? – Barmar Jun 27 '23 at 18:37
  • No not working . Need to import Blob to work.. But import allowed only in modules. require statement also not working – Dattatreya Kugve Jun 28 '23 at 04:17
  • I think this question deserves reopening as clearly none of the solution is working and it is not related to SQL injection prevention – Dattatreya Kugve Jun 28 '23 at 04:17
  • I've reopened. You can edit the question to show your attempt to employ all the techniques I linked to, then someone can help get it working. – Barmar Jun 28 '23 at 16:14

0 Answers0