猿问

如何在戈朗中查询 sqlite3?

我的表函数


func createTransactionTable(db *sql.DB) {

    transaction := `CREATE TABLE IF NOT EXISTS Transaction_history(

        "rollno" INTEGER UNSIGNED NOT NULL,

        "isReward" INTEGER UNSIGNED NOT NULL,

        "transfered_to" INTEGER UNSIGNED NOT NULL,

        "transfered_amount" INTEGER UNSIGNED NOT NULL,

        "redeems" INTEGER UNSIGNED NOT NULL,

        "date" TEXT NOT NULL

        );`

    statement, err := db.Prepare(transaction)

    if err != nil {

        panic(err)

    }

    statement.Exec()

    fmt.Println("trasaction  table created")

}

查询功能


count, err := db.Exec(`SELECT count(isReward) from Transaction_history WHERE rollno = ? AND isReward = ?`, rollno, 1)

if err != nil {

    panic(err)

}

if count != 0 {

    return true

}

return false

我试图在我的表格中找到行数,其中isReward = 1和rollno是我们选择的指定,但正在给予,我不知道如何实现,我知道这是一个非常基本但字面搜索,但没有得到任何适合我需求的东西,所以需要帮助


拉莫斯之舞
浏览 107回答 2
2回答

智慧大石

从文档中:Exec 执行不返回行的查询。例如:插入和更新。请尝试。我没有针对您的数据集(显然)对其进行测试,但希望它能为您提供一些良好的方向。此外,您还应该正确处理错误。Querytype row struct {    Count int `json:"count"`}response, _ := db.Query("SELECT count(isReward) as count from Transaction_history WHERE rollno = ? AND isReward = ?", rollno, 1)var rows []row_ = response.Scan(&rows)count := rows[0].Count如果收到数据库锁定错误,请确保未尝试同时查询 SQLite。您可以创建一个全局互斥体,并确保针对数据库的每个查询都获取锁。var m sync.Mutexfunc createTransactionTable(...) {    m.Lock()    defer m.Unlock()    // Perform your query here}

吃鸡游戏

我认为如果你像这样使用驱动程序会更容易sqliteDatabase, _ := sql.Open    ("sqlite3", "./sqlite-database.db") // Open the created SQLite File    defer sqliteDatabase.Close() // Defer Closing the database    createTable(sqliteDatabase) // Create Database Tables        // INSERT RECORDS    insertStudent(sqliteDatabase, "0001", "Liana Kim", "Bachelor")// and InsertStudent func like thiscreateStudentTableSQL := `CREATE TABLE student (        "idStudent" integer NOT NULL PRIMARY KEY AUTOINCREMENT,             "code" TEXT,        "name" TEXT,        "program" TEXT            );`     log.Println("Create student table...")    statement, err := db.Prepare(createStudentTableSQL) // Prepare SQL Statement    if err != nil {        log.Fatal(err.Error())    }    statement.Exec() // Execute SQL Statements    log.Println("student table created")您可以使用这样的方法
随时随地看视频慕课网APP

相关分类

Go
我要回答