2

I have a problem with storing in MySQL db a PDF file made with Reportlab library. Here's my code:

def insertIntoDb(pdfFullPath,name,surname,gravity):
  print('PRIMA DEL MYSQL')
  print('pdf full path'+pdfFullPath)
  mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="deepface")
  with open(pdfFullPath,'rb') as pdfvar:
     blob = pdfvar.read()
     print(blob)
     sqlQuery = "INSERT INTO diagnosi(name,surname,pdf,gravity) VALUES (%s,%s,%s,%s)"
     mycursor = mydb.cursor()
     val = (name,surname,blob,gravity,)
     mycursor.execute(sqlQuery,val)
     mydb.commit()
     mycursor.close()
     mydb.close()

The console says:

mysql.connector.errors.DataError: 1406 (22001): Data too long for column 'pdf' at row 1

I have already set max allowed packet in mysql configuration file, but the problem is that when I try to print (I know that i can't) the PDF I get this:

^\\7:[,1qq,N_Sd$dm-:XU2/Pga=O1f/`hY7X1nrca).:_\'-4,n*"L5r,CHFpGo:"E,MDLu7EW%CFF0$Rl?jT\'6%k%,?AF%UK6ojt/c$<^Xh=;VarY:L8cQYTgj/:CfA/j1=dbU@a<:%D;rDV[)WDu)5*"98A5kkfYAqs0FFVZk[*Mb(Rs?hIk

and another things, how i can store in my db? I tried to decode with b64 but doesn't work.


> SHOW CREATE TABLE diagnosi;

diagnosi | CREATE TABLE `diagnosi` (
  `tempId` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `surname` varchar(30) NOT NULL,
  `pdf` blob NOT NULL,
  `gravity` varchar(50) NOT NULL,
  PRIMARY KEY (`tempId`)\n) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

1 Answers1

2

The MySQL BLOB datatype is limited to 216 bytes in size. The LONGBLOB datatype can be up to 232 bytes, so change the column type from BLOB to LONGBLOB.

See the storage requirements for string types in the docs.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153