I would like to insert into my database the character stored inside the variable t. How do I do that?
This is my code. -
#include <my_global.h>
-
#include <mysql.h>
-
#include<time.h>
-
#include<stdio.h>
-
int main(int argc, char **argv)
-
{
-
char t='alpha';
-
MYSQL *conn;
-
-
conn = mysql_init(NULL);
-
mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0);
-
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))");
-
mysql_query(conn, "INSERT INTO csetech_test(name,password,time, sensor) VALUES(("%s",1001,now(),'0')",t);
-
-
mysql_close(conn);
-
printf("%s",t);
-
_getch();
-
Thanks
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.
Thank you for your reply Banfa.
However I am a bit confused, what do you mean by query string?
Can you please explain?
Thanks
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
@poreko
Guys I am still stuck by this. Any help will be greatly appreciated.
Thanks
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?
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. -
#include <my_global.h>
-
#include <mysql.h>
-
#include<string.h>
-
#include<conio.h>
-
#include<time.h>
-
#include<stdio.h>
-
int main(int argc, char **argv)
-
{
-
char t[60]="alpha";
-
MYSQL *conn;
-
char query[2000];
-
-
conn = mysql_init(NULL);
-
mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0);
-
//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))");
-
-
query[2000]= ("insert into csetech_test2(name) values ('%s')", t));
-
mysql_query(conn, query);
-
-
mysql_close(conn);
-
//printf("%s\n",t);
-
_getch();
-
-
}
-
I am getting the following error: warning C4047: '=' : 'char' differs in levels of indirection from 'char *'
Thanks
Guy,
I have solved the problem thanks a lot. Here is the solution: -
#include <my_global.h>
-
#include <mysql.h>
-
#include<string.h>
-
#include<conio.h>
-
#include<time.h>
-
#include<stdio.h>
-
int main(int argc, char **argv)
-
{
-
char t[60]="alpha";
-
MYSQL *conn;
-
char query[2000];
-
-
-
conn = mysql_init(NULL);
-
mysql_real_connect(conn, "localhost", "root", "", "testdb2", 0, NULL, 0);
-
//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))");
-
-
sprintf(query,"insert into csetech_test2(name,password,time,sensor) values ('%s',1234,NOW(),2)", t);
-
mysql_query(conn, query);
-
-
mysql_close(conn);
-
_getch();
-
-
}
-
[SIZE=2]
[/SIZE]
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
...
|
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...
|
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...
|
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...
|
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
--------------------------------------------------------------------------------
...
|
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'
...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |