If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help! Inserting values onto mysql using VC++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-29-04, 21:50
athur1 athur1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 2
Red face Help! Inserting values onto mysql using VC++

Newbies,
I have trouble inserting the values onto the database, when i use,
retval = mysql_query(mysql, "INSERT INTO people (FirstName, LastName) VALUES ('%s', '%s')");
the database will store the value %s,
retval = mysql_query(mysql, "INSERT INTO people (FirstName, LastName) VALUES (%s, %s, First, Last)");
there is an error message state, please check the syntax for the correct version
retval = mysql_query(mysql, "INSERT INTO people (FirstName, LastName) VALUES ('%s', '%s', First, Last)");
There is an error message state that, column count doesn't match value count at row 1. Note that i have check the data field, type, size both the database and the code itself.
i am using mysql 4.0.21-win version
Please help, advice, need it urgently,
[/COLOR][/FONT]
#include <conio.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>

#include "windows.h"
#include "mysql.h"

MYSQL *mysql = NULL;
char First[10];
char Last[10];


void MySQLError(void)
{
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(1);
}

void ExecuteSQL(char *fmt, ...)
{
char SQL[1000] = {0};
int retval;
va_list args;
va_start(args, fmt);
if (_vsnprintf(SQL, sizeof(SQL)-1, fmt, args) < 0)
{
fprintf(stderr, "SQL query truncated! Increase SQL buffer size in ExecuteSQL\n");
exit(1);
}
va_end(args);
retval = mysql_query(mysql, SQL);
if (retval)
{
fprintf(stderr, "Original SQL Statement: %s\n", SQL);
fprintf(stderr, "%s\n", mysql_error(mysql));
exit(2);
}

}

int main()
{

UINT i;
UINT FieldCount;
MYSQL_RES *res = NULL;
MYSQL_ROW row = NULL;
MYSQL_FIELD *field;
int retval;

char *DBName = "Test";
char *TableName = "people";
char *Host = "localhost";
char *Username = "root";
char *Pass = "123456";
char *GetRecords = "SELECT FirstName, LastName FROM %s";



// Initialize MySQL
mysql = mysql_init(NULL);

// Connect to MySQL
if (!(mysql_real_connect(mysql, Host, Username, Pass, DBName, 0, 0, 0)))
MySQLError();

//User Input for the first name and the last name
printf("First Name:");
gets(First);
printf("Last Name:");
gets(Last);
printf("\n-------------\n");
printf("Data Entered: %s", First);
printf("\n");
printf("Data Entered: %s", Last);
printf("\n-------------\n");

//Query to execute the Insert statement
retval = mysql_query(mysql, "INSERT INTO people (FirstName, LastName) VALUES ('%s', '%s')");
if (retval)
{
fprintf(stderr, "\n%s\n", mysql_error(mysql));
exit(2);
}
else
{
printf("\nRecord Added\n");


// Execute query
ExecuteSQL(GetRecords, TableName);

// Store the recordset
if ((res = mysql_store_result(mysql)) == NULL)
MySQLError();

// Print the recordset
printf("Table %s\n", TableName);
while((field = mysql_fetch_field(res)) != NULL)
printf("%s\t", field->name);
printf("\n---------------------------------");
printf("\n");
FieldCount = mysql_num_fields(res);
while((row = mysql_fetch_row(res)) != NULL)
{
for (i = 0; i < FieldCount; i++)
printf("%s\t", row[i]);
printf("\n");
}
printf("\nTotal rows: %d\n", mysql_num_rows(res));

// Free the recordset
mysql_free_result(res);

// Close the connection
mysql_close(mysql);
}

printf("\nPrint any key to continue...\n");
return 0;
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On