I use PHP to parse big CSV file and generate a SQL file that contains INSERT requests.
But at the beginning of the file, I also put a DELETE statement to clear the database before.
My file looks like something like this :
DELETE FROM `my_table` WHERE id IN (<list>) ;
INSERT INTO `my_table` .... (lot of values);
The lines are correctly inserted but the old ones are not deleted. I tried the delete request in PHPMyAdmin : it works. So the issue comes from the way I run the sql file.
I use the exec method in PHP :
$command = "mysql.exe -u user -pPassword my_database < sqlfile.sql";
It seems that this line, using the left chevron, works fine for INSERT statements, but not for DELETE ones.
Any idea to solve that ?
Thanks a lot :)
Code isn't entirely clear, but judging by your WHERE ... IN clause, you're either manually generating an ID via SELECT and manually incrementing (Usually bad), or you're simply doing it wrong. I see 2 options based on these scenarios:
TRUNCATE TABLE `my_table`
This empties all previous values from said table.
Second, if you were auto-generating ID's, set up the ID as auto-increment and try
# Null corresponds to auto-increment/primary key ID field in MySQL table
INSERT INTO `my_table` ... VALUES (null, value, value)
Related
I am new with PHP development and just wondering if theres a existing function on PHP than duplicate the copy command on phpmyadmin, i know that the query sequence is below, but this is like a long query/code since the table has alot of columns. i mean if phpmyadmin has this feature maybe its calling a build in function?
SELECT * FROM table where id = X
INSERT INTO table (XXX)VALUES(XXX)
Where the information is based from the SELECT query
Note: The id is primary and auto increment.
Here is the copy command on phpmyadmin
i mean if phpmyadmin has this feature maybe its calling a build in function?
There is no built-in functionality in MySQL to duplicate a row other than an INSERT statement of the form: INSERT INTO tableName ( columns-specification ) SELECT columns-specification FROM tableName WHERE primaryKeyColumns = primaryKeyValue.
The problem is you need to know the names of the columns beforehand, you also need to exclude auto_increment columns, as well as primary-key columns, and know how to come up with "smart defaults" for non-auto_increment primary key columns, especially composite keys. You'll also need to consider if any triggers should be executed too - and how to handle any constraints and indexes that may be designed to prevent duplicate values that a "copy" operation might introduce.
You can still do it in PHP, or even pure-MySQL (inside a sproc, using Dynamic SQL) but you'll need to query information_schema to get metadata about your database - which may be more trouble than it's worth.
I'm running a mid-size system (PHP + MySQL) and I have a table stores all user's friends (this information comes from Facebook). The table now have about 1 million lines.
Every time user logs in, the system DELETEs all user`s friends from this table, and then insert it again using data downloaded from Facebook, so it's coded like this:
1) Run Facebook Query and store results in an object.
2) If query wasn`t successful, end program.
3) DELETE FROM UserFriends WHERE idUser = $idUser
4) INSERT INTO UserFriends (idUser, FriendFacebookId) VALUES
($idUser, $objFB[x]),
($idUser, $objFB[x2]),
($idUser, $objFB[x3])
...
* It generates the query inside a loop, so it INSERT all lines in one query only)
Sometimes (1 in ~2000) running the INSERT statement returns the following error:
1062 : Duplicate Entry for '2030-0202001910' for key 'PRIMARY'
The '2030-0202001910' is the User's ID and the FriendFacebookID... it's always the first set of data in the INSERT statement.
So the question is: I've just deleted everything for this user, the table is supposed to be empty for this user, but then when I try to insert data for this user, it returns me this message. Why this happens and how can I avoid it?
Please note: This program runs hundreds times per hour, and the error occurs only few times... If the same user try to log in again, the program works like expected... So i'm thinking that can be some "delay" between the DELETE command and the data been removed from table... I don't know.
Useful information:Table is InnoDB and I'm not using transactions.
Try this instead of insert: INSERT IGNORE
It is meant to mean 'ignore duplicates' so it never creates the possibility of creating a collision.
See if that works for you.
You could also use INSERT...ON DUPLICATE KEY UPDATE
...or simply: REPLACE
instead of the plain INSERT command
I am running these two statements one right after another in some PHP code.
INSERT INTO input (ID) VALUES('1');
UPDATE input SET `vendor_name` = 'some name' WHERE ID=1
If the database table is empty (meaning I just truncated the table), no data is created, no warnings appear. It's as if I never executed the query.
If I then run just
INSERT INTO input (ID) VALUES('1');
On this exact same clear table, an entry is created no problem.
After that, if I again run the same INSERT/UPDATE querys
INSERT INTO input (ID) VALUES('2');
UPDATE input SET `vendor_name` = 'some name' WHERE ID=2
Then the data is created and the vendor_name set appropriately. What is going on here?? I seem to be misunderstanding something fundamental about inserting data into a database. Can I not execute statements back-to-back like this on an empty table?
To pre-empt the inevitable silly questions like 'does the column exist?', here is some extra notes:
NO warnings ever appear. This really bugs me. The INSERT/UPDATE seems to fail silently. (yes, yes, I have set error reporting on and I checked the logs)
The appropriate columns/tables/databases/permissions exist and are defined (remember the query works fine if there is an empty row in the table)
ID is the primary key. The numbers I used for IDs (1,2, whatever) do not seem to matter. I could just as well reverse them, or use 101 and 102, whatever.
Create table looks like so: CREATE TABLE IF NOT EXISTS $tablename (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY). Columns are added dynamically by users. So right now there's about 100 columns in the table ( keep in mind this shouldn't matter. I'm only trying to update one column, and that update works fine if there is data in the table)
Sneaking a Count(*) query in there reveals that the row is created, but then disappears after the Update statement. (Perhaps the row wasn't completed, or something, and there's a 'make sure the insert is finished' query I need to run?)
Try this code.... for me its works....
INSERT INTO `input` (ID) VALUES('1');
UPDATE `input` SET `vendor_name` = 'some name' WHERE ID=1
I'm using an INSERT ON DUPLICATE KEY statement for my website. It's for creating news items, so I figured I could use the same MySQL command for both creating and updating news items.
However, when I use the following:
INSERT INTO table (id,title,content) VALUES(NULL,"Test","Test");
Instead of creating a new auto increment value it throws an error. However, the command works on my main development server. But not on my laptop. Both versions of MySQL are the same, the only difference being MySQL was installed manually on my server, and with WAMP on my laptop.
Are there any MySQL Variables that could be causing this?
I would suggest using INSERT INTO table (title,content) VALUES("Test","Test");
This will create a new row in the table with a new incremented ID.
Managed to solve it as best as I can.
I checked my code and found that when I inserted the empty POST'd ID was wrapping it in quotations. I've now changed it so that it puts NULL without quotations. So my query should now look like:
INSERT INTO table (id,title,content) VALUES(NULL,"test","Test")
ON DUPLICATE KEY UPDATE title=VALUES(title), content=VALUES(content);
That now works.
I think you should make query like this,
INSERT INTO table (title,content) VALUES("Test","Test");
If it still doesn't work then check if id column is set as auto-increment or not.
I have a MySQL table for storing details of uploaded files, like this:
CREATE TABLE files (
file_id bigint not null primary key auto_increment,
file_name text,
file_path text,
file_extension varchar(15),
file_link text);
The actual name of the file on the disk is set to the file_id generated by the table when I insert the record - so although I can record all sorts of details, the database doesn't actually know what the file is called at this point.
Sure it's easy to just run another UPDATE statement shortly after to set the file_link column, but I wondered if there was a way to do something a bit more elegant here?
What I would like to do is have the value of file_link be automatically set on INSERT to this: CONCAT(file_path, file_id, file_extension) without needing to run another UPDATE statement immediately after.
Does anyone know how I can do this - and if it's a bad idea can you explain why?
P.S I'm using PHP to run these statements, if that makes a difference.
You could do this with the following trigger:
DELIMITER $$
CREATE TRIGGER makelink BEFORE INSERT ON files
FOR EACH ROW
BEGIN
SET NEW.file_link = CONCAT(NEW.file_path, NEW.file_id, NEW.file_extension);
END;
$$
DELIMITER ;
...although I have to say, I personally would go with creating this with the SELECT statement instead of having the database do things like this on insert - sure it's slightly more efficient to store static data, but it makes the database schema less transparent.
Typically you don't want to store derived data. Do some reading on db normalization when you have some spare cycles.