2

I'm trying to insert Arabic words in Sqlite database then query these words. What I'm facing is the result back to me like this : ('\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7\xd9\x8b',)

so here is simple code explain for what I'm doing :

# coding: utf-8

import sys; reload(sys).setdefaultencoding("utf-8")

import sqlite3

class Database(object):

    def execute_db(self, *args):
        db = sqlite3.connect("sqlite3.db")
        db.text_factory = str
        cur = db.cursor()
        data = True
        try:
            args = list(args)
            args[0] = args[0].replace("%s", "?")
            args = tuple(args)
            cur.execute(*args)
            arg = args[0].split()[0].lower()
            if arg in ["update", "insert", "delete", "create"]: db.commit()
        except Exception, data:
            data = False
            db.rollback()
        db.commit()
        db.close()
        return data

    def fetch_one(self, *args):
        db = sqlite3.connect("sqlite3.db")
        db.text_factory = str
        try: cur = db.cursor()
        except: return None
        data = None
        try:
            args = list(args)
            args[0] = args[0].replace("%s", "?")
            args = tuple(args)
            cur.execute(*args)
            try: data = cur.fetchone()
            except Exception, data: data = None
        except Exception, data:
            data = None
            db.rollback()
        db.close()
        return data


class Start(Database):
    def __init__(self):
        self.execute_db("create table test(title text)")
        self.execute_db("insert into test(title) values(%s)", ("مرحباً",))

        self.write_query_into_file()

    def write_query_into_file(self):
        f = open("test.txt", "w+")
        f.write(str(self.fetch_one("select title from test")))
        f.close()

try: Start()
except Exception as why: print why

In this example I create test table with one value title I insert to title Arabic word then when I'm trying to query this word and write it to file show to me this : ('\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7\xd9\x8b',)

How can I fix it ?

3
  • do not use reload(sys).setdefaultencoding("utf-8") Commented Apr 22, 2016 at 6:30
  • @J.F.Sebastian the same thing not fixed my problem Commented Apr 22, 2016 at 6:42
  • there are multiple issues in your code. Commented Apr 22, 2016 at 6:51

1 Answer 1

1

Use unicode type, to represent text in Python. Do not call str() on a Unicode string (str() converts to bytes on Python 2).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import io
import sqlite3

# insert Unicode into SQLite db
conn = sqlite3.connect(':memory:')
conn.execute("create table test(title text)")
conn.execute("insert into test(title) values(?)", ("مرحباً",))

# print titles to file as text in utf-8 encoding
with io.open('utf-8.txt', 'w', encoding='utf-8') as file:
    for title, in conn.execute("select title from test"):
        print(title, file=file)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. but did you know what is the problem in my code ? can I fix my code ?
@Mr.zero: where are too many issues in your code. Use the code from my answer instead. If it is not clear what any of the lines in the code does; ask.
Actually I want to use it with Flask. so is there any way to print this word or define it with value like : hello = conn.execute("select title from test") And when I use it. it should work fine. I mean what if I want to use it not just in writing files. thank u again.
@Mr.zero title in the code is a variable (Unicode string). Use it however you like (it is not necessary to write it to a file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.