I want to do that using a code and not using a tool like \"MySQL Migration Toolkit\". The easiest way I know is to open a connection (using MySQL connectors) to DB1 and read
The FEDERATED storage engine? Not the fastest one in the bunch, but for one time, incidental, or small amounts of data it'll do. That is assuming you're talking about 2 SERVERS. With 2 databases on one and the same server it'll simply be:
INSERT INTO databasename1.tablename SELECT * FROM databasename2.tablename;