How to store a .docx file into MySQL - and open it? - mysql

MySQL
CREATE TABLE document_control (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
person VARCHAR(40),
dateSent TIMESTAMP,
fileAttachment MEDIUMBLOB
);
MySQL Insert record query
INSERT INTO DOCUMENT_CONTROL (fileattachment) values (load_file('C:\Users\<user>\Desktop\test.docx'));
Retrieving record
If I run this query here: SELECT * FROM document_control - Everything is null - even after the insert query above.
Question
Why is the values null? and also.. how can I properly store a .docx file into MySQL and open the file?

You need to look into SQL blob data type
You could also read the file as bytes, convert it into a string or base64 encoding or something, and then save that as string in database.
You could also choose to save the file-reference (file path of file) to refer to it.

Related

Error when inserting timestamp data from CSV into a Redshift table column which is of timestamp data type

I am trying to insert data from an UTF-8 encoded CSV file into Redshift database but I get the error when attempting to insert timestamp into a column which has timestamp data type.
Here's a sample CSV:
employeeId,employeeDept,employeeName,shiftStartTime,shiftEndTime,onPremises
KL214691,John Smith,operations,2023-01-17 09:01:34,2023-01-17 16:52:41,1
KL214692,Samantha Kennedy,operations,2023-01-17 08:31:54,2023-01-17 16:09:10,1
Here's a sample table DDL:
create table historical_metrics_agent_status_time_on_status
(
employeeid varchar(10),
employeename varchar(100),
employeedept varchar(50),
shiftstarttime timestamp encode az64,
shiftendtime timestamp encode az64,
onpremises boolean,
importdatetime timestamp encode az64
)
sortkey (employeeid);
The error message shows that there's an invalid digit - on position 4 in column shiftstarttime which has raw field value 2023-01-17 09:01:34. It looks like it's not reading timestamp from CSV file properly. Is there something I'm missing in CSV?
Check stl_load_errors for the exact row that is failing. My guess is that one of the VARCHAR columns has a comma (,) in it and is throwing off the alignment of the CSV to table columns. Like if one of the names is entered as “Smith, Joe”.

inner join two datasets but return nothing without any error (date format issue)?

