Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Fevzi Ömür Tekin
Fevzi Ömür Tekin

Posted on

     

Using Sqlite in Go Programming.

alt text

SQLite is an open source code that is a relational sql search engine. The most important advantage of a host is that the server does not need a local server.

In this example I'll make a simple example running on sqlite with go.

Used Libraries

import (  "database/sql"  "fmt"  "strconv"  _ "github.com/mattn/go-sqlite3")
Enter fullscreen modeExit fullscreen mode

Model Structure

type User struct {  id         int  username   string  surname    string  age        int  university string  //I created a struct with a struct to select the rows in the table and add data.}
Enter fullscreen modeExit fullscreen mode


I am using a user model consisting of "id", "username", "surname", "age" and "university".

CRUD Methods

I have done CRUD operations using Sqlite database with Go programming language.

func addUser(db *sql.DB, username string, surname string, age int, university string) {  tx, _ := db.Begin()  stmt, _ := tx.Prepare("insert into testTable (username,surname,age,university) values (?,?,?,?)")  _, err := stmt.Exec(username, surname, age, university)  checkError(err)  tx.Commit()}func getUsers(db *sql.DB, id2 int) User {  rows, err := db.Query("select * from testTable")  checkError(err)  for rows.Next() {    var tempUser User    err =      rows.Scan(&tempUser.id, &tempUser.username, &tempUser.surname, &tempUser.age, &tempUser.university)    checkError(err)    if tempUser.id == id2 {      return tempUser    }  }  return User{}}func updateUser(db *sql.DB, id2 int, username string, surname string, age int, university string) {  sage := strconv.Itoa(age) // int to string  sid := strconv.Itoa(id2)  // int to string  tx, _ := db.Begin()  stmt, _ := tx.Prepare("update testTable set username=?,surname=?,age=?,university=? where id=?")  _, err := stmt.Exec(username, surname, sage, university, sid)  checkError(err)  tx.Commit()}func deleteUser(db *sql.DB, id2 int) {  sid := strconv.Itoa(id2) // int to string  tx, _ := db.Begin()  stmt, _ := tx.Prepare("delete from testTable where id=?")  _, err := stmt.Exec(sid)  checkError(err)  tx.Commit()}
Enter fullscreen modeExit fullscreen mode

Main Function

db, _ := sql.Open("sqlite3", "database/godb.db")  db.Exec("create table if not exists testTable (id integer,username text, surname text,age Integer,university text)")
Enter fullscreen modeExit fullscreen mode


I'm writing code that allows you to connect to the database and run. Then I use the methods that let me perform the CRUD operations above.

func main() {  db, _ := sql.Open("sqlite3", "database/godb.db")  db.Exec("create table if not exists testTable (id integer,username text, surname text,age Integer,university text)")  addUser(db, "fevzi omur ", "tekin", 24, "Sakarya University") // added data to database  updateUser(db, 2, "Ken", "Thompson", 75, "California university") //update data to database  deleteUser(db, 1) // delete data to database  fmt.Println(getUsers(db, 2)) // printing the user}
Enter fullscreen modeExit fullscreen mode

Note : The codes of the project can be found atthis address.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
rajaseelan profile image
Rajaseelan Ganeswaran
  • Joined

This article is a good reference. Thank you!

CollapseExpand
 
bashery profile image
bashery

thanks

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

{ "description": { "name": "Fevzi Ömür Tekin", "label": "Software Engineer", "email": "fevziomurtekin@gmail.com", "website": "https://fevziomurtekin.github.io" }}
  • Location
    Turkey
  • Work
    Software Developer at Kuark Dijital
  • Joined

More fromFevzi Ömür Tekin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp