write a todo program in echo golang using sqlite db
- User
write a todo program in echo golang using sqlite db
Assistant
Below is a simple example of a Todo List program written in Go (also known as Golang) that uses SQLite as its database. This program allows you to add, list, and delete todo items.
Before running this program, ensure you have Go installed on your system. You will also need to install the
github.com/mattn/go-sqlite3package, which is a Go interface to SQLite databases. You can install it using the following command:go get github.com/mattn/go-sqlite3Here's the program:
package main import ( "database/sql" "fmt" "log" _ "github.com/mattn/go-sqlite3" ) // TodoItem represents a todo item type TodoItem struct { ID int `json:"id"` Title string `json:"title"` Done bool `json:"done"` } func main() { // Connect to SQLite database. It will be created if it doesn't exist. db, err := sql.Open("sqlite3", "./todo.db") if err != nil { log.Fatal(err) } defer db.Close() // Create table if it doesn't exist _, err = db.Exec(` CREATE TABLE IF NOT EXISTS todos ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, done INTEGER NOT NULL DEFAULT 0 ); `) if err != nil { log.Fatal(err) } // Menu loop for { fmt.Println("1. Add Todo") fmt.Println("2. List Todos") fmt.Println("3. Delete Todo") fmt.Println("4. Quit") var choice int fmt.Print("Choose an option: ") fmt.Scanln(&choice) switch choice { case 1: addTodo(db) case 2: listTodos(db) case 3: deleteTodo(db) case 4: return default: fmt.Println("Invalid choice. Please choose a valid option.") } } } func addTodo(db *sql.DB) { var title string fmt.Print("Enter todo title: ") fmt.Scanln(&title) _, err := db.Exec("INSERT INTO todos (title) VALUES (?)", title) if err != nil { log.Println(err) return } fmt.Println("Todo added successfully.") } func listTodos(db *sql.DB) { rows, err := db.Query("SELECT id, title, done FROM todos") if err != nil { log.Println(err) return } defer rows.Close() for rows.Next() { var todo TodoItem err := rows.Scan(&todo.ID, &todo.Title, &todo.Done) if err != nil { log.Println(err) return } status := "Not Done" if todo.Done { status = "Done" } fmt.Printf("ID: %d, Title: %s, Status: %s\n", todo.ID, todo.Title, status) } } func deleteTodo(db *sql.DB) { var id int fmt.Print("Enter todo ID to delete: ") fmt.Scanln(&id) _, err := db.Exec("DELETE FROM todos WHERE id = ?", id) if err != nil { log.Println(err) return } fmt.Println("Todo deleted successfully.") }This program does the following:
- Connects to an SQLite database file named
todo.db. - Creates a
todostable if it doesn't exist. - Provides a simple menu-driven interface to add, list, and delete todo items.
- Uses prepared statements to prevent SQL injection.
To run this program, save it to a file named
main.go, navigate to that directory in your terminal or command prompt, and run it with the commandgo run main.go.- Connects to an SQLite database file named
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.