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 ?

Mr. zero
  • 245
  • 4
  • 18

1 Answers1

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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thank you. but did you know what is the problem in my code ? can I fix my code ? – Mr. zero Apr 22 '16 at 07:06
  • @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. – jfs Apr 22 '16 at 07:19
  • 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 Apr 22 '16 at 07:36
  • @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). – jfs Apr 22 '16 at 07:43