title: Python (flask) BLOB image, SQLite, Sqlalchemy - display image
body: <p>I am trying to store images in my sqlite database as BLOB data-type and then display the images on my webpage but i keep getting a sqlalchemy error (Am using sqlalchemy as my ORM), error:</p>
  
  <pre><code>sqlalchemy.exc.StatementError
  StatementError: buffer object expected (original cause: TypeError: buffer object expected) u'INSERT INTO room (owner_name, size, description, price, address, owner_number, room_image, university_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' [{'owner_name': u'Thomas Jefferson', 'description': u'Master Bedroom, Non-furnished, toilet attached', 'price': u'RM650/Month', 'room_image': &lt;FileStorage: u'udacity.png' ('image/png')&gt;, 'university_id': 1, 'address': u'Cyber Heights Villa', 'size': u'325sqft', 'owner_number': u'+60111223456'}]
  </code></pre>
  
  <p>Here are my codes:
  1.) my database setup file (i have the BLOB Column in the room table) :</p>
  
  <pre><code>import sys
  from sqlalchemy.dialects.sqlite import BLOB
  from sqlalchemy import Column, ForeignKey, Integer, String
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import relationship
  from sqlalchemy import create_engine
  Base = declarative_base()
  
  
  class University(Base):
  
      __tablename__ = "university"
  
      #column definitions for the university table
      id = Column(Integer, primary_key=True)
      name = Column(String(250), nullable=False)
      city = Column(String(80), nullable=False)
  
      @property
      def serialize(self):
          #Returns object data in easilty serializable format
          return{
              "name": self.name,
              "city": self.city,
              "id": self.id,
          }
  
  
  class Room(Base):
  
      __tablename__ = "room"
  
      #column definitions for the room table
      owner_name = Column(String(90), nullable=False)
      id = Column(Integer, primary_key=True)
      size = Column(String(60))
      description = Column(String(250))
      price = Column(String(10))
      address = Column(String(250))
      owner_number = Column(String(15))
      room_image = Column(BLOB)
      university_id = Column(Integer, ForeignKey("university.id"))
      university = relationship(University)
  
  engine = create_engine("sqlite:///gotroom.db")
  Base.metadata.create_all(engine)
  </code></pre>
  
  <p>2.) this is my python file code where i intend to allow uploading of images as BLOB into room_image (within newRoom function is where the upload for a photo image is to take place and stored in room_image Column in my room table): </p>
  
  <pre><code>#show rooms for a university
  @app.route("/university/&lt;int:university_id&gt;/")
  @app.route("/university/&lt;int:university_id&gt;/rooms/")
  def showRooms(university_id):
      university = session.query(University).filter_by(id=university_id).one()
      rooms = session.query(Room).filter_by(university_id=university_id).all()
      return render_template("rooms.html", rooms=rooms, university=university)
  
  
  #create a new room for a particular university
  @app.route("/university/&lt;int:university_id&gt;/rooms/new/", methods=["GET", "POST"])
  def newRoom(university_id):
      if request.method == "POST":
          aNewRoom = Room(owner_name=request.form["ownerName"], size=request.form["roomSize"]\
                         , description=request.form["roomDescription"], price=request.form["roomPrice"]\
                          , address=request.form["adress"], owner_number=request.form["phoneNum"], \
                          room_image=request.files["roomImage"], university_id=university_id)
  
          session.add(aNewRoom)
          session.commit()
  
          #feedback to user
          flash("New Room Created")       
  
          return redirect(url_for("showRooms", university_id=university_id))
      else:
          return render_template("newroom.html", university_id=university_id)
  </code></pre>
  
  <p>3.) here is the form template code for creating a new room "newroom.html" (i have  to allow the upload for the image):</p>
  
  <pre><code>&lt;form action="{{url_for('newRoom', university_id=university_id)}}" method="POST" enctype='multipart/form-data'&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-4"&gt;
              &lt;input type="text" class="form-control" name="ownerName" placeholder="Name"&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-3"&gt;
              &lt;input type="text" class="form-control" name="phoneNum" placeholder="Contact Number"&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-5"&gt;
              &lt;input type="text" class="form-control" name="adress" placeholder="Address"&gt;
          &lt;/div&gt;
      &lt;/div&gt;&lt;!--first row--&gt;
      &lt;/br&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-2"&gt;
              &lt;input type="text" class="form-control" name="roomSize" placeholder="Room Size"&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-2"&gt;
              &lt;input type="text" class="form-control" name="roomPrice" placeholder="Room Price/Month"&gt;
          &lt;/div&gt;
      &lt;/div&gt;&lt;!--second row--&gt;
      &lt;/br&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-4" style="height: 100px"&gt;
              &lt;input type="text" class="form-control" name="roomDescription" placeholder="Room Details" style="height: 100%"&gt;
          &lt;/div&gt;
          &lt;input type='file' name='roomImage'&gt;
      &lt;/div&gt;
      &lt;/br&gt;
  
      &lt;input type="submit" value="Post Room" class="btn btn-success btn-lg" role="button"&gt;
  
  &lt;/form&gt; 
  </code></pre>
  
  <p>4.) and here is the template code where i want to display all the information including the image:</p>
  
  <p></p>
  
  <pre><code>        &lt;/br&gt;
  
          &lt;img src="{{room.room_image}}"&gt;
          &lt;strong&gt;Owner Name&lt;/strong&gt;: {{room.owner_name}}
  
          &lt;/br&gt;
          &lt;strong&gt;Contact Number&lt;/strong&gt;: {{room.owner_number}}
          &lt;/br&gt;
          &lt;strong&gt;Home Address&lt;/strong&gt; : {{room.address}}
          &lt;/br&gt;
          &lt;strong&gt;Room Price&lt;/strong&gt; : {{room.price}}
          &lt;/br&gt;
          &lt;strong&gt;Room Size&lt;/strong&gt;  : {{room.size}}
          &lt;/br&gt;
          &lt;strong&gt;Room Description&lt;/strong&gt;: {{room.description}}
          &lt;div&gt;
  </code></pre>
  
  <p>Anytime i remove the image, i do not get the error, but as soon as i try to display the image, the error comes back, could any please help me out with this? i can provide more details or code if needed</p>