473,725 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL Insert Unicode Problem

Hi,

I'm trying to insert some data from an XML file into MySQL. However,
while importing one of the files, I got this error:

Traceback (most recent call last):
File "wa.py", line 304, in ?
main()
File "wa.py", line 257, in main
curHandler.walk Data()
File "wa.py", line 112, in walkData
self.cursor.exe cute(*sql) #try multi-execute to speed up query.
File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 151, in
execute
File "build/bdist.linux-i686/egg/MySQLdb/connections.py" , line 247,
in literal
File "build/bdist.linux-i686/egg/MySQLdb/connections.py" , line 185,
in unicode_literal
UnicodeEncodeEr ror: 'latin-1' codec can't encode character u'\u20ac'
in position 28: ordinal not in range(256)

What do I need to change to make this work?

Thanks!
Erik

Apr 9 '07 #1
1 5851
erikcw wrote:
Hi,

I'm trying to insert some data from an XML file into MySQL. However,
while importing one of the files, I got this error:

Traceback (most recent call last):
File "wa.py", line 304, in ?
main()
File "wa.py", line 257, in main
curHandler.walk Data()
File "wa.py", line 112, in walkData
self.cursor.exe cute(*sql) #try multi-execute to speed up query.
File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 151, in
execute
File "build/bdist.linux-i686/egg/MySQLdb/connections.py" , line 247,
in literal
File "build/bdist.linux-i686/egg/MySQLdb/connections.py" , line 185,
in unicode_literal
UnicodeEncodeEr ror: 'latin-1' codec can't encode character u'\u20ac'
in position 28: ordinal not in range(256)

What do I need to change to make this work?

Thanks!
Erik
MySQL does Unicode quite well, but Unicode is not the default. Getting
all the defaults right is a pain. It looks like you have MySQL,
or MySQLdb, or the connection between them in "latin1" mode.

The database server has to support Unicode, which is a build option
and a version issue. Do a

SHOW VARIABLES LIKE "character_set% "

Ideally, you should see the character sets for client, connection,
database, results, server, and system all as "utf8". You might not,
given the problem you're having, but that can be dealt with.
Those are just defaults.

The field you're storing into has to be in Unicode mode, which
can either be set explicitly with CREATE TABLE or can come from the
default for the server, database, or table. It's usually easier
to use UTF8 for everything in the database, unless you have legacy
database issues.

Try this in SQL:

ALTER DATABASE yourdatabasenam e DEFAULT CHARACTER SET UTF8;

If you can't execute that statement, assuming you have permissions
to do so, then your MySQL isn't configured to support Unicode.
(If you're on a shared server, you might have to use ALTER TABLE
instead; you may not have ALTER DATABASE permission.)

Once you've done that, all tables created after that point will have
Unicode text fields. You can also use ALTER TABLE on existing tables,
if you need to convert their format, or create tables with
DEFAULT CHARACTER SET UTF8.

The connection to the MySQL server has to be in Unicode mode.
This is separate from the server's mode and the table mode.
And MySQLdb has to be in Unicode mode, which is separate from
the connection mode. MySQLdb doesn't know what the server is doing;
you have to tell it what you want.

Try something like this to connect to the server:

db = MySQLdb.connect (host="localhos t",
use_unicode = True, charset = "utf8",
user=username, passwd=password , db=database)

You need to specify both "use_unicod e" (which sets the
mode in MySQLdb), and "charset" (which sets the connection mode).

Once you've put all the components into these modes, you
should have end-to-end Unicode database capability.

John Nagle
Apr 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1540
by: Lol McBride | last post by:
Hi all, I'm using Python 2.3 and the MySQLDB module on a Mandrake Linux 9.2 system and I'm encountering a problem whentrying to insert a string object intoa varchar field in a database I'm using. The query being sent to the MySQL server by my program is : INSERT INTO analysis (DrawNumber,pool1)VALUES (847, ' (4, 8, 21, 24, 39, 44) ' ) ; But the field pool1 actually contains (4, 8, 21, 24, 39, i.e it is missing the final digits ( 44 )...
0
1938
by: gw | last post by:
I am new to MySQL and my problem is with storing a datetime value in a table. I created a table with a column designated as DATETIME. I fill it with the NOW() function. No matter whether I default it or try and set with code, all I get is a date! Even running "SELECT Now()" only returns a date. If I run the following query: Date_Format(now(), GET_FORMAT(DATETIME,'USA'))
2
1979
by: Priem, Alexander | last post by:
Hi everyone, I have a PostgreSQL 7.4 database running, which was initdb-ed using standard (SQL_ASCII) encoding, with -lc-collate=C option set. Everything is running fine, but I just discovered something funny. If text containing characters like ë is inserted into the database (text field), the inserting goes fine. When I view this data using phpPgAdmin (v3.2.1) it looks fine, including these special characters.
12
1888
by: manstey | last post by:
I am writing a program to translate a list of ascii letters into a different language that requires unicode encoding. This is what I have done so far: 1. I have # -*- coding: UTF-8 -*- as my first line. 2. In Wing IDE I have set Default Encoding to UTF-8 3. I have imported codecs and opened and written my file, which doesn't have a BOM, as encoding=UTF-8 4. I have written a dictionary for translation, with entries such as...
5
1752
by: Norman Diamond | last post by:
Here are two complete lines of output from Visual Studio 2005: 1>$B%W%m%8%'%/%H=PNO$K(B Authenticode $B=pL>$7$F$$$^$9(B... 1>Successfully signed: c:\T The first line means roughly: Doing Authenticode signature to project output. The second line is harder to translate. The reason is that the second line says it successfully signed something that doesn't exist. I don't have a
1
5188
by: Chris Yan | last post by:
Hi All I have an excel sheet with Chinese characters in them. I'm using the OLEBE 4.0 Jet Driver in C# Microsoft Visual Studio 2005 to read from the Excel sheet and then enter into mySQL database. The problem isn't with mysql but with C# reading the chinese characters from Excel.
3
2238
by: Amir Alilou | last post by:
HI How we can store UNICODE characters/strings in MySQL databases and then use this character/string in the web with ASP.NET +VB. for example in MSSQL SERVER we use literal "N" when we use INSERT INTO query.but in MySQL i dont know how to store unicode characters in database. Plase help me with sample thanks
4
1323
by: daniyalnawaz | last post by:
Hi guys! Whats wrong with this statement INSERT INTO MarketData(symbol,date,time,trade_price,change)VALUES('A','2007-09-27',16:00:00,36.12,'0.00') im getting a following error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'change)VALUES('A','2007-09-27','16:00:00',36.12,'0.00')' at line 1
2
2362
Thekid
by: Thekid | last post by:
I'm having trouble getting my python scripts to insert into a mysql database. I'm able to do other commands successfully such as adding or dropping tables, selecting items from tables but inserting into tables isn't working. I can manually insert but the script won't do it. I have an entry field for a last name and am using .get() to grab the data. If I enter the name 'Jones' into the field and submit it, I get this error: OperationalError:...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.