英文:
How to set Limit or query Go Language Mysql?
问题
如何在Go语言中设置mysql的限制?我在php中尝试过,它可以工作,但在Go语言中,它显示<nil>
值,不知道如何修复。
php代码示例:
$sql = "SELECT My_Data FROM page_data";
// 或者
$sql = "SELECT My_Data FROM page_data limit 0,10";
Go代码示例:
myquery, err := db.Prepare("SELECT pages_old FROM app_library")
fmt.Println("Data---", myquery)
当我运行以下命令时:
q := "SELECT `data` FROM `page_data`"
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var data string // 与数据库中的类型相同
if err := rows.Scan(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
然后出现以下错误:
Error: error:-- commands out of sync. Did you run multiple statements at once?
panic: runtime error: invalid memory address or nil pointer dereference
panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10bee7a]
英文:
How to Set mysql limit in Go Language? I try it to php it is working but in the Go language, it show <nil>
value didn't know how to fixed
php:--- $sql = "SELECT My_Data FROM page_data"; or $sql = "SELECT My_Data FROM page_data limit 0,10";
Go :- SHOW <nil> >>>
myquery, err := db.Prepare("SELECT pages_old FROM app_library")
fmt.Println("Data---", myquery)
When I hit the below command :-
q := "SELECT data
FROM page_data
"
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var data string // same database type
if err := rows.Scan(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
Then Got the below error:
> Error: error :-- commands out of sync. Did you run multiple statements
> at once? panic: runtime error: invalid memory address or nil pointer
> dereference panic: runtime error: invalid memory address or nil
> pointer dereference [signal SIGSEGV: segmentation violation code=0x1
> addr=0x0 pc=0x10bee7a]
答案1
得分: 1
[Prepare][1] 用于创建一个预处理语句,以供以后查询或执行使用。
如果你想执行一个返回行的查询,请使用 [Query][2]。
完整示例:
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
const (
dbUser = ""
dbPassword = ""
dbName = ""
)
func main() {
db, err := sql.Open("mysql", dbUser+":"+dbPassword+"@/"+dbName+"?charset=utf8")
if err != nil {
log.Fatal(err)
}
q := "SELECT `data` FROM `page_data`"
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var data string // 相同的数据库类型
if err := rows.Scan(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
}
示例已经被编辑过了。
[1]: https://golang.org/pkg/database/sql/#DB.Prepare
[2]: https://golang.org/pkg/database/sql/#DB.Query
英文:
[Prepare][1] used to creates a prepared statement for later queries or executions.
if you want to executes a query that returns rows use [Query][2]
Full Example:
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
)
const (
dbUser = ""
dbPassword = ""
dbName = ""
)
func main() {
db, err := sql.Open("mysql", dbUser+":"+dbPassword+"@/"+dbName+"?charset=utf8")
if err != nil {
log.Fatal(err)
}
q := "SELECT `data` FROM `page_data`"
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
for rows.Next() {
var data string // same database type
if err := rows.Scan(&data); err != nil {
log.Fatal(err)
}
fmt.Println(data)
}
}
example it has been edited
[1]: https://golang.org/pkg/database/sql/#DB.Prepare
[2]: https://golang.org/pkg/database/sql/#DB.Query
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论