sqlite3数据库将不接受字体整形阿拉伯数据- python

dluptydi  于 2023-01-13  发布在  SQLite
关注(0)|答案(1)|浏览(116)

操作系统:Ubuntu 20.04
IDE:pyCharm社区构建版本号PC-223.8214.51,构建日期:2022年12月20日
语言:Python 3.10.6
我正在用kivy开发一个简单的应用程序,它可以查询sqlite3数据库中阿拉伯语的数据,现在,由于kivy本身不支持阿拉伯语,我不得不同时使用BIDI和arabic-shaper,这样我才能让用户输入正确的阿拉伯语。
该数据库是从一个.csv文件创建的,该文件由Ubuntu 20.04上的LibreOffice calc创建。我在终端上使用panda创建了我的数据库,如下所示:

users = pd.read_csv('numberDataBaseCSV.csv')
users.to_sql('users', conn, if_exists='replace', index = False)

首先,我必须重写TextInput.insert_text,如下所示:

class Ar_text(TextInput):
    max_chars = NumericProperty(20)  # maximum character allowed
    str = StringProperty()

    def __init__(self, **kwargs):
        super(Ar_text, self).__init__(**kwargs)
        self.text = bidi.algorithm.get_display(arabic_reshaper.reshape("اطبع شيئاً"))

    def insert_text(self, substring, from_undo=False):
        if not from_undo and (len(self.text) + len(substring) > self.max_chars):
            return

        self.str = self.str+substring
        self.text = bidi.algorithm.get_display(arabic_reshaper.reshape(self.str))
        substring = ""
        super(Ar_text, self).insert_text(substring, from_undo)

    def do_backspace(self, from_undo=False, mode='bkspc'):
        self.str = self.str[0:len(self.str)-1]
        self.text = bidi.algorithm.get_display(arabic_reshaper.reshape(self.str))

然后我像往常一样构建了应用程序:

class TestApp(kivy.app.App):
    def build(self):
        def create_connection(db_file):
            """ create a database connection to the SQLite database
                specified by the db_file
            :param db_file: database file
            :return: Connection object or None
            """
            conn = None
            try:
                conn = sqlite3.connect(db_file)
            except sqlite3.Error as e:
                print(e)

            return conn

        self.dbConnect = create_connection("numberData.db")
        self.number_cursor = self.dbConnect.cursor()

        reshaped_text = arabic_reshaper.reshape("بحث")
        bidi_text = bidi.algorithm.get_display(reshaped_text)

        self.circuit_number_label = kivy.uix.label.Label(text=bidi_text, font_name="janna-lt-bold/Janna LT Bold/Janna LT Bold.ttf")
        self.nameTextField = Ar_text(text=bidi_text, font_name="janna-lt-bold/Janna LT Bold/Janna LT Bold.ttf")
        self.searchButton = kivy.uix.button.Button(text=bidi_text, font_name="janna-lt-bold/Janna LT Bold/Janna LT Bold.ttf")
        self.searchButton.bind(on_press = self.search_number_by_name)

        boxLayout = kivy.uix.boxlayout.BoxLayout(orientation="vertical")
        boxLayout.add_widget(self.circuit_number_label)
        boxLayout.add_widget(self.nameTextField)
        boxLayout.add_widget(self.searchButton)

        return boxLayout

这是我点击按钮时触发的回调函数:

def search_number_by_name(self, event):
       reshaped_text = bidi.algorithm.get_display(self.nameTextField.text)
       print(reshaped_text)
       print("select الرقم,الدارة from numbers_table where الاسم = '" + reshaped_text.strip() + "'")
       self.number_cursor.execute("select الرقم,الدارة from numbers_table where الاسم = '" + reshaped_text.strip() + "'")
       rows =  self.number_cursor.fetchall()
       for row in rows:
           print(row)

然后当然运行应用程序:

testApp = TestApp()
testApp.run()

即使数据是正确的,我应该从数据库中获得查询的数据,但似乎什么也没有发生,甚至没有错误消息。
当我在where子句中硬编码数据时:

quereied_data = "خالد"

然后构建查询:

self.number_cursor.execute("select الرقم,الدارة from numbers_table where الاسم = '" + queried_data + "'")

我从数据库中得到了预期的结果,只有当我从TextInput检索数据时,我什么也得不到。
我试着用下面的代码段“撤销”整形过程:

reshaped_text = bidi.algorithm.get_display(self.nameTextField.text)

但这也不起作用,我不知道问题出在哪里,因为我不能得到任何错误信息。我会很感激的帮助。

cbwuti44

cbwuti441#

事实证明,TextInput类中有一个属性:str,因此为了检索数据并在查询中使用,您需要执行以下操作:

reshaped_text = self.nameTextField.str

然后在生成的查询中照常使用它:

self.number_cursor.execute("select الرقم,الدارة from numbers_table where الاسم = '" + reshaped_text.strip() + "'")
   rows = self.number_cursor.fetchall()
   for row in rows:
       print(row)

相关问题