title: Flask-SQLAlchemy: How to insert and query utf8 string?
body: <p>I'm trying to insert Arabic word into Database using Flask-SQLAlchemy but I'm getting alloot of errors.</p>
  
  <p>What I'm doing is :</p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///sqlite3.db'
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  
  db = SQLAlchemy(app)
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(120), unique=True)
      email = db.Column(db.String(120), unique=True)
  
      def __init__(self, username, email):
          self.username = username
          self.email = email
  
  db.create_all()
  test = Basic_Settings("مرحباً", "admin@gmail.com")
  db.session.add(test)
  db.session.commit()
  </code></pre>
  
  <p>Soo I'm getting this error :</p>
  
  <pre><code>sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings. [SQL: u'INSERT INTO user (username, email) VALUES (?, ?)'] [parameters: ('\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7\xd9\x8b', 'admin@gmail.com')]
  </code></pre>
  
  <p>Then I r-edit my code and set utf-8 to this Arabic word like this :</p>
  
  <pre><code>test = User("مرحباً".decode("utf-8"), "admin@gmail.com")
  </code></pre>
  
  <p>so now terminal not showing any errors</p>
  
  <p>After that I need to query this data and write it into file or I want to define this data so I add this to my code :</p>
  
  <pre><code>all = User.query.all()
  f = open("utf-8.txt", "w")
  f.write([x.username for x in all])
  f.close()
  </code></pre>
  
  <p>And now I'm getting this error :</p>
  
  <pre><code>TypeError: expected a string or other character buffer object
  </code></pre>
  
  <p>Is there any way to fix this problem here I don't want just to write this to file I want also to define it like :</p>
  
  <pre><code>username2 = [x.username for x in all]
  </code></pre>
  
  <p>without any error.</p>