#Creating a new user in the database gives an out of memory error

34 messages · Page 1 of 1 (latest)

midnight chasm
#

I am writing this program with a user database to manage my projects, but right now I have this issue with the database/sql library, which gives me this error.
The code I am calling that function from is this:

func CreateNewUser(username string, password string, dbDir string, saltOutputDir string) (bool, error) {
    // Check if the database file exist
    _, err := DBinit(dbDir)
    if err != nil {
        return false, err
    }

    mainDb, err := sql.Open("sqlite", dbDir+"/users.db") // "./db_manage/database/users.db"

    if err != nil {
        // warn with log
        return false, err
    }

    // Check if the user already exists
    var checkUser string
    err = mainDb.QueryRow("SELECT Username FROM users WHERE Username = ?", username).Scan(&checkUser)
    if err != sql.ErrNoRows {
        return false, errors.New("user already exists")
    }

    // Generate the token
    token, err := GenerateToken(username, password, saltOutputDir)
    if err != nil {
        return false, err
    }

    // Insert the user into the database
    _, err = mainDb.Exec("INSERT INTO users (Username, TOKEN) VALUES (?, ?)", username, token)
    if err != nil {
        return false, err
    }

    defer func(mainDb *sql.DB) {
        err := mainDb.Close()
        if err != nil {
            log.Fatal(ErrorMsg(err.Error()))
        }
    }(mainDb)

    return true, nil
}

Btw the DBinit function just checks if the database file exists and creates it if it does not.
All required photos are given.

midnight chasm
#

Damn, no one knows

errant pawn
#

what architecture are you on?

#

also check maybe you system is out of memory?

languid wharf
#

how big is the database?

midnight chasm
midnight chasm
#

Alright now it does not compile

midnight chasm
# errant pawn ~~that sqlite driver is in beta~~(was looking at older version), idk if that's t...

Error
C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1 c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\onere\AppData\Local\Temp\go-link-2970451728\000017.o: in function `_cgo_preinit_init': \\_\_\runtime\cgo/gcc_libinit_windows.c:40: undefined reference to `__imp___iob_func' c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\onere\AppData\Local\Temp\go-link-2970451728\000017.o: in function `x_cgo_notify_runtime_init_done': \\_\_\runtime\cgo/gcc_libinit_windows.c:105: undefined reference to `__imp___iob_func' c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\onere\AppData\Local\Temp\go-link-2970451728\000017.o: in function `_cgo_beginthread': \\_\_\runtime\cgo/gcc_libinit_windows.c:149: undefined reference to `__imp___iob_func' c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\onere\AppData\Local\Temp\go-link-2970451728\000018.o: in function `x_cgo_thread_start': \\_\_\runtime\cgo/gcc_util.c:18: undefined reference to `__imp___iob_func' collect2.exe: error: ld returned 1 exit status

#

My go env:
set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\onere\AppData\Local\go-build set GOENV=C:\Users\onere\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\onere\go\pkg\mod set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=C:\Users\onere\go set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Program Files\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.19.4 set GCCGO=gccgo set GOAMD64=v1 set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=D:\programmieren\ppman\go.mod set GOWORK= set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\onere\AppData\Local\Temp\go-build2474269605=/tmp/go-build -gno-record-gcc-switches

warm agate
#

Btw the DBinit function just checks if the database file exists and creates it if it does not.
All required photos are given.
you dont need to, sqlite should do this for you

warm agate
midnight chasm
#

How would I implement

midnight chasm
#

@warm agate Are you still there?

warm agate
#

i'm on another server.
i suspect all of that undefined reference to come from msvc and i'm not familiar with msvc

midnight chasm
#

oh

warm agate
#

do you have another c compiler installed on your system?
mingw64 probably?

midnight chasm
#

I am installing it right now

#

but still having issues

#

How do I uninstall mingw32 and install mingw64?

urban stirrup
#

there's a pure go version of sqlite as well that's pretty much a drop in replacement for mattn's

#

oh you were already using it before

#

out of memory is a common sqlite error that means one of your parameters is wrong

#

The "out of memory" error is often a misleading error message caused by trying to use a database without having opened it first (or if you accidentally set the sqlite3 database pointer to NULL).

#

your code's a bit cursed, for example your defer is in the wrong place (it should be immediately after sql.Open), it also unnecessarily accepts a parameter

#

you're also using UNIX style paths on windows

#

I'm skeptical of what you're doing inside DBinit

#

it's also not clear what line the out of memory error appears in

errant pawn