FAQ
This is an old post but I thought I'd comment anyway.

- make a slice of data with length 0
- append data to the slice

the slice will grow to accomodate data. A method for number of rows isn't
really needed. Here's my code
func (self *UserDAO) GetUsers() (users []models.User){
selectStatement,err := self.db.Prepare("SELECT
userid,firstname,lastname,dob,email FROM users");
if(err!=nil){
panic(err.Error());
}
defer selectStatement.Close();
rows,err := selectStatement.Query();
if(err != nil){
panic(err.Error());
}
defer rows.Close();
users = make([]models.User,0);
var id int;
var fname, lname, dob, email string;

for rows.Next(){
err := rows.Scan(&id,&fname,&lname,&dob,&email);
if(err!=nil){
panic(err.Error());
}
users = append(users,*models.NewUser(id,fname,lname,dob,email));
}
return
}





On Saturday, 25 February 2012 22:33:45 UTC+4, Silas Baronda wrote:

Looking through the database/sql package I see no easy way to get the
number of rows the selected query is returning.

I'm looking for something like this:

rows, _ := stmt.Query("SELECT id FROM cars")
res := make(Result, rows.NumberOfRows())

Is easily possible with the current implementation?

If not, maybe a rows.NumberOfRows()
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dan Stroot at Jan 4, 2016 at 1:24 am
    This was really helpful - just wanted to say thanks for taking the time to
    post it.

    On Friday, August 2, 2013 at 7:24:54 AM UTC-7, W K Sheikh wrote:

    This is an old post but I thought I'd comment anyway.

    - make a slice of data with length 0
    - append data to the slice

    the slice will grow to accomodate data. A method for number of rows isn't
    really needed. Here's my code
    func (self *UserDAO) GetUsers() (users []models.User){
    selectStatement,err := self.db.Prepare("SELECT
    userid,firstname,lastname,dob,email FROM users");
    if(err!=nil){
    panic(err.Error());
    }
    defer selectStatement.Close();
    rows,err := selectStatement.Query();
    if(err != nil){
    panic(err.Error());
    }
    defer rows.Close();
    users = make([]models.User,0);
    var id int;
    var fname, lname, dob, email string;

    for rows.Next(){
    err := rows.Scan(&id,&fname,&lname,&dob,&email);
    if(err!=nil){
    panic(err.Error());
    }
    users = append(users,*models.NewUser(id,fname,lname,dob,email));
    }
    return
    }





    On Saturday, 25 February 2012 22:33:45 UTC+4, Silas Baronda wrote:

    Looking through the database/sql package I see no easy way to get the
    number of rows the selected query is returning.

    I'm looking for something like this:

    rows, _ := stmt.Query("SELECT id FROM cars")
    res := make(Result, rows.NumberOfRows())

    Is easily possible with the current implementation?

    If not, maybe a rows.NumberOfRows()
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 2, '13 at 3:39p
activeJan 4, '16 at 1:24a
posts2
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase