const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'mybatis_plus'
});
// simple query
connection.query('SELECT * FROM `user`',
function(err, results, fields) {
console.log(results); // results contains rows returned by server
// console.log(fields); // fields contains extra meta data about results, if available
}
);
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='root',
password='root',
database='mybatis_plus',
cursorclass=pymysql.cursors.DictCursor)
with connection:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `user` ( `name`,`age`,`email`) VALUES ( %s, %s, %s)"
cursor.execute(sql, ('newPystudy', '150', '[email protected]'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `uid`, `name`,`age`,`email` FROM `user`"
cursor.execute(sql)
# result = cursor.fetchone()
result = cursor.fetchall()
print(result)
print(len(result))
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
func main() {
fmt.Println("begin ...")
//for i := 0; i < 100; i++ {
// fmt.Println(i)
//}
//r := gin.Default()
//
//r.GET("/", func(c *gin.Context) {
// fmt.Println(c.Params)
//
// c.JSON(http.StatusOK, gin.H{
// "code": 20000,
// "message": "success",
// "data": gin.H{},
// })
//})
//
//r.Run(":8080")
testDB()
}
type User struct {
uid int
age int
name string
email string
}
func testDB() {
err := initDB() // 调用输出化数据库的函数
if err != nil {
fmt.Printf("init db failed,err:%v\n", err)
return
}
//queryRowDemo()
queryMultiRowDemo()
}
// 查询单条数据示例
func queryRowDemo() {
sqlStr := "select uid, name, age, email from user where uid=?"
var u User
// 非常重要:确保QueryRow之后调用Scan方法,否则持有的数据库链接不会被释放
err := db.QueryRow(sqlStr, 1).Scan(&u.uid, &u.name, &u.age, &u.email)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d email:%s \n", u.uid, u.name, u.age, u.email)
}
// 查询多条数据示例
func queryMultiRowDemo() {
sqlStr := "select uid, name, age, email from user"
rows, err := db.Query(sqlStr)
if err != nil {
fmt.Printf("query failed, err:%v\n", err)
return
}
// 非常重要:关闭rows释放持有的数据库链接
defer rows.Close()
var users []User
// 循环读取结果集中的数据
for rows.Next() {
var u User
err := rows.Scan(&u.uid, &u.name, &u.age, &u.email)
if err != nil {
fmt.Printf("scan failed, err:%v\n", err)
return
}
fmt.Printf("id:%d name:%s age:%d email:%s \n", u.uid, u.name, u.age, u.email)
users = append(users, u)
}
println("users -----------------------")
println(len(users))
for _, v := range users {
println(v.uid, v.name, v.age, v.email)
}
//var arr []int
//arr = append(arr, 1)
//arr = append(arr, 2)
//println(len(arr))
//
//for i, v := range arr {
// println(i, v)
//}
}
var db *sql.DB
// 定义一个初始化数据库的函数
func initDB() (err error) {
// DSN:Data Source Name
dsn := "root:root@tcp(127.0.0.1:3306)/mybatis_plus?charset=utf8mb4&parseTime=True"
// 不会校验账号密码是否正确
// 注意!!!这里不要使用:=,我们是给全局变量赋值,然后在main函数中使用全局变量db
db, err = sql.Open("mysql", dsn)
if err != nil {
return err
}
// 尝试与数据库建立连接(校验dsn是否正确)
err = db.Ping()
if err != nil {
return err
}
return nil
}