HattrickNZ HattrickNZ - 6 months ago 16
SQL Question

importing a *.sql file into mysql from a script + handling password

I want to import into mysql from a script.

Can I do it like this:

mysql -u username -p password dbname < employee.sql


If i leave the
password
out in the command will work, I just have to enter the password after entering the command.

Since I am looking to do it from a script I was hoping to do
mysql -u username -p password dbname < employee.sql
including the password. Can anyone advise if this is the way to do this or am I completely wrong?

employee.sql
look like this:

CREATE TABLE IF NOT EXISTS xyz (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
employee_name varchar(255) NOT NULL COMMENT 'employee name',
employee_salary double NOT NULL COMMENT 'employee salary',
employee_age int(11) NOT NULL COMMENT 'employee age',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=64 ;
---- have to change it as above not working -- the below works -- might be different when reading in from file??


INSERT INTO xyz (id, employee_name, employee_salary, employee_age) VALUES
(1, 'Tiger Nixon', 320800, 61),
(2, 'Garrett Winters', 170750, 63),
(3, 'Ashton Cox', 86000, 66),
(4, 'Cedric Kelly', 433060, 22),
...

(56, 'Michael Bruce', 183000, 29),
(57, 'Donna Snider', 112000, 27);

Answer

You don't want a space between -p and the password. so it should be

mysql -u username -ppassword dbname < employee.sql