Skip to content

DB.Exec() always returns "no RowsAffected available after DDL statement". [moved] #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
julienschmidt opened this issue Jan 5, 2013 · 8 comments
Assignees
Labels
Milestone

Comments

@julienschmidt
Copy link
Member

This is Issue 16 moved from a Google Code project.
Added by 2012-10-28T06:12:01.000Z by [email protected].
Please review that bug for more context and additional comments, but update this bug.
Closed (Fixed).

Original labels: Type-Defect, Priority-Critical

Original description

<b>Problem summary:</b>
DB.Exec() always returns &quot;no RowsAffected available after DDL statement&quot; even if it affected some rows indeed.

<b>Used go-mysql-version (e.g. May 26, 2012):</b>
May 30, 2012


<b>MySQL-Server version (e.g. MySQL-Server 5.5 or MariaDB 5.1):</b>
mysql  Ver 14.14 Distrib 5.5.24, for osx10.6 (i386) using readline 5.1


<b>Used Operation System (e.g. Windows 7 64bit, Ubuntu 12.04 64bit):</b>
Mac OS X 10.7.5



<b>Example code which reproduces the error (optional):</b>

// 1. Make sure the Test table in MyDatabase has a row with id 1.
// 2. Run the following code with go(version go1.0.3).

package main

import (
    &quot;fmt&quot;
    &quot;database/sql&quot;
    _ &quot;code.google.com/p/go-mysql-driver/mysql&quot;
    )

func main() {
    db, err := sql.Open(&quot;mysql&quot;, &quot;root:password@/MyDatabase?charset=utf8&quot;)
    if err != nil {
        panic(err)
    }

    if result, err := db.Exec(&quot;delete from Test where id=1&quot;); err != nil {
        panic(err)
    } else {
        fmt.Printf(&quot;result=%#v\n&quot;, result)
        if affected, err := result.RowsAffected(); err != nil {
            fmt.Println(err)
        } else {
            fmt.Printf(&quot;Deleted %v rows.\n&quot;, affected)
        }
    }
}
@rjmcguire
Copy link

This still happens for me. Just upgraded to this version. The above commit is in the source so I assume that I
am using the version where this is supposed to be fixed.
Any ideas?

@rjmcguire
Copy link

It seems that you have made affectedrows=0 an error. Is that your intention?

julienschmidt added a commit that referenced this issue Feb 20, 2013
@julienschmidt
Copy link
Member Author

No sorry, it seems like some code remained there after a (too) long coding night ...

@rjmcguire
Copy link

I don't know how to commit back to my github.com repo to do a pull request
but here are the changes I did:
diff --git a/connection.go b/connection.go
index 3a3bb4c..4b6a09e 100644
--- a/connection.go
+++ b/connection.go
@@ -206,7 +206,7 @@ func (mc *mysqlConn) Exec(query string, args
[]driver.Value) (driver.Result, err
}

    if mc.affectedRows == 0 && mc.insertId == 0 {
  •           return driver.ResultNoRows, e
    
  •           return &mysqlResult{}, nil //driver.ResultNoRows, e
    }
    
    return &mysqlResult{
    

    diff --git a/statement.go b/statement.go
    index 746c9dc..dccc64f 100644
    --- a/statement.go
    +++ b/statement.go
    @@ -73,7 +73,7 @@ func (stmt mysqlStmt) Exec(args []driver.Value)
    (driver.Result, error) {
    }

    if stmt.mc.affectedRows == 0 {
    
  •           return driver.ResultNoRows, nil
    
  •           return &mysqlResult{}, nil //driver.ResultNoRows, nil
    }
    
    return mysqlResult{
    

On Wed, Feb 20, 2013 at 4:46 PM, Julien Schmidt [email protected]:

No sorry, it seems like some code remained there after a (too) long coding
night ...


Reply to this email directly or view it on GitHubhttps://github.com//issues/16#issuecomment-13835105.

@julienschmidt
Copy link
Member Author

Oops.. forgot the prepared statements.
The ResultNoRows error is for situations when LastInserID and AffectedRows aren't available, but in fact this is in MySQL never the case (every OK response packet contains these) so this code passage was complete nonsense.

The result of returning &mysqlResult{} is the same as returning just the normal result - both report 0 (default value) for LastInserID and AffectedRows

@rjmcguire
Copy link

Thanks, I'm not sure if we're talking about the same thing but the reason I
had to change those lines is because driver.ResultNoRows.AffectedRows()
always returns an error.

BTW, thanks for implementing go-sql-driver. It works, and its always good
when something does what it says on the tin.

Cheers,

On Wed, Feb 20, 2013 at 5:03 PM, Julien Schmidt [email protected]:

Oops.. forgot the statements.
The ResultNoRows error is for situations when LastInserID and AffectedRowsaren't available, but in fact this is in MySQL never the case (every
OK response packet contains these) so this code passage was complete
nonsense.

The result of returning &mysqlResult{} is the same as returning just the
normal result - both repotz 0 for LastInserID and AffectedRows


Reply to this email directly or view it on GitHubhttps://github.com//issues/16#issuecomment-13836004.

julienschmidt added a commit that referenced this issue Feb 20, 2013
@julienschmidt
Copy link
Member Author

Which error do you get? no RowsAffected available after DDL statement? This one should be fixed now 😄

BTW, thanks for reporting that something didn't work as expected 😉

julienschmidt added a commit that referenced this issue Feb 20, 2013
@ghost ghost assigned julienschmidt Feb 20, 2013
@rjmcguire
Copy link

Thats the one.

Its from database/sql/sql.go if I remember correctly.

On Wed, Feb 20, 2013 at 5:17 PM, Julien Schmidt [email protected]:

Which error do you get? no RowsAffected available after DDL statement?
This one should be fixed now [image: 😄]

BTW, thanks for reporting that something didn't work as expected [image:
😉]


Reply to this email directly or view it on GitHubhttps://github.com//issues/16#issuecomment-13836777.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants