最不屑一顾是相思的gravatar头像
最不屑一顾是相思 2017-06-06 17:24:49

如何将数据库中一张表的数据复制到另外一张表?

把a项目中的a数据库中的a表,所有数据 查询 插入到 b项目,b数据库,b表,

ab两表 字段不一定相同, a表存的是文件路径,文件也需要复制到指定路径,各位大牛,给个思路,当中可能涉及b表字段类型长度不一样,需要用md5之类,字段加密合适长度,主要表表结构会有很多不一致,

所有回答列表(4)
最代码官方的gravatar头像
最代码官方  LV167 2017年6月7日

最简单的写个程序转换吧,当时javaniu到zuidaima的数据库设计完全不同,我就是通过java程序实现的。

比如评论分享

@Test
	public void convertComment() throws Exception {
		Connection connection1 = null;
		Connection connection2 = null;
		String selectSql = null;
		String insertSql = null;
		String deleteSql = null;
		PreparedStatement statement1 = null;
		PreparedStatement statement2 = null;
		ResultSet rs = null;
		try {
			connection1 = getJavaniu1Connection();
			connection2 = getJavaniu2Connection();
			deleteSql = "delete from javaniu_comment";
			statement2 = connection2.prepareStatement(deleteSql);
			statement2.execute(deleteSql);

			connection1 = getJavaniu1Connection();
			connection2 = getJavaniu2Connection();

			insertSql = "insert into javaniu_comment(create_time,user_id,type,content,status,target_id)"
					+ "values (?,?,?,?,?,?)";
			statement2 = connection2.prepareStatement(insertSql);
			selectSql = "select * from share_comment";
			statement1 = connection1.prepareStatement(selectSql);
			rs = statement1.executeQuery();
			while (rs.next()) {
				int parameterIndex = 1;
				String create_time = rs.getString("create_time");
				statement2.setString(parameterIndex, create_time);

				parameterIndex++;
				long user_id = rs.getLong("user_id");
				statement2.setLong(parameterIndex, user_id);

				parameterIndex++;
				statement2.setInt(parameterIndex,
						ModuleConstants.COMMENT_TYPE_PROJECT);

				parameterIndex++;
				String content = rs.getString("content");
				content = formatContent(content);
				statement2.setString(parameterIndex, content);

				parameterIndex++;
				statement2.setInt(parameterIndex,
						ModuleConstants.MODULE_STATUS_NORMAL);

				parameterIndex++;
				String description = getActivityBlogDescriptionById(rs
						.getLong("activity_blog_id"));
				long target_id = getProjectIdByContent(description);
				statement2.setLong(parameterIndex, target_id);

				statement2.addBatch();
			}
			statement2.executeBatch();




			statement1.close();
			statement2.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			connection1.close();
			connection2.close();
		}
	}
评论(3) 最佳答案
最不屑一顾是相思的gravatar头像
最不屑一顾是相思  LV19 2017年6月7日

有人回答吗

Nero丶的gravatar头像
Nero丶  LV9 2017年6月8日

不知道,你现在用的是什么数据库,我目前在用的sqlserver。可以把数据导出到excle,,在导入到另外一个库中,字段可以自己进行对应的映设,这样就可以不用管表结构了,如果tid也需要一一对应的话,就要新起表,关闭主键自增,导入后,在进行递增。至于字段长度不一致。。感觉这个只能写脚本,批量去跑一下,直接增加长度啥的把。我平时都是这么干的

1040287230的gravatar头像
1040287230  LV4 2017年6月8日

这个在数据库操作更加的方便、简洁,就以oracle来说,我之前使用的是 minus 这个oracle的关键字,可以比对两张表一样列的值,然后使用insert插入即可
这个关键字原意是比对两张表中的数据,有不一样的数据就插入到另外一张表,不过我感觉同样适用你这里。
 

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友