Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > BOOK Beginning Linux Programming, 3rd Edition
|
BOOK Beginning Linux Programming, 3rd Edition
This is the forum to discuss the Wrox book Beginning Linux Programming, 2nd Edition by Richard Stones, Neil Matthew, Alan Cox; ISBN: 9780764543739
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK Beginning Linux Programming, 3rd Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2007, 04:15 AM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default MYSQL Prepared Statements for updating failed!?

 Hi everybody,
 These days I was writing a program with C API for MYSQL in Linux,and I encountered a lot of problems .And I begin to understand what Java and .NET bring to we programmers.
 first I want to thank our authors for writing this great book,it was excellent. No doubt it was one of the best book I have ever read. Well done :)
 Anyway,it can not cover all the details of the subjects it discuss, so I am asking a question here:
 It was about Prepared Statements for updating using MYSQL_BIND:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>

int main(int argc, char *argv[])
{

  MYSQL *conn;         // connection to MySQL server
  MYSQL_STMT *stmt;    // data structure for the prepared statement
  MYSQL_BIND bind[3]; // description of the parameters
  char *update =   "UPDATE Roles  SET name=? , description=?  WHERE RoleId=?";
  char name_buf[32];
  char description_buf[256];
  unsigned long name_len, description_len;
  int roleId;
    int roleId_is_null;

// connection to MySQL
conn = mysql_init(NULL);
mysql_options(conn, MYSQL_READ_DEFAULT_FILE, "");
if(mysql_real_connect(
      conn, hostName, userName, passwd,
      dbName, PortNumber,UnixSocketName,Flags) == NULL) {
    fprintf(stderr, "sorry, no database connection ...\n");
    return 1;
  }

// create statement structure
stmt = mysql_stmt_init(conn);
// initialize prepared statement
mysql_stmt_prepare(stmt, update, strlen(update));
// define parameters of the prepared statement
memset(bind, 0, sizeof(bind));
bind[0].buffer_type    = FIELD_TYPE_VAR_STRING;
bind[0].buffer         = name_buf;
bind[0].buffer_length = 32;
bind[0].length         = &name_len;
bind[1].buffer_type    = FIELD_TYPE_VAR_STRING;
bind[1].buffer         = description_buf;
bind[1].buffer_length  = 256;
bind[1].length         = &description_len;
bind[2].buffer_type    = FIELD_TYPE_LONG;
bind[2].buffer         = (gptr) &roleId;
bind[2].is_null        = &roleId_is_null;

mysql_stmt_bind_param(stmt, bind);
// execute command for the first time
strcpy(name_buf, "roleName");
name_len = strlen(name_buf);
strcpy(description_buf, "test prepared statements");
description_len = strlen(description_buf);
roleId=1;
roleId_is_null = 0;

int error=mysql_stmt_execute(stmt);
if(error){
    printf("error occur: %s\n",mysql_error(conn));
    exit(1);
}
/*
printf("new title with titleId=%d has been inserted\n",
       (int) mysql_insert_id(conn));
*/
printf("%d rows affected \n",mysql_affected_rows(conn));

// close statement and connection
mysql_stmt_close(stmt);
mysql_close(conn);
return 0;
}
The Result is : 0 rows affected .

But When I change the program for Inserting and Deleting,it works.
Code:
 INSERTING: char *insert =   "INSERT INTO Roles (RoleId,Name,Description) VALUES(?,?,?)";
 UPDATING: char *delete =   "DELETE FROM Roles WHERE RoleId=?";
I am rather confused,and helpless,for the resource for the C API for MYSQL is so little. Hope someone can help.Thanks.

------------------------------------------------------------------------
We learn from the history that we do not learn from the history
__________________
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
 
Old December 9th, 2007, 09:53 AM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

I have workout it.But I don't know why.
I try many ways,and suddenly it works,but I could not see any difference .

------------------------------------------------------------------------
We learn from the history that we do not learn from the history





Similar Threads
Thread Thread Starter Forum Replies Last Post
starting MySQL Failed-Please Help annumol MySQL 1 June 25th, 2008 03:49 AM
Case Select Statements & Updating the Database jackiew General .NET 1 April 11th, 2006 11:47 AM
mysql connection failed in linux kalai JSP Basics 0 December 14th, 2004 03:40 AM
updating mysql wfrisch PHP Databases 2 August 14th, 2003 01:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.