2016-11-23 43 views
0
void SignUp() 
{ 
    MYSQL* conn; 
    MYSQL_ROW row; 
    MYSQL_RES* res; 
    int nQueryState = 0; 
    int nId   = 0; 
    char szName[45]; 
    char szPassWord[45]; 
    char szQuestion[45]; 
    char szPhone[45]; 

    cout<<"Enter the Id"; 
    cin>>nId; 

    cout<<"Enter the Name"; 
    cin>>szName; 

    cout<<"Enter the Password"; 
    cin>>szPassWord; 

    cout<<"Enter the Question"; 
    cin>>szQuestion; 

    cout<<"Enter the Phone"; 
    cin>>szPhone; 

conn = mysql_init(0); 
conn =mysql_real_connect(conn,"localhost","root","12356","hari",0,NULL,0); 
nQueryState = mysql_query(conn,"insert into userdetails values(nId,szName,szPassword,szQuestion,szPhone)"); 

if(0!=nQueryState) 
{ 
    cout<<"\n\nConnection not Established"; 
} 
} 

我想輸入詳細信息到名爲userdetails的表中。它給了我一個輸出,使得連接不被建立。請通過它並幫助我。我使用的代碼塊IDE和mySql5.1如何使用cpp程序將值插入數據庫(mySql)?

+2

你應該添加一些錯誤檢查,如檢查是否'mysql_init'或'mysql_real_connect'沒有按」 t返回一個空指針。 –

+0

插入不指定'userdetails'表的列名,在某些情況下這可能是個問題。檢查'mysql_query'是否報告錯誤並打印出來。 – karastojko

回答

0

嘗試如下:

MYSQL mysql,*connection; 
MYSQL_RES result; 
MYSQL_ROW row; 


mysql_init(&mysql); 
connection =mysql_real_connect(&mysql,"localhost","root","12356","hari",0,NULL,0); 

if (connection==NULL) 
{ 
    cout<<mysql_error(&mysql)<<endl; 
} 
else{ 
    nQueryState = mysql_query(&mysql,"insert into userdetails values('"+nId+"','"+szName+"','"+szPassword+"','"+szQuestion+"','"+szPhone+"')"); 

    if (nQueryState !=0) { 
    cout << mysql_error(connection) << endl; 
    return 1; 
    } 
} 

mysql_close(&mysql); 

參見:http://www.codeproject.com/Questions/337901/How-to-insert-data-in-database-using-Cplusplus

+0

這裏顯示了一個錯誤,使得szname不是一個字段。實際上字段名稱是name.I想要將存儲在szname中的szName.Value中存儲的用戶輸入插入到database.Plz幫助中。在此先感謝 –

+0

我改變了插入查詢。請檢查一下。 –

+0

我改變了插入Query.But它顯示了一個語法錯誤,例如:「類型'const char *'和'const char [4]'的無效操作數到二進制'operator +'|」.Plz幫助我避免這個錯誤。預先感謝 –

相關問題