Store Arabic in SQL Database

store arabic in SQL database

You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar)

CREATE TABLE #test
(
col1 VARCHAR(100) COLLATE Latin1_General_100_CI_AI,
col2 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
col3 NVARCHAR(100)
)
INSERT INTO #test VALUES(N'لا أتكلم العربية',N'لا أتكلم العربية',N'لا أتكلم العربية')

Note the N before values in insert statement above. If you do not mention it, system will treat the values as Varchar, not NVarchar.

SELECT * FROM #test

Returns

col1                           col2                           col3
------------------------------ ------------------------------ ------------------------------
?? ????? ??????? لا أتكلم العربية لا أتكلم العربية

To see a list of Arabic collations use

SELECT name, description 
FROM fn_helpcollations()
WHERE name LIKE 'Arabic%'

How to store Arabic in SQL server?

To store unicode string data, use NVARCHAR(2000) rather than VARCHAR(2000) for column [Ayat_Description]

Ref.: nchar and nvarchar

How to insert Arabic characters into SQL database?

For the field to be able to store unicode characters, you have to use the type nvarchar (or other similar like ntext, nchar).

To insert the unicode characters in the database you have to send the text as unicode by using a parameter type like nvarchar / SqlDbType.NVarChar.

(For completeness: if you are creating SQL dynamically (against common advice), you put an N before a string literal to make it unicode. For example: insert into table (name) values (N'Pavan').)

Insert arabic words in sql server

Use unicode datatypes for your data storage and begin your strings with N literal like this:

declare @t table(col Nvarchar(100)); 
insert @t (col) values (N'your text');

Or use the appropriate COLLATION with non-unicode data types but pass your values as unicode ones (with N) because your server/database collation seems to be different from arabic

How to store arabic text in mysql database using python?

To clarify a few things, because it will help you along in the future as well.

txt = u'Arabic (\u0627\u0644\u0637\u064a\u0631\u0627\u0646)'

This is not an Arabic string. This is a unicode object, with unicode codepoints. If you were to simply print it, and if your terminal supports Arabic you would get output like this:

>>> txt = u'Arabic (\u0627\u0644\u0637\u064a\u0631\u0627\u0646)'
>>> print(txt)
Arabic (الطيران)

Now, to get the same output like Arabic (الطيران) in your database, you need to encode the string.

Encoding is taking these code points; and converting them to bytes so that computers know what to do with them.

So the most common encoding is utf-8, because it supports all the characters of English, plus a lot of other languages (including Arabic). There are others too, for example, windows-1256 also supports Arabic. There are some that don't have references for those numbers (called code points), and when you try to encode, you'll get an error like this:

>>> print(txt.encode('latin-1'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 8-14: ordinal not in range(256)

What that is telling you is that some number in the unicode object does not exist in the table latin-1, so the program doesn't know how to convert it to bytes.

Computers store bytes. So when storing or transmitting information you need to always encode/decode it correctly.

This encode/decode step is sometimes called the unicode sandwich - everything outside is bytes, everything inside is unicode.


With that out of the way, you need to encode the data correctly before you send it to your database; to do that, encode it:

q = u"""
INSERT INTO
tab1(id, username, text, created_at)
VALUES (%s, %s, %s, %s)"""

conn = MySQLdb.connect(host="localhost",
user='root',
password='',
db='',
charset='utf8',
init_command='SET NAMES UTF8')
cur = conn.cursor()
cur.execute(q, (id.encode('utf-8'),
user_name.encode('utf-8'),
text.encode('utf-8'), date))

To confirm that it is being inserted correctly, make sure you are using mysql from a terminal or application that supports Arabic; otherwise - even if its inserted correctly, when it is displayed by your program - you will see garbage characters.

Save Data in Arabic in MySQL database

To insert Arabic Data manually into your Phpmyadmin.

First you check either your database , table and column name is utf8 set or not.
If these are not set to utf8 then first you set it then you may insert arabic data into you db table.

YOU MAY CHECK EACH OF THESE BY LOOKING BELOW EXAMPLE.

For Database:

SELECT default_character_set_name FROM information_schema.SCHEMATA S
WHERE schema_name = "schemaname";

For Tables:

SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE CCSA.collation_name = T.table_collation
AND T.table_schema = "schemaname"
AND T.table_name = "tablename";

For Columns:

SELECT character_set_name FROM information_schema.`COLUMNS` C
WHERE table_schema = "schemaname"
AND table_name = "tablename"
AND column_name = "columnname";

You may easily set utf8 to your tables if you are using SQLYog.

Just right click on db, table, column name and click on alter option and set to

Database Chartset = utf8
Database Collation = utf8_general_ci .

Just Enjoy ....



Related Topics



Leave a reply



Submit