I am trying to save an URL in mysql db and get it back in my application. It gets saved properly.
http://i.>/00/s/NTAwWDUwMA==/$(KGrHqZHJC4E8fW,EPnUBPN1zoBtIQ~~60_1.JPG?set_id=8800005007
but while retrieving, all the '.' operators in the URL gets replaced by
http://i�domain�com/00/s/NTAwWDUwMA==/$�KGrHqZHJC4E8fW�EPnUBPN1zoBtIQ~~60_1�JPG?set_id=8800005007
Is there a way to remove those special characters. Attaching the create script for the table..
Im getting the url from the result set.
rs.getString(image)
delimiter $$
CREATE TABLE `livedeals` (
`ItemID` bigint(20) NOT NULL,
`category` varchar(200) CHARACTER SET latin1 NOT NULL,
`deal_like` int(4) NOT NULL,
`deal_dislike` int(4) NOT NULL,
`image` varchar(200) CHARACTER SET armscii8 COLLATE armscii8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
any help would be helpful.
thanks.
If for some reason you can't change the character set of the table, then you could get that field the following way:
SELECT CAST(image AS CHAR CHARACTER SET utf8) AS image2 FROM livedeals
Related
I use MySQL 5.7, but I do not know how to config it to display Vietnamese correctly.
I have set
CREATE DATABASE brt
DEFAULT CHARACTER SET utf8 COLLATE utf8_vietnamese_ci;
After that I used "LOAD DATA LOCAL INFILE" to load data written by Vietnamese into the database.
But I often get a result with error in Vietnamese character display.
For the detailed codes and files, please check via my GitHub as the following link
https://github.com/fivermori/mysql
Please show me how to solve this. Thanks.
As #ysth suggests, using utf8mb4 will save you a world of trouble going forward. If you change your create statements to look like this, you should be good:
CREATE DATABASE `brt` DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
USE `brt`;
DROP TABLE IF EXISTS `fixedAssets`;
CREATE TABLE IF NOT EXISTS `fixedAssets` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` varchar(250) UNIQUE NOT NULL DEFAULT '',
`name` varchar(250) NOT NULL DEFAULT '',
`type` varchar(250) NOT NULL DEFAULT '',
`createdDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE INDEX `idx_fa_main` ON `fixedAssets` (`code`);
I've tested this using the data that you provided and get the expected query results:
name
----------------------------------------------------------------
Mould Terminal box cover BN90/112 612536030 39 tháng
Mould W2206-045-9911-VN #3 ( 43 tháng)
Mould Flange BN90/B5 614260271 ( 43 tháng)
Mould 151*1237PH04pC11 ( 10 năm)
Transfer 24221 - 2112 ( sửa chữa nhà xưởng Space T 07-2016 ) BR2
Using the utf8mb4 character set and utf8mb4_unicode_ci collation is usually one of the simpler ways to ensure that your database can correctly display everything from plain ASCII to modern emoji and everything in between.
I'm trying to create a dump file from an MSAccess database to import in to a MySQL database. I can create the dump file, but when I try to import it I get this error Incorrect string value: '\xA325- R...' for column...
I'm not really sure what it means or how to fix it. I know the dump partially works as other tables and data get imported. The import gets to this error and then stops
I've tried setting this DEFAULT CHARACTER SET = utf8; on the table
I've also tried this on the column CHARACTER SET utf8 COLLATE utf8_unicode_ci
Here's an example from my dump file
CREATE TABLE Agent_Table(
AgentID INT NOT NULL AUTO_INCREMENT ,
Agent VARCHAR(255) ,
Archive VARCHAR(255) ,
AgentEmail VARCHAR(255) ,
AgentMobile VARCHAR(255) ,
PRIMARY KEY (`AgentID`)
)
I also tried
CREATE TABLE Agent_Table(
AgentID INT NOT NULL AUTO_INCREMENT ,
Agent VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
Archive VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci ,
AgentEmail VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
AgentMobile VARCHAR(255)CHARACTER SET utf8 COLLATE utf8_unicode_ci ,
PRIMARY KEY (`AgentID`)
)
DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
I've tried a number of the utf8 options as well and still no joy
Nothing seems to work.
I'm writing a dump file because I've exhausted all other methods to try and to import/export the data
All the methods/tools I have tried all fail with some form of ODBC error I'd list them here but there are so many as I have been at this all day.
If anyone knows how to fix this problem or how else I can import this data I would appreciate it
UPDATE
The problems are £ and ` and ’ that I have seen so far. If I do a find and replace of those chars they no longer throw up errors. But it seems no matter what encoding I set the database to it just does not like these characters during the import.
I'm trying to save a string of all 4-byte characters to a MySQL utf8mb4 column.
UPDATE `uga_libsteam`.`group_history` SET `source_name`='𝓔𝓶𝓹𝓻𝓮𝓼𝓼' WHERE `group_id`='103582791430024497' and`history_id`='1655';
The characters are as follows.
http://www.fileformat.info/info/unicode/char/1d4d4/index.htm
http://www.fileformat.info/info/unicode/char/1D4F6/index.htm
http://www.fileformat.info/info/unicode/char/1D4F9/index.htm
http://www.fileformat.info/info/unicode/char/1D4FB/index.htm
http://www.fileformat.info/info/unicode/char/1D4EE/index.htm
http://www.fileformat.info/info/unicode/char/1D4FC/index.htm
However, when I run this query, I receive this error.
Executing:
UPDATE `uga_libsteam`.`group_history` SET `source_name`='𝓔𝓶𝓹𝓻𝓮𝓼𝓼' WHERE `group_id`='103582791430024497' and`history_id`='1655';
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1366: 1366: Incorrect string value: '\xF0\x9D\x93\x94\xF0\x9D...' for column 'source_name' at row 1
SQL Statement:
UPDATE `uga_libsteam`.`group_history` SET `source_name`='𝓔𝓶𝓹𝓻𝓮𝓼𝓼' WHERE `group_id`='103582791430024497' and`history_id`='1655'
Here is my schema. Title is deprecated and eventually I'll be removing it. Previously, source_name and target_name were both utf8/utf8_unicode_ci, but I was able to fix that last night.
CREATE TABLE `group_history` (
`group_id` bigint(20) unsigned NOT NULL,
`history_id` bigint(20) unsigned NOT NULL,
`type_id` tinyint(2) DEFAULT NULL,
`title` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`display_date` datetime DEFAULT NULL,
`year_offset` tinyint(2) unsigned DEFAULT NULL,
`month` tinyint(2) unsigned DEFAULT NULL,
`day` tinyint(2) unsigned DEFAULT NULL,
`time` time DEFAULT NULL,
`source_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`source_steam_id` bigint(20) DEFAULT NULL,
`target_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`target_steam_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`group_id`,`history_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Why is the database rejecting characters that should be legal to input? Do I have to switch to UTF-32 for this to work?
Previously, source_name and target_name were both utf8/utf8_unicode_ci, but I was able to fix that last night.
I guess you tried to fix the charset by changing the table's default charset.
ALTER TABLE group_history DEFAULT CHARSET utf8mb4;
This does NOT change the charset of existing columns of the table, it only changes the table's default charset, which will be used only if you subsequently add new columns. Existing columns are still encoded with the old charset, utf8.
To convert the existing columns, you must use:
ALTER TABLE group_history CONVERT TO CHARACTER SET utf8mb4;
This will rewrite all the existing string columns using the new character set, and subsequently allow you to insert 4-byte character values.
Thanks to this answer, I had to run this before inserts into my database would work...
SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
So good news is I now have inserts into my database working, bad news is I'm writing this for a PHP application and PDO isn't working and is saving ??????? to the database.
Edit: Thanks to this question, I figured out the solution to my PHP question as well. This is what my code looked like before:
$dsn = "mysql:host=$this->hostname;port=$this->port;dbname=$this->database;charset=utf8mb4";
try {
$this->pdo = new \PDO($dsn, $this->username, $this->password, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
$this->pdo->exec('SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;');
} catch (\PDOException $e) {
die($e->getMessage());
}
This is what I had to change it to:
$dsn = "mysql:host=$this->hostname;port=$this->port;dbname=$this->database;charset=utf8mb4";
try {
$this->pdo = new \PDO($dsn, $this->username, $this->password, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'));
} catch (\PDOException $e) {
die($e->getMessage());
}
I am trying to add X to some table in my DB, but I am getting this error. Even if X doesn't exist in the table, it say it's there. Although X is added to the DB, I want to get rid of this error. I don't know if it's relevant at all, but I'm using Mysqli's prepared statements and this error is printed using $statement->errno." ".$statement->error. Could someone explain this to me? Thanks.
UPDATE: this is the code: X = USER_USERNAME
$stmt = $mysqli->prepare("INSERT INTO USERS (USER_USERNAME, USER_EMAIL, USER_BIRTHDAY, USER_PASSWORD, USER_SALT, USER_IP, USER_ACTIVATION_CODE) VALUES (?,?,?,?,?,INET_ATON(?),?)");
$stmt->bind_param('sssssss',$username,$email,$date,$hashed_password,$salt,$IP,$activation_code);
$stmt->execute();
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
else {
echo "ok";
}
SHOW CREATE TABLE USERS:
CREATE TABLE `USERS` (
`USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`USER_FIRSTNAME` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`USER_LASTNAME` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`USER_USERNAME` varchar(30) CHARACTER SET utf8 DEFAULT NULL,
`USER_PASSWORD` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
`LEVEL_ID` int(11) NOT NULL,
`USER_BIRTHDAY` date DEFAULT NULL,
`USER_EMAIL` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`USER_GENDER` enum('M','W','U') CHARACTER SET utf8 DEFAULT NULL,
`USER_COUNTRY` varchar(30) CHARACTER SET utf8 DEFAULT NULL,
`USER_LOCATION` varchar(30) CHARACTER SET utf8 DEFAULT NULL,
`USER_ADDRESS` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`USER_HOUSENUMBER` varchar(8) CHARACTER SET utf8 DEFAULT NULL,
`USER_AVATAR` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`USER_REGISTRATION_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`USER_ACTIVATION_DATE` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`USER_STATUS` enum('REGISTERED','ACTIVE','BANNED','NONACTIVE') CHARACTER SET utf8 DEFAULT NULL,
`USER_BANNED_DATE` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`USER_LATEST_LOGIN` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`USER_EXP` bigint(20) DEFAULT NULL,
`USER_DESCRIPTION` text CHARACTER SET utf8,
`USER_ACTIVATION_CODE` varchar(32) CHARACTER SET utf8 NOT NULL DEFAULT '0',
`USER_SALT` varchar(15) CHARACTER SET utf8 NOT NULL,
`USER_IP` int(10) NOT NULL,
`USER_REMEMBER_KEY` varchar(32) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`USER_ID`),
UNIQUE KEY `USER_USERNAME` (`USER_USERNAME`),
UNIQUE KEY `USER_EMAIL` (`USER_EMAIL`),
KEY `LEVEL_ID` (`LEVEL_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
that's because you are trying to insert or update a value violating a constraint (like PK or UK), for example
if your Table have ID an it's a PK or a Unique Key, you can only have ONE UNIQUE VALUE... it's not possible no repeated....
with a Unique Key you can have Null... but never repeat values in the same column, because you will be breaking the constraint... for more information take a look to http://dev.mysql.com/doc/refman/5.0/es/constraints.html
You already defined a Primary or Unique Key, but again you tried to insert the same value. That time since already there exists a same value, it is not possible to have a redundant value for the primary key or unique key.
It is something like this. Consider you have a table with id and name, with id as primary key. You insert first row as:
INSERT INTO `users` (`id`, `name`) VALUES (1, 'Praveen');
INSERT INTO `users` (`id`, `name`) VALUES (1, 'Kumar');
The second query violates the id uniqueness.
Solution: Try to truncate the table and then run the query again, if it is possible.
Some steps to try
Check for other fields with unique constraints being violated too.
See what query is getting generated.
Put the same query in phpMyAdmin and check.
If you are getting that error then X does exist in the table. Apparently you have not noticed it, but it's there. (Check for things like trimmed spaces, and case sensitivity.)
If you don't get the error in phpMyAdmin then the query you think you are sending is not actually the query you are sending. Most likely you are sending blank for the field, and after you do it once, all further query are using the identical blank value.
Easiest way to check is delete everything from the table (copy it elsewhere first if you want). Then run the query and see what actually got inserted vs. what you expected to get inserted.
You are probably trying to insert a record with the ID (or some other field) 1 set, while such a record already exists in the table. The field that is the primary key must have a unique value for each record.
You could try a simple error check to avoid this error:
$rows = mysql_query("SELECT X from tablenme WHERE X = Y;");
if(mysql_num_rows($rows) > 0){
echo 'Error Messege';
}else{
insert...
}
where X is the primary key or foregin key column and Y is the value to be inserted in that column.
EDIT: Some of the problems that might have caused the problem may be that you have not specified certain columns that can't be null in you insert statement.
Like LEVEL_ID and USER_REMEMBER_KEY. They are set to NOT NULL but the values are not being inserted using the insert statement.
I also don't understand why you are inserting 'sssssss' when the first column is USER_USERNAME and its corresponding value is $username. Do check that too.
SQL Fiddle
I have a search function, I want it to be case insensitive, including characters like éüò etc.
So, i transform the input to uppercase before querying the database. But MySQL doesn't convert the accented characters right.
SELECT * FROM items WHERE UPPER(description) = $input
I have MySQL 5.1.32, i have tried different collations but none seem to work right. Same with LOWER().
CREATE TABLE `items` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`description` text CHARACTER SET utf8 COLLATE utf8_bin,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=187 DEFAULT CHARSET=utf8"
The description field contains 'hellö'. Mysql converts it to 'HELLö'. I want 'HELLÖ'.
This works for me:
CREATE TABLE items (id INT NOT NULL, name VARCHAR(100) COLLATE UTF8_GENERAL_CI) ENGINE=InnoDB;
INSERT
INTO items
VALUES (1, 'Eyjafjallajökull');
SELECT *
FROM items
WHERE name = 'EYJAFJALLAJOKULL';
--
1 Eyjafjallajökull
SELECT UPPER('Eyjafjallajökull')
FROM items;
--
EYJAFJALLAJÖKULL