dougan4663 2013-09-03 17:41 采纳率: 0%
浏览 23

我的Result.Scan()有什么错误?

I'm trying to learn Go and am trying to scan rows into values which I will then assign to a struct. It seems like not all values are making it into the struct but I don't know why. Below is the full script. Running the query against the server actually returns results. However, when I test in the Scanned results, no data appears. In this case, the expected results would be "en" "en" Below is the script. I'm pretty sure I'm messing something up with my struct or with my types (or both).

package main
import (
    "log"
    _ "github.com/lib/pq"
    "fmt"
    "database/sql"  
)
//local variables
var cnstr = "cnstr"
var dbdriver = "postgres"
//data structure
type WordData struct {
    Word string
    WordID int  
    Root string
    RootID int
    PartOfSpeech string
    Case *string
    Person *string
    Gender *string
    Number *string
    Tense *string
    Voice *string
    Mood *string    
    Transliteration *string
    Root_Definition *string
}

func main() {
    db, _ := sql.Open(dbdriver, cnstr)
    var word string
            var wordid int
            var root string
            var rootid int
            var partofspeech string
            var caseval *string
            var person *string
            var gender *string
            var number *string
            var tense *string
            var voice *string
            var mood *string            
            var transliteration *string         
            var definition *string  
    if db != nil {
        err := db.QueryRow(`select 
                                    morph as word, 
                                    morph_id as wordid, 
                                    lemma as root, 
                                    lemma_id as rootid,
                                    part_of_speech as partofspeech,
                                    "case",
                                    person, gender, number, tense, voice, mood,                                     
                                    transliteration,                                                                        
                                    definition
                                 from texts where sort_order = 65006`).Scan(&word,&wordid,&root,&rootid,&partofspeech,&caseval,&person,&gender,&number,&tense,&voice,&mood,&transliteration,&definition)

        switch {
        case err == sql.ErrNoRows:
                log.Printf("No user with that ID.")
        case err != nil:
                log.Fatal(err)
        default:
                /*wd := new (WordData)
                wd.Word = word
                wd.WordID = wordid
                wd.Root = root
                wd.RootID = rootid
                wd.PartOfSpeech = partofspeech
                wd.Case = caseval
                wd.Person = person
                wd.Gender = gender
                wd.Number = number
                wd.Tense = tense
                wd.Voice = voice
                wd.Mood = mood              
                wd.Transliteration = transliteration            
                wd.Root_Definition = definition  */             
                wd := WordData{
                        word,
                        wordid,
                        root,
                        rootid,
                        partofspeech,
                        caseval,
                        person,
                        gender,
                        number,
                        tense,
                        voice,
                        mood,                       
                        transliteration,                        
                        definition}             
                fmt.Printf("Printf: %s",wd.Transliteration)
                fmt.Println(transliteration)    
        }


            //or, comment out and use the other initializer type


    }
    db.Close()
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看