Salve,
da poco mi sono affacciato al mondo della programmazione in c++ ed ho un piccolo problema ovvero, volendo inserire un record in una tabella del DB MySql ricevo con il comando sprintf(........) il seguente errore "Error Commands out of sync"... come IDE uso DEV c++ vi posto il codice
nella tabella "arch_fe" il campo 'chiave primaria' e "Id".
codice:
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <mysql/mysql.h>
using namespace std;
int main()
{
//connection params
char *host = "localhost";
char *user = "root";
char *pass = "prova";
char *db = "archivio_ferrucci";
//sock
MYSQL *sock;
sock = mysql_init(0);
if (sock) cout << "sock handle ok!" << endl;
else {
cout << "sock handle failed!" << mysql_error(sock) << endl;
}
//connessione
if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
cout << "connection ok!" << endl;
else {
cout << "connection fail: " << mysql_error(sock) << endl;
}
if (mysql_query(sock,"select Id,Scaffale,Cassetto,Grado,Cognome,Nome,giorno,mese,anno,Luogo_nascita,note from arch_fe"))
{
cout << "Errore durante l'esecuzione della query " << endl << mysql_error(sock) << endl;
system("PAUSE");
return -1;
}
MYSQL_RES *result;
result = mysql_store_result(sock);
if (resullt
{
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))){
cout << " Scaffale:" << row[1];
cout << " Cassetto:" << row[2];
cout << " Grado:" << row[3];
cout << " Cognome:" << row[4];
cout << " Nome:" << row[5];
cout << " Giorno:" << row[6];
cout << " Mese:" << row[7];
cout << " Anno:" << row[8];
cout << " Luogo di nascita:" << row[9];
cout << " Note:" << row[10]<<endl;
}
mysql_free_result(result);
}
/*
###################################################
ESEMPIO DI UNA ESECUZIONE DI UNA QUERY DI COMANDO
##################################################
*/
int Scaffale;
int Cassetto;
char Grado[20];
char Cognome[50];
char Nome[50];
int Giorno;
int Mese;
int Anno;
char Luogo_nascita[50];
char Note[50];
char sql[255];
cout<<endl << "INSERIRE LO SCAFFALE :" ;
cin>>Scaffale;
cout<<endl << "INSERIRE IL CASSETTO :" ;
cin>>Cassetto;
cout<<endl << "INSERIRE IL GRADO :" ;
cin>>Grado;
cout<<endl << "INSERIRE IL COGNOME CLIENTE :" ;
cin>>Cognome;
cout<<endl << "INSERIRE IL NOME CLIENTE :" ;
cin>>Nome;
cout<<endl << "INSERIRE IL GIORNO :" ;
cin>>Giorno;
cout<<endl << "INSERIRE IL COGNOME CLIENTE :" ;
cin>>Mese;
cout<<endl << "INSERIRE IL COGNOME CLIENTE :" ;
cin>>Anno;
cout<<endl << "INSERIRE IL COGNOME CLIENTE :" ;
cin>>Luogo_nascita;
cout<<endl << "INSERIRE IL COGNOME CLIENTE :" ;
cin>>Note;
sprintf(sql, "INSERT INTO arch_fe VALUES (%d,%d,%s,%s,%s,%d,%d,%d,%s,%s)",Scaffale,Cassetto,Grado,Cognome,Nome,Giorno,Mese,Anno,Luogo_nascita,Note);
cout<<sql;
mysql_query(sock,sql);
if (mysql_affected_rows(sock) != 1) {
cout << "Errore durante l'esecuzione della query " << endl << mysql_error(sock) << endl;
system("PAUSE");
return -1;
}
system("PAUSE");
mysql_close(sock);
return EXIT_SUCCESS;
}
Grato per l'aiuto ringrazio per la collaborazione