how to insert utf8 characters into oracle database using robotframework database library

Deadly 提交于 2019-12-07 08:23:39

问题


I have a robot script which inserts some sql statements from a sql file; some of these statements contain utf8 characters. If I insert this file manually into database using navicat tool, everything's fine. But when I try to execute this file using database library of robot framework, utf8 characters go crazy!

This is my utf8 included sql statement:

INSERT INTO "MY_TABLE" VALUES (2, 'تست1');

This is how I use database library:

Connect To Database Using Custom Params     cx_Oracle   ${dbConnection}
Execute Sql Script      ${sqlFile}
Disconnect From Database

This is what I get in the database:

������������ 1

I have tried to execute the SQL file using cx_Oracle directly and it's still failing! It seems there is a problem in the original library. This is what I've used for importing SQL file:

import cx_Oracle
if __name__ == "__main__":
    dsn_tns = cx_Oracle.makedsn(ip, port, sid)
    db = cx_Oracle.connect(username, password, dsn_tns)
    sql_commands = open(sql_file_addr, 'r').read().split(";")
    cr = db.cursor()
    for command in sql_commands:
        if not command in ["", "\t", "\n", "\r", "\n\r", "\r\n", None]:
            print "Executing SQL command:", command
            cr.execute(command)

    db.commit()

I have found that I can define character-set in the connection string. I've done it for mysql database and it the framework successfully inserted UTF8 characters into database; this is my connection string for MySQL:

database='db_name', user='db_username', password='db_password', host='db_ip', port=3306, charset='utf8'

But I don't know how to define character-set for Oracle connection string. I have tried this:

'db_username','db_password','db_ip:1521/db_sid','utf8'

And I've got this error:

TypeError: an integer is required

回答1:


As @Yu Zhang suggested, I read discussion in this link and I found out that I should set an environment variable NLS_LANG in order to have a UTF-8 connection to the database. So I've added below line in my test setup:

os.environ["NLS_LANG"] = "AMERICAN_AMERICA.AL32UTF8"



回答2:


Would any of links below help?

http://docs.oracle.com/cd/B19306_01/server.102/b14225/ch6unicode.htm#i1006779
http://www.theserverside.com/news/thread.tss?thread_id=39575
https://community.oracle.com/thread/502949



回答3:


There can be several problems in here...

The first problem might be that you don't save the test files using UTF-8 encoding.

Robot framework expects plain text test files to be saved using UTF-8 encoding, yet most text editors will not save by default using UTF-8.

Verify that your editor saves that way - for example, by opening the file using NotePad++ and choosing Encoding -> UTF-8

Another problem might be the connection to the Oracle database. It doesn't seem like you can configure the connection custom properties to explicitly state UTF-8

This means you probably need to state that the database schema itself is UTF-8



来源:https://stackoverflow.com/questions/32310396/how-to-insert-utf8-characters-into-oracle-database-using-robotframework-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!