36 lines
711 B
Go
36 lines
711 B
Go
package utils
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"time"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func CreateDatabase() {
|
|
// Create a new SQLite database if it doesn't exist
|
|
fileStartTime := time.Now()
|
|
os.Create("address.db")
|
|
db, err := sql.Open("sqlite3", "address.db")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fileDuration := time.Since(fileStartTime)
|
|
log.Info("Database file created in ", fileDuration)
|
|
|
|
// Create the contacts table if it doesn't exist
|
|
createTableQuery := `CREATE TABLE IF NOT EXISTS contacts (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
fname TEXT NOT NULL,
|
|
lname TEXT NOT NULL,
|
|
phone TEXT NOT NULL
|
|
);`
|
|
|
|
_, err = db.Exec(createTableQuery)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer db.Close()
|
|
}
|