I'm new to SQL, currently I'm doing a task about join two datasets, one of the dataset was created by myself, here's the query I used:
USE `abcde`;
CREATE TABLE `test_01`(
`ID` varchar(50) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`NUMBER01` bigint(20) NOT NULL DEFAULT '0',
`NUMBER02` bigint(20) NOT NULL,
`date01` date DEFAULT NULL,
PRIMARY KEY (`ID`, `date01`))
Then I load the data from a csv file to this table, the csv file looks like this:
ID NUMBER01 NUMBER02 DATE01
aaa=ee 12345678 235896578 **2009-01-01T00:00:00**
If I query this newly-created table, it looks like this(the format of the 'DATE01' changes):
ID NUMBER01 NUMBER02 DATE01
aaa=ee 12345678 235896578 **2009-01-01**
Another dataset, I queried and exported to a csv file, the format of the date01 column is like 01/12/1979 and in SQL the format looks like 1979-12-01.
I also usedselect * from information_schema.columns to check the datatype of the columns I need to join, for the newly-created dataset:
The date column for another dataset is:
The differences are:
1. The format of the date column in csv appears different
2. The COLUMN_DEFAULT are different, one is 0000-00-00, another one is NULL.
I wonder the reason why I got empty output is probably because the difference in the 'date' format, but I'm not sure how to make them the same so that I can get something in the output, can someone gave me some hint? Thank you.
the format of the 'DATE01' changes
Of course, DATE datatype does not contain timezone info/component.
I wonder the reason why I got empty output is probably because the difference in the 'date' format
If input value have some disadvantage (like wrong data format) than according value is truncated or is set to NULL. See - you must obtain a bunch of warnings during the importing similar to "truncate incorrect value".
If the date field in CSV have wrong format then you must use intermediate user-defined variable for accepting raw value, and apply proper converting expression to it in SET clause. Like
LOAD DATA INFILE ...
INTO TABLE tablename (field1, ..., #date01)
SET date01 = STR_TO_DATE(#date01, '%d/%m/%Y');

Insert data from CSV in one column and custom input in other columns with LOAD DATA INFILE in MySQL?

I have a pre-defined table (with data) in a MySQL database with 3 columns:
brandId INT AUTO_INCREMENT,
brand CHAR,
insertDateTime DATETIME
And I have a list of brands stored in a csv file (10,000 rows).
I want to insert the brands into the table as new rows, with insertDateTime shows the date time of the insertion.
I know I can use LOAD DATA INFILE to load the brands from the csv, and I can use the NOW() function to compute insert datetime as we go, but how to combine them in one query?
You can use the SET clause of LOAD DATA to provide values that do not come from the input file.
Consider the following syntax:
LOAD DATA INFILE 'myfile.csv'
INTO TABLE mytable (brand)
SET insertDateTime = NOW();

golang - mysql driver - database functions

I have created a struct to store spatial types and I have created a scan function to help query rows in my database. I am having issues inserting this type.
I can insert data using the following sql;
INSERT INTO 'table' ('spot') VALUES (GeomFromText('POINT(10 10)'));
If I use Value interface in database/sql/driver;
type Value interface{}
Value is a value that drivers must be able to handle. It is either nil or an instance of one of these types:
int64
float64
bool
[]byte
string [*] everywhere except from Rows.Next.
time.Time
And use this code;
func (p Point) Value() (driver.Value, error) {
return "GeomFromText('" + p.ToWKT() + "')", nil
}
I end up with the following sql statement going to the database;
INSERT INTO 'table' ('spot') VALUES ('GeomFromText('POINT(10 10)')');
The issue being that the function GeomFromText is in quotes. Is there a way to avoid this scenario? I am using gorm and trying to keep raw sql queries to a minimum.
The mysql type being used on the database end is a point.
Please see the two urls below where the concept was poached from
Schema
-- http://howto-use-mysql-spatial-ext.blogspot.com/
create table Points
( id int auto_increment primary key,
name VARCHAR(20) not null,
location Point NOT NULL,
description VARCHAR(200) not null,
SPATIAL INDEX(location),
key(name)
)engine=MyISAM; -- for use of spatial indexes and avoiding error 1464
-- insert a row, so we can prove Update later will work
INSERT INTO Points (name, location, description) VALUES
( 'point1' , GeomFromText( ' POINT(31.5 42.2) ' ) , 'some place');
Update statement
-- concept borrowed from http://stackoverflow.com/a/7135890
UPDATE Points
set location = PointFromText(CONCAT('POINT(',13.33,' ',26.48,')'))
where id=1;
Verify
select * from points;
(when you open the Value Editor to see the blob, the point is updated)
So, the takeaway is to play with the concat() inside of the update statement.

How to handle mysql #1062 - Duplicate entry error when creating a large table

I am working on a table having around 5 million records. I'm loading records from a csv file.
There is a unique column, url.
While inserting, if the url is already in the table, I want to make a change in the new url value and then do the insertion.
Example:
try inserting a record with a url of "book". If "book" already exists, the new record should have a url of "book-1" (then "book-2" and so on)
result: the url values "book-1","book-2"... are in the table in addition to the initial value book
I have figured out that there are 2 ways to do so.
before inserting each record: check whether the url value already exists; if it does then make the required changes in the new url value and insert. I am afraid that this will result in a poor performance.
insert records without checking if the url value already exists. If url value already exists handle the "mysql #1062 - Duplicate entry error" and make the required changes in the url value; retry the insertion.
Is this possible? If so, how?
If this is an one-off problem, I'd like to recommend an ad-hoc MySQL solution:
If your table isn't MyISAM, convert to MyISAM.
Temporarily create an auto_increment integer column named
url_suffix.
Temporarily delete the unique constraint on the url column.
Create the multiple-column index (url, url_suffix) and ensure that there are no other indexes that use url_suffix.
Insert all of your rows, allowing duplicate URLs. You'll notice that the auto_increment url_suffix column is keyed on the url now. So, the first particular url will have url_suffix of 1 and the next 2, and so on.
Do an update like the following, then delete your temporary url_suffix column and put your unique constraint back.
Query to update all the rows:
UPDATE urls
SET url = if (url_suffix = 1, url, CONCAT(url, '-', url_suffix - 1))
In fact, you could skip step 6, keep the auto_increment field so you could easily add duplicate URLs in the future, and simply fetch your URLs like this:
SELECT (if (url_suffix = 1, url, CONCAT(url, '-', url_suffix - 1))) AS url
FROM urls
Your data would look something like this:
url url_suffix
---------------------------
that 1
that 2
this 1
this 2
this 3
those 1
You have the problem here that a simple trigger will prove inefficient when inserting due to the fact that you are saying they will go from 'book' to 'book-1' 'book-2' etc. The easiest way to do this would be to have a new column which contains a numeric value defaulting to 0. This could be done in a stored procedure i.e.
CREATE PROCEDURE `insertURL`(inURL VARCHAR(255))
BEGIN
DECLARE thisSuffix INT UNSIGNED DEFAULT 0;
// We have to get this ID first, as MySQL won't let you select from the table you are inserting to
SELECT COALESCE(MAX(url_suffix)+1,0) INTO thisSuffix FROM urls WHERE url_column = inURL;
// Now the ID is retrieved, insert
INSERT INTO urls (
url_column,
url_suffix
) VALUES (
inURL,
thisSuffix
);
// And then select the generated URL
SELECT IF(thisSuffix>0,CONCAT(inURL,'-',thisSuffix),inURL) AS outURL;
END
Which is then invoked using
CALL insertURL('book');
And will then return 'book' if the suffix = 0, or 'book-1' if it's got a suffix greater than 0.
For purposes of testing my table design was
CREATE TABLE `urls` (
`url_column` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`url_suffix` tinyint(3) UNSIGNED NOT NULL ,
PRIMARY KEY (`url_column`, `url_suffix`)
);