Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql error ERROR 3141 is not cathed in db.query() #1622

Closed
sora91s opened this issue Aug 12, 2024 · 7 comments
Closed

mysql error ERROR 3141 is not cathed in db.query() #1622

sora91s opened this issue Aug 12, 2024 · 7 comments

Comments

@sora91s
Copy link

sora91s commented Aug 12, 2024

Issue description

mysql error ERROR 3141 is not cathed in query()

sql: select JSON_TYPE('[1,2,3');
'[1,2,3' is not a json str, so So an error will be reported for JSON_TYPE

image

but when i use query, no error is reported

image

Example code

func main() {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "user", "pswd", "127.0.0.1", 3306, "database"))
if err != nil {
panic(err)
}
sqlStr := "select JSON_TYPE('[1,2,3');"
err = query(db, sqlStr)
fmt.Printf("error: %v", err)
}

func query(db *sql.DB, sqlStr string) error {
_, err := db.Query(sqlStr) // ignore_security_alert
if err != nil {
fmt.Println(fmt.Sprintf("db query err: %s, sql:%s", err.Error(), sqlStr))
panic(err)
}
return nil
}

Error log

no error log but it should be error

Configuration

Driver version (or git SHA):

Go version: run go version in your console

Go 1.21.3

Server version: E.g. MySQL 5.6, MariaDB 10.0.20

MySQL 8.0/5.7

Server OS: E.g. Debian 8.1 (Jessie), Windows 10

MacOs

@methane
Copy link
Member

methane commented Aug 12, 2024

No error while sending query.
You need to read result and check error.

@methane methane closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
@sora91s
Copy link
Author

sora91s commented Aug 16, 2024

image

no result in rows, what should i do?

@methane
Copy link
Member

methane commented Aug 16, 2024

You don't call rows.Err() nor rows.Close().

@sora91s
Copy link
Author

sora91s commented Aug 19, 2024

image

can you try it?

select JSON_TYPE('[1,2,3');

@sora91s
Copy link
Author

sora91s commented Aug 23, 2024

@methane Can you help me solve it?

@nussjustin
Copy link
Contributor

Rows.Err must be used after calling Rows.Next and returns the error, if any, that occurred during scanning.

Calling it before calling Rows.Next will always return nil.

From the docs of Rows.Next

Next prepares the next result row for reading with the Rows.Scan method. It returns true on success, or false if there is no next result row or an error happened while preparing it. Rows.Err should be consulted to distinguish between the two cases

Example:

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	log.SetFlags(0)

	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "root", "root", "127.0.0.1", 3306, "test"))
	if err != nil {
		log.Fatalf("error in sql.Open: %s", err)
	}
	defer db.Close()

	rows, err := db.Query(`SELECT JSON_TYPE('[1,2,3')`)
	if err != nil {
		log.Fatalf("error in db.Query: %s", err)
	}
	defer rows.Close()

	for rows.Next() {
		log.Println("scanned row")

		var dest any

		if err := rows.Scan(&dest); err != nil {
			log.Fatalf("error in rows.Scan: %s", err)
		}
	}

	if err := rows.Err(); err != nil {
		log.Fatalf("error in rows.Err: %s", err)
	}
}

Output:

error in rows.Err: Error 3141 (22032): Invalid JSON text in argument 1 to function json_type: "Missing a comma or ']' after an array element." at position 6.

You cal also use DB.QueryRow if you know the query only returns one row, in which case Row.Scan will return the error:

	// ...

	row := db.QueryRow(`SELECT JSON_TYPE('[1,2,3')`)

	var dest any

	if err := row.Scan(&dest); err != nil {
		log.Fatalf("error in rows.Scan: %s", err)
	}
}

@sora91s
Copy link
Author

sora91s commented Aug 28, 2024

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants