Solved

Add C++ Variables into mySQL

Posted on 2004-04-25
7
422 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mysql Crashing Intermittently 16 104
simple shopping cart database structure 5 46
FrontEnd tools to create web database application 7 62
INDEX does not make a difference, why? 10 52
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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