Solved

Add C++ Variables into mySQL

Posted on 2004-04-25
7
419 Views
Last Modified: 2008-03-06
What is the syntax of adding C++ variables into mySQL?
Could anyone raise an example?
I've tried
insert into aTable (field1, field2) values (variable1, variable2)
But mySQL throws error messages at me saying varible1 is not in the field list.
Thanks for your attention
Jack
0
Comment
Question by:luckie
  • 5
7 Comments
 
LVL 7

Expert Comment

by:madwax
ID: 10912956
INSERT INTO myTable(field1,field2) VALUES('var2','var2')

//madwax
0
 

Author Comment

by:luckie
ID: 10914711
Hi madwax,
Are you sure? :)
'var2' inserts the string 'var2' into the database, not the its variable representation
It returns duplicate entry right after the second insert.... :)
Thanks
Jack
0
 

Author Comment

by:luckie
ID: 10915051
int var2 = 3;

Insert into table1 (field1, field2) values (var2, 'hello')
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 20

Accepted Solution

by:
ikework earned 50 total points
ID: 10943332
hi luckie, i guess you mean something like this;


int var0 = 0;
int var1 = 20;

char pszSql[ 200 ];
char pszBuffer[ 10 ];

// put the base into the sql-string
strcpy( pszSQL, "insert into aTable (field1, field2) values (" );
// convert var0 into a string
sprintf( pszBuffer, "%d", var0 );
// add converted var0 to sql-string
strcat( pszSQL, pszBuffer );
strcat( pszSQL, "," );
// convert var1 into a string
sprintf( pszBuffer, "%d", var1 );
// add converted var1 to sql-string
strcat( pszSQL, pszBuffer );
strcat( pszSQL, ")" );


now you have your sql-string, hope it helps a bit ...

good luck ;-)

maik

0
 

Author Comment

by:luckie
ID: 10946175
Okay. Thanks a lot :)
0
 

Author Comment

by:luckie
ID: 10946192
Wait a minute... I've got a question...:O
How come when the field and the variables are both of type integer, why do you need to convert them into varchars. As in my database, they are of integer types.
Thanks for any attempt for solving my question
Jack
0
 

Author Comment

by:luckie
ID: 10947057
rc = SQLExecDirect (hstmt, (SQLCHAR *) "INSERT INTO aTable (a,b,...  )  values (var1, '10',  '', '', '', '', '', '', '', '','', '', c);", SQL_NTS);
mystmt (hstmt, rc);


where var1 is already a character pointer (C String)
and c is a stored procedure containing binary data.
Thanks
                              
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now