亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在戈朗中查詢 sqlite3?

如何在戈朗中查詢 sqlite3?

Go
拉莫斯之舞 2022-09-12 16:06:36
我的表函數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是我們選擇的指定,但正在給予,我不知道如何實現,我知道這是一個非常基本但字面搜索,但沒有得到任何適合我需求的東西,所以需要幫助
查看完整描述

2 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

從文檔中:


Exec 執行不返回行的查詢。例如:插入和更新。


請嘗試。我沒有針對您的數據集(顯然)對其進行測試,但希望它能為您提供一些良好的方向。此外,您還應該正確處理錯誤。Query


type 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.Mutex


func createTransactionTable(...) {

    m.Lock()

    defer m.Unlock()

    // Perform your query here

}


查看完整回答
反對 回復 2022-09-12
?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

我認為如果你像這樣使用驅動程序會更容易


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 this


createStudentTableSQL := `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")

您可以使用這樣的方法


查看完整回答
反對 回復 2022-09-12
  • 2 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號