Inserts - Flaarum Tutorials (Golang) Python
Insertion Method 1
The first method of insertion expects a map[string]any for its data. Flaarum provides no statements for inserts. One would need to use a function call for insertions.
Sample Code
package main
import (
"fmt"
"github.com/saenuma/flaarum"
)
func main() {
cl := flaarum.NewClient("127.0.0.1", "not-yet-ready", "first_proj")
toInsert := map[string]any {
"userid": 32,
"roleid": 12,
}
retid, err := cl.InsertRowAny("user_roles", toInsert)
if err != nil {
flErr := err.(flaarum.FlaarumError)
if flErr.Code == 21 {
fmt.Println("An element is not unique")
}
}
fmt.Println(retid)
}
The flaarum lib expects the address of the database, its keystring (like a password) and the name of the project.
All Error Codes
- 10: Connection Error
- 11: Error happended on server
- 12: Statements Error
- 20: Validation error
- 21: Errors relating to unique constraints
- 22: Errors relating to required constraints
- 23: Errors relating to foreign keys
- 24: Type errors
Insertion Method 2
The second method of insertion expects a map[string]string for its data. This introduces some comfort in some situations.
Sample Code
package main
import (
"github.com/saenuma/flaarum"
)
func main() {
cl := flaarum.NewClient("127.0.0.1", "not-yet-ready", "first_proj")
toInsert := map[string]string {
"userid": "32",
"roleid": "12",
}
retid, err := cl.InsertRowStr("user_roles", toInsert)
if err != nil {
panic(err)
}
}