如何使用bash或java将三个不同的sqlite3数据库D1,D2和D3附加到另一个Database D4

时间:2018-01-04 00:50:37

标签: java shell sqlite

我在同一个文件夹中有3个不同的数据库,如下所示

enter image description here

我想将其他3个数据库附加到joined.db数据库,然后创建一个名为join1的表,如下所示

public class attaching_DB {


public static void main(String args[]) {
    Connection c = null;
    Statement stmt = null;

    try {
        Class.forName("org.sqlite.JDBC");


        String database_path = "/Users/amar/Documents/ThesisCode/CEP_Architectures/databases/joined.db";

        c = DriverManager.getConnection("jdbc:sqlite:" + database_path);


        System.out.println("connection to sql is made");

        stmt = c.createStatement();




        // attaching all the tables in joined database
        String a1 = "ATTACH DATABASE 'mobile_data.db' as 'mobile';" ;
        String a2 = "ATTACH DATABASE 'server_data.db' as 'server';" ;
        String a3 = "ATTACH DATABASE 'flink_data.db' as 'flink';" ;


        stmt.execute(a1);
        stmt.execute(a2);
        stmt.execute(a3);


        String b1 =  "CREATE TABLE if not EXISTS  mobile_events as select * from mobile.mobile_events;"   ;
        String b2 =  "CREATE TABLE if not EXISTS  server_events  as select * from server.server_events;" ;
        String b3 =  "CREATE TABLE if not EXISTS  flink_events  as select * from flink.flink_events;"  ;

        stmt.executeUpdate(b1);
        stmt.executeUpdate(b2);
        stmt.executeUpdate(b3);


        // joining mobile and server data
        String join1 = "CREATE TABLE  if not EXISTS join1 as select M.patientid, M.sensorid , M.uid , M.egtl, M.egtg, S.eatg, M.valuez from mobile_events M inner join server_events S  on M.sensorid = S.sensorid and  M.uid = S.uid ; " ;
        stmt.executeUpdate(join1);



        stmt.close();
        c.commit();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("attach is successful");
}

}

我收到以下错误

org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: mobile.mobile_events)

问题是附件不起作用,我尝试使用可执行bash执行相同的操作,如下所示,但是当执行sqlite3 joined.db时,会打开一个新选项卡并停止脚本

    #! /bin/bash
clear
cd /Users/amar/Documents/ThesisCode/CEP_Architectures/databases/
#starting sqlite3
sqlite3 joined.db

# attaching other db's
ATTACH DATABASE 'mobile_data.db' as 'mobile';
ATTACH DATABASE 'server_data.db' as 'server';
ATTACH DATABASE 'flink_data.db' as 'flink';

# creating tables
CREATE TABLE mobile_events as select * from mobile.mobile_events;
CREATE TABLE server_events as select * from server.server_events;
CREATE TABLE server_events as select * from server.server_events;

# crate a joined table
CREATE TABLE join1 as select M.patientid, M.sensorid , M.uid , M.egtl, M.egtg, S.eatg, M.valuez from mobile_events M inner join server_events S  on M.sensorid = S.sensorid and  M.uid = S.uid ;
echo "program is complete"

P.S:我可以通过在终端中手动编写这些脚本来附加数据库并创建表join1,但是由于我正在做一些实验,所以我不想一次又一次地这样做

提前致谢!

1 个答案:

答案 0 :(得分:1)

您将SQL语句传递给bash,后者无法理解它们。将它们传递给sqlite3,如:

#! /bin/bash
clear
cd /Users/amar/Documents/ThesisCode/CEP_Architectures/databases/
#starting sqlite3
sqlite3 joined.db <<EOF

-- attaching other db's
ATTACH DATABASE "mobile_data.db" as mobile;
ATTACH DATABASE "server_data.db" as server;
ATTACH DATABASE "flink_data.db" as flink;

-- creating tables
CREATE TABLE mobile_events as select * from mobile.mobile_events;
CREATE TABLE server_events as select * from server.server_events;
CREATE TABLE flink_events as select * from flink.flink_events;

-- create a joined table
CREATE TABLE join1 as select M.patientid, M.sensorid , M.uid , M.egtl, M.egtg, S.eatg, M.valuez from mobile_events M inner join server_events S  on M.sensorid = S.sensorid and  M.uid = S.uid ;

EOF

echo "program is complete"