Jump to content

how can I upload some Chinese words to sql server database?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
wqjwftcaqr

wqjwftcaqr

    Newbie

  • Members
  • Pip
  • 8 posts
this is my script,I use pymssql module to upluad some data to my database,but I have some problem,please help me,thank you:c-smile:

#-*-coding:cp936-*-

import pymssql  #这个模块你自己上网找一下

##class SqlDataBase:

conn = pymssql.connect(host='192.168.0.114',user='charles',password='charles',database='newsforPDA')

cursor = conn.cursor()

##resource='select id,new_date from uploadnews'

##cursor.execute(resource,())

##print cursor.description


cursor.execute(" INSERT INTO newsbycharles (title,content,raw_link,new_date,system_date,source) VALUES('%s','%s','%s','%s','%s','%s') "%('测试',u'abcd',u'http://abc',u'1800-1-1',u'1800-1-1',u'test2')) # 你可以自己写代码拼接出语句

##cursor.execute(" update table1 set col1 .... " ) #\u751f\u547d\u65f6\u62a5


conn.commit() # 提交


conn.close() # 关闭


raw_input('finished')

the warm is:

Quote

>>>
Traceback (most recent call last):
File "E:\学习资料\python improve\using20101203\class\softwareforupload_collection\复件 python upload to SQL 组件.py", line 10, in <module>
cursor.execute(" INSERT INTO newsbycharles (title,content,raw_link,new_date,system_date,source) VALUES('%s','%s','%s','%s','%s','%s') "%('测试',u'abcd',u'http://abc',u'1800-1-1',u'1800-1-1',u'test2')) # 你可以自己写代码拼接出语句
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position 88: ordinal not in range(128)
>>>

Edited by Roger, 14 February 2012 - 01:59 PM.
added code tags


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You have to indicate that the strings are unicode by prefixing them with an n before the single quote. You also need to make sure your columns are nvarchar, ntext, etc.

Unicode Data
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
wqjwftcaqr

wqjwftcaqr

    Newbie

  • Members
  • Pip
  • 8 posts
I am sure my title column is nvarchar, but I still don't understand. could you give me an example about how toindicate that the strings are unicode by prefixing them with an n before the single?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
insert into MyTable (title) values (n'chinese here');
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
wqjwftcaqr

wqjwftcaqr

    Newbie

  • Members
  • Pip
  • 8 posts
Thank you for your help, and this sentence would be useful:
cursor.execute(" INSERT INTO newsbycharles (title) VALUES('%s') "%u'\u5973\u6027\u6cbb\u4e0d\u5b55\u572835\u5c81\u524d\u708e\u75c7\u4e3a\u4e0d\u5b55\u4e0d\u80b2\u9996\u56e0'.encode('gbk')) # 你可以自己写代码拼接出语句