Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite - How do you join tables from different databases?

I have an application that uses a SQLite database and everything works the way it should. I'm now in the process of adding new functionalities that require a second SQLite database, but I'm having a hard time figuring out how to join tables from the different databases.

If someone can help me out with this one, I'd really appreciate it!

Edit: See this question for an example case you can adapt to your language when you attach databases as mentioned in the accepted answer.

like image 530
Adam Smith Avatar asked Jul 26 '11 02:07

Adam Smith


People also ask

Can you join 2 tables from different databases?

SQL Server allows you to join tables from different databases as long as those databases are on the same server. The join syntax is the same; the only difference is that you must fully qualify table names.

Does SQLite support multiple databases?

Yes, SQLite explicitly supports multi-database transactions (see https://www.sqlite.org/atomiccommit.html#_multi_file_commit for technical details).

Does SQLite have join?

A SQLite JOIN is performed whenever two or more tables are joined in a SQL statement. There are different types of SQLite joins: INNER JOIN (or sometimes called simple join) LEFT OUTER JOIN (or sometimes called LEFT JOIN)


2 Answers

If ATTACH is activated in your build of Sqlite (it should be in most builds), you can attach another database file to the current connection using the ATTACH keyword. The limit on the number of db's that can be attached is a compile time setting(SQLITE_MAX_ATTACHED), currently defaults to 10, but this too may vary by the build you have. The global limit is 125.

attach 'database1.db' as db1; attach 'database2.db' as db2; 

You can see all connected databases with keyword

.databases 

Then you should be able to do the following.

select   * from   db1.SomeTable a     inner join    db2.SomeTable b on b.SomeColumn = a.SomeColumn; 

Note that "[t]he database names main and temp are reserved for the primary database and database to hold temporary tables and other temporary data objects. Both of these database names exist for every database connection and should not be used for attachment".

like image 197
Brian Gideon Avatar answered Oct 14 '22 14:10

Brian Gideon


Here is a C# example to complete this Question

/// <summary> /// attachSQL = attach 'C:\\WOI\\Daily SQL\\Attak.sqlite' as db1 */ /// path = "Path of the sqlite database file /// sqlQuery  = @"Select A.SNo,A.MsgDate,A.ErrName,B.SNo as BSNo,B.Err as ErrAtB from Table1 as A  ///                    inner join db1.Labamba as B on  ///                    A.ErrName = B.Err"; /// </summary> /// <param name="attachSQL"></param> /// <param name="sqlQuery"></param> public static DataTable GetDataTableFrom2DBFiles(string attachSQL, string sqlQuery) {     try     {         string conArtistName = "data source=" + path + ";";         using (SQLiteConnection singleConnectionFor2DBFiles = new SQLiteConnection(conArtistName))         {             singleConnectionFor2DBFiles.Open();             using (SQLiteCommand AttachCommand = new SQLiteCommand(attachSQL, singleConnectionFor2DBFiles))             {                 AttachCommand.ExecuteNonQuery();                 using (SQLiteCommand SelectQueryCommand = new SQLiteCommand(sqlQuery, singleConnectionFor2DBFiles))                 {                     using (DataTable dt = new DataTable())                     {                         using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(SelectQueryCommand))                         {                             adapter.AcceptChangesDuringFill = true;                             adapter.Fill(dt);                             return dt;                         }                     }                 }             }         }     }     catch (Exception ex)     {         MessageBox.Show("Use Process Exception method An error occurred");         return null;     }  } 
like image 24
Dr.Sai Avatar answered Oct 14 '22 15:10

Dr.Sai