我有一个使用SQLite数据库的应用程序,一切都正常运行。我现在正在添加需要第二个SQLite数据库的新功能,但我很难弄清楚如何连接来自不同数据库的表格。
如果有人能帮助我解决这个问题,我将不胜感激!
编辑:请参考此问题以获取示例案例,您可以根据答案中提到的附加数据库适用于您的语言。
如果有人能帮助我解决这个问题,我将不胜感激!
编辑:请参考此问题以获取示例案例,您可以根据答案中提到的附加数据库适用于您的语言。
attach 'database1.db' as db1;
attach 'database2.db' as db2;
您可以使用关键字查看所有已连接的数据库
.databases
select
*
from
db1.SomeTable a
inner join
db2.SomeTable b on b.SomeColumn = a.SomeColumn;
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"。'
(单引号)时才起作用。我也发现了相同的情况。 - bkribbs/// <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;
}
}
嗯,我对SQLite没有太多经验,但你需要在一个查询中访问两个数据库。
你可以像这样:
select name from DB1.table1 as a join DB2.table2 as b where a.age = b.age;