Difference between revisions of "Sqlite sync"
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("...") |
(No difference)
|
Revision as of 00:07, 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!")
}