Difference between revisions of "Sqlite sync"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "``` package main import ( "database/sql" "fmt" _ "github.com/mattn/go-sqlite3" ) func main() { // Open source SQLite database sourceDB, err := sql.Open("...")
 
 
Line 54: Line 54:
 
         fmt.Println("Error iterating over rows:", err)
 
         fmt.Println("Error iterating over rows:", err)
 
         return
 
         return
 +
    }
 +
 +
    fmt.Println("Sync completed successfully!")
 +
}
 +
 +
```
 +
 +
# sqlx
 +
 +
```
 +
package main
 +
 +
import (
 +
    "fmt"
 +
    "github.com/jmoiron/sqlx"
 +
    _ "github.com/mattn/go-sqlite3"
 +
)
 +
 +
func main() {
 +
    // Open source SQLite database
 +
    sourceDB, err := sqlx.Open("sqlite3", "/path/to/source.db")
 +
    if err != nil {
 +
        fmt.Println("Error opening source database:", err)
 +
        return
 +
    }
 +
    defer sourceDB.Close()
 +
 +
    // Open destination SQLite database
 +
    destDB, err := sqlx.Open("sqlite3", "/path/to/destination.db")
 +
    if err != nil {
 +
        fmt.Println("Error opening destination database:", err)
 +
        return
 +
    }
 +
    defer destDB.Close()
 +
 +
    // Define a struct to represent your table
 +
    type YourTable struct {
 +
        ID  int    `db:"id"`
 +
        Name string `db:"name"`
 +
    }
 +
 +
    // Query data from source database
 +
    var data []YourTable
 +
    err = sourceDB.Select(&data, "SELECT * FROM your_table")
 +
    if err != nil {
 +
        fmt.Println("Error querying source database:", err)
 +
        return
 +
    }
 +
 +
    // Insert or update into destination database
 +
    for _, row := range data {
 +
        _, err = destDB.NamedExec("INSERT OR REPLACE INTO your_table (id, name) VALUES (:id, :name)", row)
 +
        if err != nil {
 +
            fmt.Println("Error inserting into destination database:", err)
 +
            continue
 +
        }
 
     }
 
     }
  

Latest revision as of 00:18, 1 April 2024

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    // Open source SQLite database
    sourceDB, err := sql.Open("sqlite3", "/path/to/source.db")
    if err != nil {
        fmt.Println("Error opening source database:", err)
        return
    }
    defer sourceDB.Close()

    // Open destination SQLite database
    destDB, err := sql.Open("sqlite3", "/path/to/destination.db")
    if err != nil {
        fmt.Println("Error opening destination database:", err)
        return
    }
    defer destDB.Close()

    // Query data from source database
    rows, err := sourceDB.Query("SELECT * FROM your_table")
    if err != nil {
        fmt.Println("Error querying source database:", err)
        return
    }
    defer rows.Close()

    // Iterate over rows and insert into destination database
    for rows.Next() {
        var id int
        var name string
        // Assuming your_table has columns id and name
        err = rows.Scan(&id, &name)
        if err != nil {
            fmt.Println("Error scanning row:", err)
            continue
        }

        // Insert or update into destination database
        _, err = destDB.Exec("INSERT OR REPLACE INTO your_table (id, name) VALUES (?, ?)", id, name)
        if err != nil {
            fmt.Println("Error inserting into destination database:", err)
            continue
        }
    }
    if err = rows.Err(); err != nil {
        fmt.Println("Error iterating over rows:", err)
        return
    }

    fmt.Println("Sync completed successfully!")
}

sqlx

package main

import (
    "fmt"
    "github.com/jmoiron/sqlx"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    // Open source SQLite database
    sourceDB, err := sqlx.Open("sqlite3", "/path/to/source.db")
    if err != nil {
        fmt.Println("Error opening source database:", err)
        return
    }
    defer sourceDB.Close()

    // Open destination SQLite database
    destDB, err := sqlx.Open("sqlite3", "/path/to/destination.db")
    if err != nil {
        fmt.Println("Error opening destination database:", err)
        return
    }
    defer destDB.Close()

    // Define a struct to represent your table
    type YourTable struct {
        ID   int    `db:"id"`
        Name string `db:"name"`
    }

    // Query data from source database
    var data []YourTable
    err = sourceDB.Select(&data, "SELECT * FROM your_table")
    if err != nil {
        fmt.Println("Error querying source database:", err)
        return
    }

    // Insert or update into destination database
    for _, row := range data {
        _, err = destDB.NamedExec("INSERT OR REPLACE INTO your_table (id, name) VALUES (:id, :name)", row)
        if err != nil {
            fmt.Println("Error inserting into destination database:", err)
            continue
        }
    }

    fmt.Println("Sync completed successfully!")
}