I'm trying to import a csv to a mysql table in my db but I keep getting this error:
Unknown column '' in 'field list'
INSERT INTO `job_budget_report` (`Job_Num`, `Description`, `EngWeekly`, `EngTTD`, `EngBudget`, `DesignWeekly`, `DesignTTD`, `DesignBudget`, `DetailsWeekly`, `DetailsTTD`, `DetailsBudget`, `EjectionWeekly`, `EjectionTTD`, `EjectionBudget`, `CarbonEDMWeekly`, `CarbonEDMTTD`, `CarbonEDMBudget`, `GdrillBMWeekly`, `GdrillBMTTD`, `GdrillBMBudget`, `CNCWeekly`, `CNCTTD`, `CNCBudget`, `MMQCOtherWeekly`, `MMQCOtherTTD`, `MMQCOtherBudget`, `SpottingWeekly`, `SpottingTTD`, `SpottingBudget`, `SpotPLWeekly`, `SpotPLTTD`, `SpotPLBudget`, `HWWeekly`, `HWTTD`, `HWBudget`, `TotalMach`, `TotalOther`, `TotalOFMach`, ``) VALUES
('1000', 'description text here', '0', '0', '0', NULL, '0', '0', NULL, NULL, '0', NULL, '0', NULL, '0', '0', '0', '0', '0', '0', '0', '0', '0', '20.5', '0', '0', '0', '0', '0', '18.5', '40', '0', '0', '0', '0', '0', '0', '0', '');
Table Structure:
http://imgur.com/rFLw4c7
Now I realize that theres extra empty fields in that query but how do I remove them from my csv ?
Here's what my csv looks like when I open it in sublime text:
Job_Num;Description;EngWeekly;EngTTD;EngBudget;DesignWeekly;DesignTTD;DesignBudget;DetailsWeekly;DetailsTTD;DetailsBudget;EjectionWeekly;EjectionTTD;EjectionBudget;CarbonEDMWeekly;CarbonEDMTTD;CarbonEDMBudget;GdrillBMWeekly;GdrillBMTTD;GdrillBMBudget;CNCWeekly;CNCTTD;CNCBudget;MMQCOtherWeekly;MMQCOtherTTD;MMQCOtherBudget;SpottingWeekly;SpottingTTD;SpottingBudget;SpotPLWeekly;SpotPLTTD;SpotPLBudget;HWWeekly;HWTTD;HWBudget;TotalMach;TotalOther;TotalOFMach
5710;GM K2XL Bracket FRT 5/D Belt
R;0;0;0;;0;0;;;0;;0;;0;0;0;0;0;0;0;0;0;20.5;0;0;0;0;0;18.5;40;0;0;0;0;0;0;0;0;0;457.56;328.8;;;;;
Sorry I don't know how to structure this nicer, I'll post an image of one of the rows in the excel file before and after I clear formatting as well.
Before:
http://imgur.com/f6jRBZR
After:
http://imgur.com/M1mQb8V
Is there a more efficient way to do this ?
Use fgetcsv() to read, clean and insert into your table. - http://www.php.net/fgetcsv
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
INSERT INTO `job_budget_report`
( `Job_Num`
, `Description`
, `EngWeekly`
, `EngTTD`
, `EngBudget`
, `DesignWeekly`
, `DesignTTD`
, `DesignBudget`
, `DetailsWeekly`
, `DetailsTTD`
, `DetailsBudget`
, `EjectionWeekly`
, `EjectionTTD`
, `EjectionBudget`
, `CarbonEDMWeekly`
, `CarbonEDMTTD`
, `CarbonEDMBudget`
, `GdrillBMWeekly`
, `GdrillBMTTD`
, `GdrillBMBudget`
, `CNCWeekly`
, `CNCTTD`
, `CNCBudget`
, `MMQCOtherWeekly`
, `MMQCOtherTTD`
, `MMQCOtherBudget`
, `SpottingWeekly`
, `SpottingTTD`
, `SpottingBudget`
, `SpotPLWeekly`
, `SpotPLTTD`
, `SpotPLBudget`
, `HWWeekly`
, `HWTTD`
, `HWBudget`
, `TotalMach`
, `TotalOther`
, `TotalOFMach`
, `` -- clearly, this is the offending line!
) VALUES
('1000'
, 'description text here'
, '0'
, '0'
, '0'
, NULL
, '0'
, '0'
, NULL
, NULL
, '0'
, NULL
, '0'
, NULL
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, '20.5'
, '0'
, '0'
, '0'
, '0'
, '0'
, '18.5'
, '40'
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, '0'
, ''); -- and its corresponding value
Related
I am trying to create a table on my database. this is the error
#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 ') NOT NULL , `ctc` DOUBLE(5) NOT NULL , `ref` VARCHAR(50) NOT NULL , `date` D' at line 1
My query:
CREATE TABLE `job`.`form_details` (
`email_id` VARCHAR(50) NOT NULL ,
`name` VARCHAR(50) NOT NULL ,
`number` VARCHAR(14) NOT NULL ,
`city` VARCHAR(50) NOT NULL ,
`skill` VARCHAR(50) NOT NULL ,
`qualification` VARCHAR(50) NOT NULL ,
`position` VARCHAR(50) NOT NULL ,
`exp` DOUBLE(5) NOT NULL ,
`ctc` DOUBLE(5) NOT NULL ,
`ref` VARCHAR(50) NOT NULL ,
`date` DATE NOT NULL ,
`time stamp` TIMESTAMP(30) NOT NULL ) ENGINE = InnoDB;
A double's precision is specified by two arguments - (M, D) - M digits in total and D digits after the decimal point. A timestamp's precision must be no greater than 6. So:
CREATE TABLE `job`.`form_details` (
`email_id` VARCHAR(50) NOT NULL ,
`name` VARCHAR(50) NOT NULL ,
`number` VARCHAR(14) NOT NULL ,
`city` VARCHAR(50) NOT NULL ,
`skill` VARCHAR(50) NOT NULL ,
`qualification` VARCHAR(50) NOT NULL ,
`position` VARCHAR(50) NOT NULL ,
`exp` DOUBLE(5, 2) NOT NULL , -- Two arguments for the double's precision
`ctc` DOUBLE(5, 2) NOT NULL , -- Here too
`ref` VARCHAR(50) NOT NULL ,
`date` DATE NOT NULL ,
`time stamp` TIMESTAMP(6) NOT NULL -- Timestamp precision capped at 6
) ENGINE = InnoDB
Here is my code
CREATE TABLE IF NOT EXISTS `Employees` (
`EmployeeID` VARCHAR( 6 ) NOT NULL DEFAULT '',
`FullName` VARCHAR( 35 ) NOT NULL DEFAULT '',
`EntryYMD` DATE NOT NULL DEFAULT '0000-00-00',
`PartTime` TINYINT( 3 ) NOT NULL DEFAULT '100',
`Mo` TINYINT( 1 ) NOT NULL DEFAULT '8',
`Tu` TINYINT( 1 ) NOT NULL DEFAULT '8',
`We` TINYINT( 1 ) NOT NULL DEFAULT '8',
`Th` TINYINT( 1 ) NOT NULL DEFAULT '8',
`Fr` TINYINT( 1 ) NOT NULL DEFAULT '8',
`Sa` TINYINT( 1 ) NOT NULL DEFAULT '0',
`Su` TINYINT( 1 ) NOT NULL DEFAULT '0',
`Version` INT( 8 ) NOT NULL DEFAULT '0',
`Changed` SMALLINT( 1 ) NOT NULL DEFAULT '1',
`Time_Stamp` TIMESTAMP( 14 ) NOT NULL ,
PRIMARY KEY ( `EmployeeID` ) ,
) ENGINE = MYISAM;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(14) NOT NULL,
PRIMARY KEY (EmployeeID),
)ENGINE =MyISAM' at line 15
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Looking to fix the syntax error
Error Message:
force int(9) NOT NULL default '0', perm tinyint(1) NOT NULL default
'0', act' at line 11
My Table Syntax:
CREATE TABLE `ttp_sites` (
siteid int( 4 ) NOT NULL AUTO_INCREMENT ,
wname char( 64 ) NOT NULL default '',
email char( 64 ) NOT NULL default '',
siteurl char( 255 ) NOT NULL default '',
sitename char( 128 ) NOT NULL default '',
furl char( 255 ) NOT NULL default '',
icqnumb char( 20 ) default '',
icqname char( 20 ) default '',
sent int( 9 ) NOT NULL default '0',
FORCE int( 9 ) NOT NULL default '0',
perm tinyint( 1 ) NOT NULL default '0',
active tinyint( 1 ) NOT NULL default '0',
manage_type tinyint( 1 ) NOT NULL default '0',
send_ratio int( 4 ) NOT NULL default '0',
PRIMARY KEY ( siteid ) ,
KEY siteurl( siteurl ) ,
KEY sitename( sitename ) ,
KEY active( active )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =1;
The issue with your code is you have used a reserved keyword FORCE , you can use the reserve keyword to create a table column name is by giving the name within single quotes which takes the keyword as a name.
CREATE TABLE `ttp_sites` (
siteid int( 4 ) NOT NULL AUTO_INCREMENT ,
wname char( 64 ) NOT NULL default '',
email char( 64 ) NOT NULL default '',
siteurl char( 255 ) NOT NULL default '',
sitename char( 128 ) NOT NULL default '',
furl char( 255 ) NOT NULL default '',
icqnumb char( 20 ) default '',
icqname char( 20 ) default '',
sent int( 9 ) NOT NULL default '0',
`FORCE` int( 9 ) NOT NULL default '0',
perm tinyint( 1 ) NOT NULL default '0',
active tinyint( 1 ) NOT NULL default '0',
manage_type tinyint( 1 ) NOT NULL default '0',
send_ratio int( 4 ) NOT NULL default '0',
PRIMARY KEY ( siteid ) ,
KEY siteurl( siteurl ) ,
KEY sitename( sitename ) ,
KEY active( active )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT =1;
Sql Fiddle
P.s : Don't use keyword for naming a column.
Updated Query of your's in the comment
CREATE TABLE ttp_traffic
( siteid int(4) NOT NULL default '0',
ipaddr char(25) NOT NULL default '',
click int(3) NOT NULL default '0',
prox int(1) NOT NULL default '0',
refer char(255) NOT NULL default '',
datev timestamp NOT NULL,
KEY siteid (siteid),
KEY datev (datev),
KEY click (click),
KEY ipaddr (ipaddr) )ENGINE = MYISAM;
Timestamp should not have datatype size that was the error in your code
SQl Fiddle
I have a query I need to run and I have 2 problems.
first here is my query:
INSERT INTO `anzie_oscommerce`.`discount_codes` (
`discount_codes_id` ,
`discount_description` ,
`products_id` ,
`categories_id` ,
`manufacturers_id` ,
`excluded_products_id` ,
`customers_id` ,
`orders_total` ,
`order_info` ,
`exclude_specials` ,
`discount_codes` ,
`discount_values` ,
`minimum_order_amount` ,
`expires_date` ,
`number_of_orders` ,
`number_of_use` ,
`number_of_products` ,
`status`
)
VALUES (
'' , 'GALA 2012', '' , '' , '' , '' , '' , '2', '1', '0', substr(md5(uniqid(rand(), true)), 0, 8), 250, '0.0000', '2013-22-06', '0', '1', '0', '1'
);
The first problem is I need to generate the random code which I am trying to do with the uniqid function I took from php but I know that is not possible. Is there a way to do something similar in sql?
The second problem is I need to run this 250 times to generate 250 different discount codes. Is there a quick way to run an sql multiple times?
For the first problem: it depends on the database you're using. Mysql has a rand() function: https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
For the second: 250 is not many for a database - just putting it in a php for loop would most likely be fine.
I like the answer sonofagun answer. Another trick is to select from a dummy table 250 times.
INSERT INTO `anzie_oscommerce`.`discount_codes` (
`discount_codes_id` ,
`discount_description` ,
`products_id` ,
`categories_id` ,
`manufacturers_id` ,
`excluded_products_id` ,
`customers_id` ,
`orders_total` ,
`order_info` ,
`exclude_specials` ,
`discount_codes` ,
`discount_values` ,
`minimum_order_amount` ,
`expires_date` ,
`number_of_orders` ,
`number_of_use` ,
`number_of_products` ,
`status`
)
(select '' , 'GALA 2012', '' , '' , '' , '' , '' , '2', '1', '0', substr(md5(uniqid(rand(), true)), 0, 8), 250, '0.0000', '2013-22-06', '0', '1', '0', '1' union all
select '' , 'GALA 2012', '' , '' , '' , '' , '' , '2', '1', '0', substr(md5(uniqid(rand(), true)), 0, 8), 250, '0.0000', '2013-22-06', '0', '1', '0', '1' union all
select '' , 'GALA 2012', '' , '' , '' , '' , '' , '2', '1', '0', substr(md5(uniqid(rand(), true)), 0, 8), 250, '0.0000', '2013-22-06', '0', '1', '0', '1' union all
select '' , 'GALA 2012', '' , '' , '' , '' , '' , '2', '1', '0', substr(md5(uniqid(rand(), true)), 0, 8), 250, '0.0000', '2013-22-06', '0', '1', '0', '1' union all
...however many times
);
I am getting a
"Cannot add or update a child row: a foreign key constraint fails
(smarturbia.pois, CONSTRAINT fk_pois_cities1 FOREIGN KEY
(city) REFERENCES cities (id) ON DELETE NO ACTION ON UPDATE NO
ACTION) (1509)"
and i have no idea why.
My tables are generated through Mysql workbench 5.2, if you need the rest of the tables let me know :
-- -----------------------------------------------------
-- Table `smarturbia`.`cities`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`cities` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`cities` (
`id` BIGINT(11) NOT NULL AUTO_INCREMENT ,
`published` VARCHAR(1) NOT NULL DEFAULT '0' ,
`open` VARCHAR(1) NOT NULL DEFAULT '0' ,
`path` VARCHAR(25) NOT NULL DEFAULT 'taxi' ,
`key` VARCHAR(50) NOT NULL ,
`world` VARCHAR(10) NOT NULL DEFAULT 'earth' ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NULL DEFAULT NULL ,
`logo` VARCHAR(250) NULL DEFAULT NULL ,
`footer` VARCHAR(250) NOT NULL DEFAULT '/taxi/assets/img/taxi_smarturbia_image_default.png' ,
`footer_large` VARCHAR(250) NOT NULL ,
`leftpub` VARCHAR(50) NOT NULL ,
`rightpub` VARCHAR(50) NOT NULL ,
`model` VARCHAR(250) NOT NULL ,
`modelxscale` FLOAT NULL DEFAULT '1' ,
`modelyscale` FLOAT NULL DEFAULT '1' ,
`modelzscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelxscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelyscale` FLOAT NULL DEFAULT '1' ,
`wheelmodelzscale` FLOAT NULL DEFAULT '1' ,
`allwheels` VARCHAR(250) NULL DEFAULT NULL ,
`frontleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`frontrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearleftwheel` VARCHAR(250) NULL DEFAULT NULL ,
`rearrightwheel` VARCHAR(250) NULL DEFAULT NULL ,
`axisdistance` FLOAT NOT NULL DEFAULT '2.5' ,
`wheelsdistance` FLOAT NULL DEFAULT '1' ,
`wheelsheight` FLOAT NULL DEFAULT '1' ,
`kms` FLOAT NULL DEFAULT '0' ,
`maxspeed` FLOAT NULL DEFAULT '160' ,
`accel` FLOAT NULL DEFAULT '25' ,
`accelstep` FLOAT NULL DEFAULT '25' ,
`minaccelstep` FLOAT NULL DEFAULT '5' ,
`maxrevspeed` FLOAT NULL DEFAULT '30' ,
`decel` FLOAT NULL DEFAULT '90' ,
`gravity` FLOAT NULL DEFAULT '70' ,
`camheight` FLOAT NULL DEFAULT '5' ,
`camtilt` FLOAT NULL DEFAULT '90' ,
`traildistance` FLOAT NULL DEFAULT '15' ,
`mass` FLOAT NULL DEFAULT '3000' ,
`vehicleagility` FLOAT NULL DEFAULT '0.0005' ,
`suspensionstiffness` FLOAT NULL DEFAULT '0.5' ,
`suspensionrestlength` FLOAT NULL DEFAULT '0.5' ,
`suspensiondamping` FLOAT NULL DEFAULT '-0.15' ,
`suspensiondeltatime` FLOAT NULL DEFAULT '0.25' ,
`turnspeedmin` FLOAT NULL DEFAULT '20' ,
`turnspeedmax` FLOAT NULL DEFAULT '60' ,
`speedmaxturn` FLOAT NULL DEFAULT '5' ,
`speedminturn` FLOAT NULL DEFAULT '50' ,
`steerroll` FLOAT NULL DEFAULT '-1' ,
`rollspring` FLOAT NULL DEFAULT '0.5' ,
`rollclamp` FLOAT NULL DEFAULT '50' ,
`mapiconurl` VARCHAR(250) NULL DEFAULT NULL ,
`vehicleshadow` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclesoundtime` FLOAT NULL DEFAULT '150' ,
`vehiclefastsound` VARCHAR(250) NULL DEFAULT NULL ,
`vehiclefastsoundtime` FLOAT NULL DEFAULT '150' ,
`backgroundsound` VARCHAR(250) NULL DEFAULT NULL ,
`backgroundsoundtime` FLOAT NULL DEFAULT '150' ,
`crashsound` VARCHAR(250) NULL DEFAULT NULL ,
`crashsoundtime` FLOAT NULL DEFAULT '150' ,
`vehicletype` VARCHAR(50) NULL DEFAULT 'car' ,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
INDEX `key` (`key` ASC) )
ENGINE = InnoDB
AUTO_INCREMENT = 153
DEFAULT CHARACTER SET = latin1;
-- -----------------------------------------------------
-- Table `smarturbia`.`pois`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `smarturbia`.`pois` ;
CREATE TABLE IF NOT EXISTS `smarturbia`.`pois` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT ,
`city` BIGINT(11) NOT NULL ,
`name` VARCHAR(50) NOT NULL ,
`description` VARCHAR(250) NOT NULL ,
`lat` DOUBLE NOT NULL ,
`lon` DOUBLE NOT NULL ,
`heading` FLOAT NULL DEFAULT '0' ,
PRIMARY KEY (`id`) ,
INDEX `fk_pois_cities1_idx` (`city` ASC) ,
CONSTRAINT `fk_pois_cities1`
FOREIGN KEY (`city` )
REFERENCES `smarturbia`.`cities` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 408
DEFAULT CHARACTER SET = latin1;
Insert the values in the table in which your primary key is defined..Then try to insert in the table in which you having the foreign key...
I think you are inserting the values in the table of foreign key without inserting in the table having primary key..
It is not recommended but you can try this
set foreign_key_checks=0;
To enable foreign key checking
set foreign_key_checks=1;