473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Help with mysql C API

38
I would like to insert into my database the character stored inside the variable t. How do I do that?
This is my code.
Expand|Select|Wrap|Line Numbers
  1. #include <my_global.h> 
  2. #include <mysql.h> 
  3. #include<time.h> 
  4. #include<stdio.h> 
  5. int main(int argc, char **argv) 
  6. char t='alpha'; 
  7. MYSQL *conn; 
  8.  
  9. conn = mysql_init(NULL); 
  10. mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0); 
  11. mysql_query(conn, "CREATE TABLE csetech_test (id int not null auto_increment,name varchar(20), password int,time varchar (20),sensor varchar(10), primary key(id))"); 
  12. mysql_query(conn, "INSERT INTO csetech_test(name,password,time, sensor) VALUES(("%s",1001,now(),'0')",t); 
  13.  
  14. mysql_close(conn); 
  15. printf("%s",t);
  16. _getch();
  17.  
Thanks
Jan 14 '09 #1
7 5182
Banfa
9,065 Expert Mod 8TB
char t='alpha';

is not valid, in fact I am surprised it compiled without warnings and if it did you should consider switching to a higher warning level on your compiler or getting a new compiler.

t is declared as a single character but alpha is a string of 5 characters, however you have surrounded them with ' indicating only a single character is contained.

mysql_query is not a varidac function, you can not use it like you use printf. You have to create the query string first and then call mysql_query.

If you managed to get this to compile and link then you would get the string %s inserted into your database.
Jan 14 '09 #2
poreko
38
Thank you for your reply Banfa.
However I am a bit confused, what do you mean by query string?
Can you please explain?
Thanks
Jan 14 '09 #3
Banfa
9,065 Expert Mod 8TB
The query string is the string containing the SQL command. Since all commands are queries it is commonly called the query string
Jan 14 '09 #4
poreko
38
@poreko
Guys I am still stuck by this. Any help will be greatly appreciated.
Thanks
Jan 14 '09 #5
donbock
2,426 Expert 2GB
How about if you explain what you want to happen. Use sentences, not code.

What are the function prototypes for mysql_real_connect(), mysql_query(), and mysql_close()? Notice that Banfa has suggested that you are not complying with the prototype for mysql_query().

Please change
char t = 'alpha';
to
char *t = "alpha";
for the reasons presented earlier by Banfa.

Presumably you posted this question because your code doesn't work. Please describe the ways in which it has disappointed you. Compiler errors? Run-time errors? Incorrect execution?
Jan 14 '09 #6
poreko
38
I have tried to do what Banfa has told me but now the code is not eev compiling. The more I try to do it the more I have errors.
Expand|Select|Wrap|Line Numbers
  1. #include <my_global.h> 
  2. #include <mysql.h> 
  3. #include<string.h>
  4. #include<conio.h>
  5. #include<time.h> 
  6. #include<stdio.h> 
  7. int main(int argc, char **argv) 
  8. char t[60]="alpha"; 
  9. MYSQL *conn; 
  10. char query[2000];
  11.  
  12. conn = mysql_init(NULL); 
  13. mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0); 
  14. //mysql_query(conn, "CREATE TABLE csetech_test2 (id int not null auto_increment,name varchar(20), password int,time varchar (20),sensor varchar(10), primary key(id))"); 
  15.  
  16. query[2000]= ("insert into csetech_test2(name) values ('%s')", t));
  17. mysql_query(conn, query); 
  18.  
  19. mysql_close(conn); 
  20. //printf("%s\n",t);
  21. _getch();
  22.  
  23.  
I am getting the following error:warning C4047: '=' : 'char' differs in levels of indirection from 'char *'

Thanks
Jan 14 '09 #7
poreko
38
Guy,
I have solved the problem thanks a lot. Here is the solution:
Expand|Select|Wrap|Line Numbers
  1. #include <my_global.h> 
  2. #include <mysql.h> 
  3. #include<string.h>
  4. #include<conio.h>
  5. #include<time.h> 
  6. #include<stdio.h> 
  7. int main(int argc, char **argv) 
  8. char t[60]="alpha"; 
  9. MYSQL *conn; 
  10. char query[2000];
  11.  
  12.  
  13. conn = mysql_init(NULL); 
  14. mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0); 
  15. //mysql_query(conn, "CREATE TABLE csetech_test2 (id int not null auto_increment,name varchar(20), password int,time varchar (20),sensor varchar(10), primary key(id))"); 
  16.  
  17. sprintf(query,"insert into csetech_test2(name,password,time,sensor) values ('%s',1234,NOW(),2)", t);
  18. mysql_query(conn, query); 
  19.  
  20. mysql_close(conn); 
  21. _getch();
  22.  
  23. [SIZE=2]
[/SIZE]
Jan 14 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: pancho | last post by:
Greetings, I need help configuring/building PHP3 with MySQL as a DSO on a Solaris 8 box - this module is needed to host some existing sites I will be migrating Note. I built PHP4 from source and...
0
by: root | last post by:
hi there, I've tried to install mysql-3.23.55.tar.gz but failed. Firstly, I've created directory /home/users/mysql and add group for mysql. Those are the command that I've used previously: ...
0
by: Richard Gabriel | last post by:
Hi everyone, Since we upgraded to MySQL 4.0.13 from 3.23, we have been getting table corruption often. It happens about twice per week (with about 500 queries per second average). I have even...
0
by: Ryan Schefke | last post by:
------=_NextPart_000_0077_01C34C8B.2B90C960 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ..I just sent this out to the win32 distribution list but no one has...
0
by: Mark Adams | last post by:
I really need some help with this. MySQL will not start on boot despite everything I've done to make sure that it is set to do so. When I start it as root from a terminal with...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
1
by: DarkGiank | last post by:
Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.