Find and replace entire mysql database

后端 未结 12 2062
走了就别回头了
走了就别回头了 2020-11-27 13:16

i would like to do a find and replace inside an entire database not just a table.

How can i alter the script below to work?

 update [table_name] set          


        
相关标签:
12条回答
  • 2020-11-27 13:40

    Update old URL to new URL in word-press mysql Query:

    UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
    
    UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com');
    
    UPDATE wp_posts SET post_content = replace(post_content, 'http://olddomain.com', 'http://newdomain.com');
    
    UPDATE wp_posts SET post_excerpt = replace(post_excerpt, 'http://olddomain.com', 'http://newdomain.com');
    
    UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');
    
    0 讨论(0)
  • 2020-11-27 13:47

    Short answer: You can't.

    Long answer: You can use the INFORMATION_SCHEMA to get the table definitions and use this to generate the necessary UPDATE statements dynamically. For example you could start with this:

    SELECT TABLE_NAME, COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'your_schema'
    

    I'd try to avoid doing this though if at all possible.

    0 讨论(0)
  • 2020-11-27 13:47

    I just wanted to share how I did this find/replace thing with sql database, because I needed to replace links from Chrome's sessionbuddy db file.

    • So I exported sql database file as .txt file by using SQLite Database Browser 2.0 b1
    • Find/replace in notepad++
    • Imported the .txt file back on SQLite Database Browser 2.0 b1
    0 讨论(0)
  • 2020-11-27 13:48

    Simple Soltion

    UPDATE `table_name`
     SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')
    
    0 讨论(0)
  • 2020-11-27 13:49

    This strongly implies that your data IS NOT NORMALISED to begin with.

    Something like this should work (NB you've not mentioned of your using any other languages - so its written as a MySQL stored procedure)

     create procedure replace_all(find varchar(255), 
            replce varchar(255), 
            indb varcv=char(255))
     DECLARE loopdone INTEGER DEFAULT 0;
     DECLARE currtable varchar(100);
     DECLARE alltables CURSOR FOR SELECT t.tablename, c.column_name 
        FROM information_schema.tables t,
        information_schema.columns c
        WHERE t.table_schema=indb
        AND c.table_schema=indb
        AND t.table_name=c.table_name;
    
     DECLARE CONTINUE HANDLER FOR NOT FOUND
         SET loopdone = 1;
    
     OPEN alltables;
    
     tableloop: LOOP
        FETCH alltables INTO currtable, currcol; 
        IF (loopdone>0) THEN LEAVE LOOP;
        END IF;
             SET stmt=CONCAT('UPDATE ', 
                      indb, '.', currtable, ' SET ',
                      currcol, ' = word_sub(\'', find, 
                      '\','\'', replce, '\') WHERE ',
                      currcol, ' LIKE \'%', find, '%\'');
             PREPARE s1 FROM stmt;
             EXECUTE s1;
             DEALLOCATE PREPARE s1;
         END LOOP;
     END //
    

    I'll leave it to you to work out how to declare the word_sub function.

    0 讨论(0)
  • 2020-11-27 13:53

    BE CAREFUL, when replacing with REPLACE command!

    why?

    because there is a great chance that your database contains serialized data (especially wp_options table), so using just "replace" might break data.

    Use recommended serialization: https://puvox.software/tools/wordpress-migrator

    0 讨论(0)
提交回复
热议问题