DB2 database stores/reads umlauts and special chars wrong - sql

I create my database with the following command:
db2 create database kixfs using codeset UTF-8 territory AT
My insert scripts (DMLs) are ecoded in UTF-8.
Example insert statement from resources.dml:
INSERT INTO RESOURCES (RESOURCEKEY, LANGUAGE, CATEGORY, RESOURCEVALUE) VALUES ('XXXX', 'de', 'action', 'Funktion "Gerätemodell erfassen" erfolgreich ausgeführt.');
If i check the table content after creation:
Fehler bei der Ausführung der Funktion "Gerätemodell erfassen".
If i check the database configuration everything looks fine: (db2 get db cfg for MY_DB)
Any ideas why the data is read or stored wrong?
Edit:
I execute the insert script via a batchfile from the db2 admin console (CLP):
db2 -t -v -f resources.dml +o -z createTablesViews.log
Could it depend from the encoding of the db2 termminal?? And if yes how do i change it?

We had the same problem. Script, too. The script encoding has to match the server encoding, in our case ANSI. Even if the database is UTF-8. Converting the script file to ANSI did the trick for us.

Related

Creating a Format File for Bulk Import

I am trying to create a Format File to bulk import a .csv file but i, am getting an error.
Query I used
"BCP -SMSSQLSERVER01.[Internal_Checks].[Jan_Flat] format out -fC:\Desktop\exported data\Jan_FlatFormat.fmt -c -T -Uasda -SMSSQLSERVER01 -PPASSWORD"
I am getting an error
"A valid table name is required for in, out, or format options."
This is the error. can anyone suggest what need to do.
According to the bcp Utility documentation the first parameter should be a [Database.]Schema.{Table | View | "query"}, so don't put -SMSSQLSERVER01 where you've got it. Also use format nul instead of format out.
Try using:
bcp.exe [Internal_Checks].[Jan_Flat] format nul "-fC:\Desktop\exported data\Jan_FlatFormat.fmt" -c -SMSSQLSERVER01 -T -Uasda -PPASSWORD
Note the quotes " around the -f switch because your path name contains space characters.
Also note that the -c switch causes single-byte characters (ASCII/OEM/codepage with SQLCHAR) to be written out. If your table contains nchar, nvarchar or ntext columns you should consider using the -w switch instead so as to write out UTF-16 encoded data (using SQLNCHAR).

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

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
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"
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
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

How can I update a single field in sqlite3 with the contents of a file?

This is equivalent to my earlier question here, but for sqlite.
As before, I am trying to do the following using the sqlite3 command line client.
UPDATE my_table set my_column=CONTENT_FROM_FILE where id=1;
I have looked at the documentation on .import, but that seems to be a little heavyweight for what I am trying to do.
What is the correct way to set the value of one field from a file?
The method I seek should not impose constraints on the contents of the file.
Assuming the file content is all UTF-8 text and doesn't have any quote characters that would be misinterpreted, you could do this (assuming posix shell - on Windows try cygwin):
$ echo "UPDATE my_table set my_column='" >> temp.sql
$ cat YourContentFile >> temp.sql
$ echo "' where id=1;" >> temp.sql
$ sqlite3
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .read temp.sql
If the content does have single quotes, escape them first with a simple find-and-replace (you'd need to do that anyway).
hth!
See: http://www.sqlite.org/cli.html#fileio
sqlite> INSERT INTO images(name,type,img)
...> VALUES('icon','jpeg',readfile('icon.jpg'));
In your case:
UPDATE my_table set my_column=readfile('yourfile') where id=1;
If you don't have readfile, you need to .load the module first.
Note
I found that the provided fileio module: http://www.sqlite.org/src/artifact?ci=trunk&filename=ext/misc/fileio.c uses sqlite3_result_blob. When I use it in my project with text columns, it results in Chinese characters being inserted into the table rather than the bytes read from file. This can be fixed by changing it to sqlite3_result_text. See http://www.sqlite.org/loadext.html for instructions on building and loading run-time extensions.

Cyrillic symbols in SQL code are not correctly after insert

I use SQL Server 2005 and I am try to store Cyrillic characters but I can't with SQL code by trying to run this is SQL Server:
INSERT INTO Assembly VALUES('Македонски парлиамент број 1','','');
Or from C# is happening the same problem but inserting/updating the column from SQL Server it work and it is store normally.
The datatype of column is nvarchar.
You have to add N prefix before your string.
When you implicitly declare a string variable it is treated as varchar by default. Adding prefix N denotes that the subsequent string is in Unicode (nvarchar).
INSERT INTO Assembly VALUES(N'Македонски парлиамент број 1','','');
Here is some reading:
http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html
https://msdn.microsoft.com/en-IN/library/ms186939.aspx
What is the meaning of the prefix N in T-SQL statements?
I'm not sure if you are doing a static stored procedure or scripting, but maybe the text is not being encoded properly when you save it to disk. I ran into this, and my problem was solved in PowerShell by correcting the encoding of the SQL that I saved to disk for osql processing:
Out-File -FilePath "MyFile.sql" -InputObject $MyRussianSQL -Encoding "Unicode" -Force;
& osql -U myuser -P password -i "MyFile.sql";

sqlite3 echo and '<' piping command not working as expected

I am reading a SQL book and the author is using Sqlite3, which is awesome because there is not a server to mess with.
In the book the author says to type:
sqlite3 -echo something.db < some.sql
The problem is nothing ever echos out to the terminal nor is there even a database created from the '<' redirection command.
Does anyone know what is going on...with this?
Actually, the command you show IS a proper way of creating a new database from sql dump.
Can you please show your sql file contents (cat some.sql)? The only way I can reproduce the behavior described is by feeding sqlite an empty sql file.
Try this commands and see if you can get the same result:
$ cat <<EOF > test.sql
> create table test1 (f1, f2, f3);
> insert into test1(f1, f2, f3) values ("foo", "bar", "baz");
> EOF
$ sqlite3 -echo test.db < test.sql
create table test1 (f1, f2, f3);
insert into test1(f1, f2, f3) values ("foo", "bar", "baz");
$ file test.db
test.db: SQLite 3.x database
something.db is an existing database. < some.sql means it picks data from that file and write it in the sqlite console. So both files have to exist.
something.db must be a valid l sqlite3 database file (or an empty or not existing file); some.sql must be a text file with your commands in it.
The -echo params specify that it have to print the command before execution.
According to your comments and your terminal screen shot, I get the impression that your "some.sql" file may be in the wrong encoding and/or starts with a BOM which confuses sqlite.
You can use the file command to find out, and if this is the problem use iconv, recode or your favorite text editor to convert the file into the encoding your terminal expects, and the correct line endings (\n aka "LF" in Linux, \r\n aka "CRLF" in Windows).