go-sqlite3
go-sqlite3 copied to clipboard
It doesn't create a new database file if opened in a `network drive` with `:encrypted.db?_key`.
It doesn't create a new database file if opened in a network drive with :encrypted.db?_key.
I attempted to open a new encrypted database on a network drive, and there were no errors upon opening. However, the database was not created, and then when attempting to create tables, errors occurred.
package main
import (
"database/sql"
"fmt"
"log"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3" // Standard SQLite driver
)
func main() {
// Replace with your network drive path, e.g., "\\server\share\encrypted.db" on Windows
// or "/mnt/network/encrypted.db" on Linux
dbPath := filepath.Join("/mnt/network", "encrypted.db")
fmt.Printf("Attempting to create database at: %s\n", dbPath)
// Ensure the directory exists
dir := filepath.Dir(dbPath)
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatalf("Failed to create directory %s: %v", dir, err)
}
// Open the database (this should create the file if it doesn’t exist)
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
// Verify the file was created
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
log.Fatalf("Database file %s was not created", dbPath)
}
fmt.Println("Database file created or opened successfully.")
// Create a table
err = createTable(db)
if err != nil {
log.Fatalf("Failed to create table: %v", err)
}
fmt.Println("Table created successfully.")
}
// createTable sets up a simple users table
func createTable(db *sql.DB) error {
query := `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER
);`
_, err := db.Exec(query)
if err != nil {
return fmt.Errorf("error creating table: %v", err)
}
return nil
}