Adding sqlite3 and testing connection
							parent
							
								
									bda1ba8324
								
							
						
					
					
						commit
						550b7f553e
					
				| @ -0,0 +1,30 @@ | |||||||
|  | package database | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"database/sql" | ||||||
|  | 	"fmt" | ||||||
|  | 	"log/slog" | ||||||
|  | 
 | ||||||
|  | 	_ "github.com/mattn/go-sqlite3" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // OpenSqlite3DB is a wrapper
 | ||||||
|  | //
 | ||||||
|  | // TODO wtf dail uses context.Background(). Look into it more
 | ||||||
|  | func OpenSqlite3DB(dbPath string) (*sql.DB, error) { | ||||||
|  | 	full_database_path := "file:" + dbPath + "?cache=shared" | ||||||
|  | 
 | ||||||
|  | 	slog.Debug(fmt.Sprintf("Using database path: %s", full_database_path)) | ||||||
|  | 
 | ||||||
|  | 	db, err := sql.Open("sqlite3", full_database_path) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, fmt.Errorf("failed to open: %s", full_database_path) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err = db.Ping() | ||||||
|  | 	if err != nil { | ||||||
|  | 		db.Close() | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return db, nil | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in New Issue