• 0

how to insert data in a table that has a foreign key of another table using c++ code ?


Question

I have a Table Bailleur

IDB IDF nameb Adresseb telb

----+------+------+---------------+-------

Where IDF is a foreign key to a table named Fournisseur

The Addresses Table:

IDF namef adressef telf 
----+-------+----------+------+

This is a ONE-TO-N Relationship: Each Bailleur Has N fournisseur and each fournisseur has N bailleur

when i generated MPD I have a new table named fournir

IDF IDB

----+------

i want to insert data in "bailleur" by typing the name of founisseur which is namef and the IDF of "fournisseur" will be inserted into column IDF of table "bailleur" all this using c++

here is what ive tried i alredy inserted data in table "Fournisseur"

here im using bailleur in a class called bailleur

int qstate1;
MYSQL_ROW row1;
MYSQL_RES* res1;
void Bailleur::addb( MYSQL* conn){

string idf="";

cin.ignore(1, '\n');
cout << "Enter Nom du bailleur: ";
getline(cin, nomb);
cout << "Enter Adresse: ";
getline(cin, adresseb);
cout << "Enter tel : ";
getline(cin, telb);
cout<<"tapez le nom du fournisseur";
getline(cin,nomf);

string insert_query = ""insert into bailleur (IDF,nomb, tel_b ,adresseb) 
   values (select IDF from fournisseur where nomf = '"+nomfou+"' 
  ,'"+nomb+"','"+telb+"','"+adresseb+"')";;


const char* q = insert_query.c_str(); 

qstate1 = mysql_query(conn, q);

if (!qstate1)
{
    cout << endl << "Successfully added in database." << endl;
}


 else
{
   cout << "Query Execution Problem!" << mysql_errno(conn) << endl;
}
}

i expect output to be successfully added in database but its alway query execution problem

sorry for the long post this is my first time posting in neowin

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.