this program written in C console and it connect to mysql server to create database, table and insert data into table. The problem is i learn C++ and my knowledge of input/output is CIN/COUT . i have no idea how to work with printf... from C. I can go search site tutorial about basic C standard input/output... but that gonna take time. On the other hand, i'm pretty good with C++. So, someone plz convert these code to C++, thank you.
#include <conio.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "winsock.h"
#include "mysql.h"
MYSQL *mysql = NULL;
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;
char *DBName = "Hoang";
char *TableName = "People";
char *CreateDB = "CREATE DATABASE IF NOT EXISTS %s";
char *DropTable = "DROP TABLE IF EXISTS %s";
char *CreateTable = "CREATE TABLE IF NOT EXISTS %s (\n"
"ID INT UNSIGNED AUTO_INCREMENT, \n"
"FirstName VARCHAR(32), \n"
"MiddleName VARCHAR(32), \n"
"LastName VARCHAR(32), \n"
"Address1 VARCHAR(50), \n"
"Address2 VARCHAR(50), \n"
"City VARCHAR(50), \n"
"State VARCHAR(2), \n"
"Zip VARCHAR(5), \n"
"Email VARCHAR(50), \n"
"Gender VARCHAR(1), \n"
"WorkPhone VARCHAR(50), \n"
"HomePhone VARCHAR(50), \n"
"CellPhone VARCHAR(50), \n"
"BirthDate DATE NULL, \n"
"Primary Key (ID) \n"
")\n";
char *AddRecord = "INSERT INTO %s (FirstName, MiddleName, LastName) VALUES ('FirstName %03d', 'MiddleName %03d', 'LastName %03d')";
char *GetRecords = "SELECT ID, FirstName, MiddleName, LastName FROM %s";
// Initialize MySQL
mysql = mysql_init(NULL);
// Connect to MySQL
if (!(mysql_real_connect(mysql, "server", "workstation", "4545", NULL, mysql->port, 0, 0)))
MySQLError();
// Create database
ExecuteSQL(CreateDB, DBName);
// Connect to temporary database
if (mysql_select_db(mysql, DBName))
MySQLError();
// Delete table if it already exists.
ExecuteSQL(DropTable, TableName);
// Create table
ExecuteSQL(CreateTable, TableName);
// Insert some data into the table.
for (i = 0; i < 2000; i++)
ExecuteSQL(AddRecord, TableName, i, i);
// 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");
FieldCount = mysql_num_fields(res);
while((row = mysql_fetch_row(res)) != NULL)
{
for (i = 0; i < FieldCount; i++)
printf("%s\t", row);
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");
getche();
}
My idea is i want user to control the creation of data and choose table like:
instead of : char *DBName="Hoang"; i will put in char *DBName;
then down the line where it said create database, i will put in cin >> DBName;
of course i have to use #include <iostream> using namespace std;
but i got this error: eh.h is only for C++!