Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask SQLAlchemy:如何插入和查询utf8字符串?_Python_Python 2.7_Sqlite_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python Flask SQLAlchemy:如何插入和查询utf8字符串?

Python Flask SQLAlchemy:如何插入和查询utf8字符串?,python,python-2.7,sqlite,sqlalchemy,flask-sqlalchemy,Python,Python 2.7,Sqlite,Sqlalchemy,Flask Sqlalchemy,我试图使用Alchemy将阿拉伯语单词插入数据库,但我得到了很多错误 我正在做的是: 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

我试图使用Alchemy将阿拉伯语单词插入数据库,但我得到了很多错误

我正在做的是:

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()
所以我得到了这个错误:

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')]
TypeError: expected a string or other character buffer object
然后我对代码进行r编辑,并将utf-8设置为以下阿拉伯语单词:

test = User("مرحباً".decode("utf-8"), "admin@gmail.com")
所以现在终端没有显示任何错误

之后,我需要查询此数据并将其写入文件,或者我想定义此数据,因此我将其添加到我的代码中:

all = User.query.all()
f = open("utf-8.txt", "w")
f.write([x.username for x in all])
f.close()
现在我得到了这个错误:

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')]
TypeError: expected a string or other character buffer object
有没有办法解决这个问题?我不想只把它写到文件中,我还想这样定义它:

username2 = [x.username for x in all]

没有任何错误。

当你需要一个
str
@univerio好的时候,你正试图写一个
列表,所以当我将它编辑为:f.write(str([x.username for x in all])时,它是写到如下文件:[u'\u0645\u0631\u062d\u0628\u0627\u064b']我应该在这里做什么?好的,你想做什么?您希望写入文件的内容是什么?@univerio我想定义它,而不是像这样写入:username2=str([x.username for x in all]),然后我想在主页中使用它,比如:return render_template(“main.html”,username=username2)它可以工作,但返回的内容如下:[u'\u0645\u0631\u062d\u0628\u0627\u064b']我也不知道如何修复它。谢谢。你不能盲目地将
列表
转换为
str
。如果您没有准确地告诉Python您希望
str
的外观,它将打印列表的默认字符串表示形式。那么,再说一次,你到底希望字符串看起来像什么?