我需要替换数据库中出现的所有字符串。我不知道table的名字。列定义。没什么
数据库是WordPress的MySQL数据库,我需要替换所有表中的字符串。如果用户安装一些WordPress插件,它可以创建自己的数据库表。所以我不能使用Hibernate将表Map到POJO。
表格可能看起来像这个图像:
我尝试entityManager.createNativeQuery()
获取所有表的名称。它的工作原理:
@PersistenceContext
EntityManager entityManager;
//...
String q3 = "SELECT table_name, table_schema FROM information_schema.tables WHERE table_schema = 'wh" + Integer.toString(serverId) + "' AND table_type = 'BASE TABLE' ORDER BY table_name ASC";
System.out.println(q3);
List<Object[]> objects3 = entityManager.createNativeQuery(q3).getResultList();
for (Object[] o : objects3) {
String table_name = (String) o[0];
String table_schema = (String) o[1];
System.out.println(table_name + " --- " + table_schema);
}
产出:
wh123_commentmeta --- wh123
wh123_comments --- wh123
wh123_links --- wh123
wh123_options --- wh123
wh123_postmeta --- wh123
wh123_posts --- wh123
wh123_term_relationships --- wh123
wh123_term_taxonomy --- wh123
wh123_termmeta --- wh123
wh123_terms --- wh123
wh123_usermeta --- wh123
wh123_users --- wh123
wh123_ve_posts_layer --- wh123
然后找到所有出现的字符串“example.net“。它也可以工作:
// example for one table:
String find = "example.net";
String q4 = "SELECT * FROM wh" + Integer.toString(serverId) + ".wh" + Integer.toString(serverId) + "_options";
System.out.println(q4);
List<Object[]> objects4 = entityManager.createNativeQuery(q4).getResultList();
for (Object[] o : objects4) {
for (int i = 0; i < o.length; i++) {
System.out.print(o[i]);
System.out.print(" - ");
if (o[i].toString().contains(find)) {
System.err.println("STRING FOUND: " + o[i].toString());
}
}
}
产出:
617 - web_option_license - a:1:{s:7:"license";s:16:"[email protected]";} - STRING FOUND: a:1:{s:7:"license";s:16:"[email protected]";}
现在我需要用(例如)text.com
替换example.net
,并替换数据库中的原始字符串。但是现在我不知道如何把替换的字符串放回数据库...
PHP数组的序列化(反)将是下一步.
1条答案
按热度按时间nnt7mjpx1#
它并不完美,但它有效:
Maven依赖:
我的代码: