I imported data from csv files into a MySQL database, but made the mistake of not
removing the trailing spaces in the csv columns. So the spaces are seen as '?' at
the end of some values in the database (of type Varchar). I want to get rid of these.
Can I somehow delete all these ?s in the database in one go? I know of the replace
command, but I think that works on a single column of a singe table at a time, which will
be very time consuming for me. Could anyone please suggest something better? Thanks!
You can use the trim function
UPDATE table SET column = TRIM(TRAILING '?' FROM column)
Related
I have so many data records and I want to import it into a table in database. I'm using phpmyadmin. I typed
LOAD DATA INFILE 'C:/Users/Asus/Desktop/cobacobaa.csv' INTO TABLE akdhis_kelanjutanstudi
but the result came like this:
I do not know why it said that I have duplicate entry "0" for primary, but actually in my data there is no duplicate entry, here is a part of my data
could you please help me to solve this? what may I do to solve that problem? thanks in advance
I would guess that your primary key is a number. The problem would then be that the value starts with a double quote. When converting a string to a number, MySQL converts the leading numeric characters -- with no such characters, the value is zero.
The following might fix your problem:
LOAD DATA INFILE 'C:/Users/Asus/Desktop/cobacobaa.csv'
INTO TABLE akdhis_kelanjutanstudi
FIELDS TERMINATED BY ',' ENCLOSED BY '"';
I usually load data into a staging table, where all the values are strings, and then convert the staging table into the final version. This may use a bit more space in the database but I find that the debugging process goes much faster when something goes wrong.
In MySQL Workbench I want to replace all instances of newlines with the explicit string "\\n" in my entire database, except where the string already exists. How would I form the query? Thanks.
[edit]
It just occurred to me that it might be easier to dump the database and use Notepad++ to replace the strings. But I was hoping there were a way to do this from within MySQL Workbench itself.
You will need to do this for each table and each column that you want to do the replacement. For example:
update table set column = REPLACE(column,'\n','\\n')
I just noticed that $wpdb->prepare() put slashes in the inserted values while it shouldn't cause this! e.g.: if you insert 'test' as a value it ends up as \'test\' in its table field.
How can I reliably remove those slashes when retrieving data from DB?
It escapes some special characters before storing them on your data base. You can use function stripslashes on your data after reading, to restore it.
I have been handed a very large CSV file that needs to be imported to a database.
I have done this before and know the general syntax, however, I am failing and getting errors on the column names as they contain a space.
I have tried different sorts of quotes without luck and have been searching the internet for the past hour.
Can anyone help me?
Backticks: `, to the left of the 1 key.
I have some small data with slashes in it.
I am doing a csv import with over 2000 lines. I am looping through each one and it goes fine but when I have slashes in the text the whole string wont import. I dont get any errors. The data just doesn't show up in the database.
Sample text would be "Barr/Massive"
How can I make it so mysql doesn't strip the slash.
Use bind parameters, don't insert the data directly into your SQL.
INSERT INTO table VALUES ($name)
versus
INSERT INTO table VALUES (?)
I'm assuming you're using some kind of PHP or Perl script for this. Otherwise, phpMyAdmin has a CSV import.