我需要将数据从SQLite数据库的一个表(在本例中为unsc1.unsc)复制到另一个数据库的另一个新表(CompleteNewDB.new_table),通过JDBC。我不断收到错误消息:“ SQL错误或缺少数据库(无此表:unsc)。
表存在; sqlite查询的工作方式与我使用其他软件分别尝试过的一样。程序和尝试执行该程序的方式是否存在错误?有没有更简单的方法可以将JDBC的数据从table1复制到table2,每个分别位于单独的数据库中?
import java.sql.*;
public class Application {
public static void main(String[] args) {
Connection un1 = null;
Connection un2 = null;
Connection newDB = null;
Statement stmt1 = null;
Statement stmt2 = null;
String queryCreate = "CREATE TABLE newtable ("
+ "m_code_txt TEXT, "
+ "m_date_txt TEXT, "
+ "m_filename TEXT, "
+ "m_resumption_int INT, "
+ "m_status_txt TEXT, "
+ "m_suspension_int INT, "
+ "m_agenda_txt TEXT, "
+ "p_name_txt TEXT, "
+ "p_nation_txt TEXT, "
+ "p_role_txt TEXT, "
+ "s_speech_lang TEXT, "
+ "s_speech_txt TEXT, "
+ "s_speech_pos REAL );";
String queryData = "INSERT INTO new_table "
+ "SELECT * "
+ "FROM unsc1.unsc;";
try {
Class.forName("org.sqlite.JDBC");
//DB unsc1
un1 = DriverManager.getConnection("jdbc:sqlite:unsc1.db");
System.out.println("Connection to unsc1.db established");
//DB unsc2
un2 = DriverManager.getConnection("jdbc:sqlite:unsc2.db");
System.out.println("Connection to unsc2.db established");
//Migrated DB
newDB = DriverManager.getConnection("jdbc:sqlite:CompleteNewDB.db");
System.out.println("Creation & Connection to CompleteNewDB.db established");
//First query: create a table in new database
stmt1 = newDB.createStatement();
stmt1.execute(queryCreate);
System.out.println("Created table in CompleteNewDB ");
stmt1.close();
//PROBLEM HERE: copy data from table in database "unsc1.db" to the new_table ??
stmt2 = newDB.createStatement();
stmt2.execute(queryData);
System.out.println("Copied data from first DB to new DB.");
stmt2.close();
//closing all
un1.close();
un2.close();
newDB.close();
}
catch(Exception e) {e.printStackTrace(); }
}
}
要在一个SQL中访问多个数据库(与上述queryData
相同,下级数据库必须为ATTACH
ed。我对jdbc并不熟悉,因此无法提供有关如何完成该操作的具体指导。
当然,另一种选择是将第一个数据库中的行选择为某种程序数据结构,然后对第二个数据库执行INSERT。