mariadb utf8mb4 with dynamic column & procedure - mariadb

I have two questions for mariadb utf8mb4 with dynamic column.
Above all, I use mariadb version 10.0 and connect by jdbc.
For saving emoji characters, I modified mariadb as follow that,
Edited in /etc/my.cnf
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Edited DB Table Charset.
CREATE TABLE `MEMBER` (
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`regdate` datetime DEFAULT NULL,
`sso_json` blob,
..(skip)..
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Remove characterEncoding parameter from JDBC String
deleted particle : characterEncoding=utf-8
So, It's perfect, emoji character was saved in a varchar column ㅁccurately. But not in a dynamic column. In both Ad-Hoc Query and procedure, column_create() save question mark instead of emoji.
As follow, procedure sample.
CREATE DEFINER=`sample`#`%` PROCEDURE `SP_INSERT`(
inName varchar(500) CHARACTER SET utf8mb4
)
BEGIN
SET #pSql = CONCAT( ' INSERT INTO SAMPLE_TBL ( '
, ' name, sso_json '
, ' ) VALUES ( '
, ' ?, COLUMN_CREATE(?, ?) '
, ' ) '
);
-- variables bind
SET #pName = inName;
SET #pKey = 'title';
-- prepare stmt
PREPARE pstmt FROM #pSql;
EXECUTE pstmt USING #pName, #pKey, #pName;
END
Procedure Result : {'title', '?????'}.
And In a Ad-Hoc query,
set names utf8mb4 collate 'utf8mb4_unicode_ci';
select 'testπŸ˜„πŸ‰πŸš“πŸš…', column_json(column_create('name','testπŸ˜„πŸ‰πŸš“πŸš…'));
Result :
testπŸ˜„πŸ‰πŸš“πŸš…' || {\"name\":\"test????\"}
result column is accurately but column_json no.
set names utf8;
select 'testπŸ˜„πŸ‰πŸš“πŸš…', column_json(column_create('name','testπŸ˜„πŸ‰πŸš“πŸš…'));
Result :
testπŸ˜„πŸ‰πŸš“πŸš… || {\"name\":\"testπŸ˜„πŸ‰πŸš“πŸš…\"}
I don't know why. Help me, please.

sso_json blob acquires the table's DEFAULT CHARACTER SET utf8; you need utf8mb4 for Emoji, as you did with name.

Related

Issue with importing Image into CLOB data using TPT script

I have a simple tpt script (given below) to load image into CLOB column in an empty table.
USING CHARACTER SET UTF8
DEFINE JOB LoadingtableData
DESCRIPTION 'Loading data into table using TPT'
(
DEFINE SCHEMA TableStaging
DESCRIPTION 'SYS FILE Staging Table'
(
Col_Colb CLOB(131072) AS DEFERRED BY NAME
,Col_FNAME VARCHAR(100)
,Col_ID VARCHAR(50)
);
DEFINE OPERATOR FileReader()
DESCRIPTION 'Read file with list'
TYPE DATACONNECTOR PRODUCER
SCHEMA TableStaging
ATTRIBUTES (
VARCHAR TraceLevel = 'None'
, VARCHAR PrivateLogName = 'read_log'
, VARCHAR FileName = 'datafile.txt'
, VARCHAR OpenMode = 'Read'
, VARCHAR Format = 'Delimited'
, VARCHAR TextDelimiter = ',');
DEFINE OPERATOR SQLInserter()
DESCRIPTION 'Insert from files into table'
TYPE INSERTER
INPUT SCHEMA TableStaging
ATTRIBUTES (
VARCHAR TraceLevel = 'None'
, VARCHAR PrivateLogName = '#LOG'
, VARCHAR TdpId = '#TdpId '
, VARCHAR UserName = '#UserName '
, VARCHAR UserPassword = '#UserPassword ');
STEP LoadData (
APPLY ('INSERT INTO table_A(Col_Colb,Col_FNAME,Col_ID) VALUES (:Col_Colb,:Col_FNAME,:Col_ID);')
TO OPERATOR (SQLInserter [1])SELECT * FROM OPERATOR (FileReader ());););
To load data into table I'm using two text files:
File have all the Varchar column values and Clob data location.
Example data in file: <Clob_File_Location>,Name,123
Clob column value
Example data in file: Image.png
After executing the above tpt, I get message as "data loaded successfully". But when I check the table in place of image in Clob column text is loaded.
Can someone help in letting me know what I might be doing wrong.

Syntax Error when I try to create trigger in mysql

I am trying to create an trigger for an audit log, where after an insert into manageMemberLog and the action column is equal to CREATE, then insert the associate columns of users table and then grab the select the newly created row and grab the userID and then insert the rest of the data into profile, but I get this error msg when I tried to submit the query.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 9
Line 9 = END IF;
I tried carefully re-typing what I wrote but same error msg.
UPDATE: used the formatting and it says WHERE userID = #newUserID is the error, which i still don't understand as I grab the data from select and set the value into #newUserID.
DELIMITER $$
CREATE TRIGGER usersDB_ai AFTER INSERT
ON manageMemberLog
FOR EACH ROW
IF NEW.action = 'CREATE' THEN
INSERT INTO users (firstName, lastName, userName, email, pwd) VALUES (NEW.firstName, NEW.lastName, NEW.userName, NEW.email, NEW.pwd);
SET #newUserID := (SELECT userID FROM users WHERE userName = NEW.userName AND email = NEW.email);
UPDATE profiles SET rankID = NEW.rankID WHERE userID = #newUserID;
END IF;
END$$
DELIMITER ;
The related tables:
CREATE TABLE `users` (
`userID` int(11) NOT NULL,
`firstName` varchar(128) NOT NULL,
`lastName` varchar(128) NOT NULL,
`userName` varchar(128) NOT NULL,
`email` varchar(128) NOT NULL,
`pwd` varchar(128) NOT NULL
)
DELIMITER $$
CREATE TRIGGER `userCreated` AFTER INSERT ON `users` FOR EACH ROW BEGIN
INSERT INTO profiles (userID) VALUES(NEW.userID);
END
$$
DELIMITER ;
CREATE TABLE `profiles` (
`profileID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`rankID` int(11) NOT NULL,
...
)
CREATE TABLE `manageMemberLog` (
`manageMemberLogID` int(11) NOT NULL,
`manageDate` datetime NOT NULL,
`managerID` int(11) DEFAULT NULL,
`action` varchar(6) CHARACTER SET utf8mb4 NOT NULL,
`userID` int(11) DEFAULT NULL,
`firstName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`lastName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`userName` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`email` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`pwd` varchar(128) CHARACTER SET utf8mb4 NOT NULL,
`rankID` int(11) NOT NULL
)
Can anyone help thanks in advance!
Silly Me, so what went wrong is I forgot to write "BEGIN" after FOR EACH ROW and does not involve any of the error that it says. Error messages are sometimes not reliable but I guess it did help by say END. A better error message is you can't end when it never began.
so correct query is:
DELIMITER $$
CREATE TRIGGER usersDB_ai AFTER INSERT
ON manageMemberLog
FOR EACH ROW BEGIN
IF NEW.action = 'CREATE' THEN
INSERT INTO users (firstName, lastName, userName, email, pwd) VALUES (NEW.firstName, NEW.lastName, NEW.userName, NEW.email, NEW.pwd);
SET #newUserID := (SELECT userID FROM users WHERE userName = NEW.userName AND email = NEW.email);
UPDATE profiles SET rankID = NEW.rankID WHERE userID = #newUserID;
END IF;
END$$
DELIMITER ;

how to trim trailing spaces in teradata table columns

i want to trim trailing spaces for teradata table columns,
i do it like this,
trim(trailing from dictionary_managed_databases.dbname),
or use trim directly,
trim(dictionary_managed_databases.dbname),
but the result shows:
seems the trim do not work,
not sure how to do it in teradata,
create volatile table test ( dbname varchar(128) CHARACTER SET UNICODE ) on commit preserve rows;
insert into test values ( 'Database-Name' );
-- you don't need to trim a varchar column
select dbname || '~' from test;
(dbname||'~')
---------------------------------------------------------------------------------------------------------------------------------
Database-Name~
-- it is always max length, so not to loose any possible content
select trim(dbname) || '~' from test;
(Trim(BOTH FROM dbname)||'~')
---------------------------------------------------------------------------------------------------------------------------------
Database-Name~
-- you may cast it to shorten the resulting column
select cast(trim(dbname) as varchar(30)) from test;
Trim(BOTH FROM dbname)
------------------------------
Database-Name
-- it will never be less then the header, even if the content is less
select cast(trim(dbname) as varchar(10)) from test;
Trim(BOTH FROM dbname)
----------------------
Database-N
-- but it will truncate the result
select cast(trim(dbname) as varchar(10)) as dbname from test;
dbname
----------
Database-N
sel
dictionary_object_map.moId,
trim(dictionary_managed_databases.dbname)|| '~',
dictionary_deployed_info.dictionaryId,
dictionary_deployed_info.dictionaryName,
dictionary_managed_objects.moname
from dictionary_object_map,
dictionary_deployed_info,
dictionary_managed_objects ,
dictionary_managed_databases
where
dictionary_object_map.dictionaryId=dictionary_deployed_info.dictionaryId
and dictionary_object_map.moid=dictionary_managed_objects.moid
and dictionary_managed_databases.moDBId=dictionary_managed_objects.moDBId
and dictionary_managed_databases.dbname = 'customerservice';
the result
don't understand why output of field dbname still look like this,

How can I use IF statements in Teradata without using BTEQ

I'm trying to create some deployment tools and I don't want to use BTEQ. I've been trying to work with the Teradata.Client.Provider in PowerShell but I'm getting syntax errors on the creation of a table.
[Teradata Database] [3706] Syntax error: expected something between
';' and the 'IF' keyword.
SELECT * FROM DBC.TablesV WHERE DatabaseName = DATABASE AND TableName = 'MyTable';
IF ACTIVITYCOUNT > 0 THEN GOTO EndStep1;
CREATE MULTISET TABLE MyTable ,
NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
MyColId INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 2147483647
NO CYCLE)
NOT NULL,
MyColType VARCHAR(50) NULL,
MyColTarget VARCHAR(128) NULL,
MyColScriptName VARCHAR(256) NULL,
MyColOutput VARCHAR(64000) NULL,
isMyColException BYTEINT(1) NULL,
ExceptionOutput VARCHAR(64000) NULL,
MyColBuild VARCHAR(128) NULL,
MyColDate TIMESTAMP NOT NULL
)
PRIMARY INDEX PI_MyTable_MyColLogId(MyColLogId);
LABEL EndStep1;
I would rather not use BTEQ as I've not found it has worked well in other deployment tools we have created and requires a bit of hacks. Is there anything I can use that would avoid using that tool?
What Parse error?
The CREATE will fail due to double INTEGER in MyColId and VARCHAR(max) in ExceptionOutput, it's an unknown datatype in Teradata.

(No such column).. But, it's there

Here's my tables:
CREATE TABLE IF NOT EXISTS message_threads (
thread_id integer primary key autoincrement NOT NULL,
user_id integer NOT NULL,
last_checked timestamp NOT NULL DEFAULT '0',
last_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
)
CREATE TABLE IF NOT EXISTS messages (
message_id integer primary key autoincrement NOT NULL,
thread_id integer NOT NULL,message_type integer NOT NULL DEFAULT '0',
message_content varchar(500) NOT NULL,
message_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
sent integer NOT NULL DEFAULT '0'
);
Here's the error I'm getting:
Could not prepare statement (1 no such column: m.message_date)
Here's the query that I'm using (This query works in MySQL, as I tested it in PHPMyAdmin with a dummy table)
SELECT * FROM messages m, message_threads t
WHERE m.thread_id = t.thread_id
ORDER BY t.last_updated, t.thread_id, m.message_date;
I'm using WebSQL (which I think is SQLite)
FULL WebSQL CODE
$rootScope.database = openDatabase('application.db', '1.0', 'Application database', 1024 * 1024);
$rootScope.database.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS message_threads (thread_id integer primary key autoincrement NOT NULL, user_id integer NOT NULL, last_checked timestamp NOT NULL DEFAULT '0', last_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP);");
tx.executeSql("CREATE TABLE IF NOT EXISTS messages (message_id integer primary key autoincrement NOT NULL,thread_id integer NOT NULL,message_type integer NOT NULL DEFAULT '0',message_content varchar(500) NOT NULL, message_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, sent integer NOT NULL DEFAULT '0');");
});
// Empty messages/threads for testing purposes
$rootScope.database.transaction(function(tx) {
tx.executeSql("DELETE FROM messages;");
tx.executeSql("DELETE FROM message_threads;");
});
$rootScope.database.transaction(function(tx) {
tx.executeSql("INSERT INTO message_threads (user_id) VALUES (?);", [0]);
tx.executeSql("INSERT INTO messages (thread_id, message_content, sent) VALUES (?, ?, ?);", [1, "How are you doing?", 1]);
tx.executeSql("INSERT INTO messages (thread_id, message_content) VALUES (?, ?);", [1, "Good you?"]);
});
$rootScope.database.transaction(function(tx) {
tx.executeSql("SELECT * FROM messages m, message_threads t WHERE m.thread_id = t.thread_id ORDER BY t.last_updated, t.thread_id, m.message_date", [], function(tx, rs) {
console.log(JSON.stringify(rs));
}, function(tx, err) {
alert("Error: " + err.message);
});
});
I should add that the query works fine using DBBrowser for SQLite.
Chances are that your database has an older version of the table without the column.
CREATE TABLE IF NOT EXISTS only creates a new table with the given specification if a table by the same name does not exist. It does nothing to make sure the columns are there.
To fix it, either remove your database file, or use DROP TABLE to get rid of your old tables before recreating them.
I have created a fresh new sqlite database and tested both of your create queries using the plain command line sqlite.exe version 3.8.0.2 on windows 7. There were no errors.
Then I have used SQLiteStudio Version 3.0.6 and entered some dummy data and executed your select query. Again no issues.
The tools that I have used can only deal with sqlite. Therefore, it seems to me that there are some configuration issues with your tools.

Resources