dth96108
2016-05-14 09:29
浏览 189

您可以在Golang中使用SQLite ATTACH和DETACH吗

I am using https://github.com/mattn/go-sqlite3 in a Golang application. I need to use multiple databases, and was wondering whether I can use ATTACH and DETACH in Golang (http://sqlite.org/lang_attach.html).

I don't have any code right now, because I don't know how else to tackle this problem except creating two database connections and joining data in the application:

db1, err := sql.Open("sqlite3", "./db1.db")

db2, err := sql.Open("sqlite3", "./db2.db")

// query db1 and db2
// put into map
// join data

It would be much easier if you could use:

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

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

图片转代码服务由CSDN问答提供 功能建议

我正在使用 https://github.com/mattn/go-sqlite3 在Golang应用程序中。 我需要使用多个数据库,并且想知道是否可以在Golang中使用ATTACH和DETACH( http://sqlite.org /lang_attach.html )。

我现在没有任何代码,因为除了创建两个数据库连接并加入连接之外,我不知道如何解决此问题 应用程序中的数据:

  db1,err:= sql.Open(“ sqlite3”,“ ./db1.db")

db2,err:= sql。 打开(“ sqlite3”,“ ./ db2.db”)
 
 //查询db1和db2 
 //放入地图
 //连接数据
   
 \  n 

使用以下方法会容易得多:

 将数据库database1.db附加为db1; 
将数据库database2.db附加为db2; 
 
select \  n * 
from 
 db1.SomeTable a 
内部联接
 db2.someTable b在b.SomeColumn = a.SomeColumn; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi6528 2016-05-14 11:24
    已采纳

    You can attach a database, you would do it in SQL after opening the first database (omit error check for brevity):

    db1, err := sql.Open("sqlite3", "./db1.db")
    defer db1.Close()
    
    s := "attach database '/path/to/db2.db' as db2;"
    
    _, err = db.Exec(s)
    
    s = `select *
         from db1.SomeTable a
         inner join db2.SomeTable b on b.SomeColumn = a.SomeColumn; `
    
    results, err := db.Query(s)
    defer results.Close()
    
    //process results ...
    
    _, err = db.Exec("detach database db2;")
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题