Help with mysql C API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poreko
    New Member
    • Nov 2006
    • 38

    Help with mysql C API

    I would like to insert into my database the character stored inside the variable t. How do I do that?
    This is my code.
    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • poreko
      New Member
      • Nov 2006
      • 38

      #3
      Thank you for your reply Banfa.
      However I am a bit confused, what do you mean by query string?
      Can you please explain?
      Thanks

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        The query string is the string containing the SQL command. Since all commands are queries it is commonly called the query string

        Comment

        • poreko
          New Member
          • Nov 2006
          • 38

          #5
          Originally posted by poreko
          I would like to insert into my database the character stored inside the variable t. How do I do that?
          This is my code.
          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
          Guys I am still stuck by this. Any help will be greatly appreciated.
          Thanks

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            How about if you explain what you want to happen. Use sentences, not code.

            What are the function prototypes for mysql_real_conn ect(), 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?

            Comment

            • poreko
              New Member
              • Nov 2006
              • 38

              #7
              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.
              Code:
              #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

              Comment

              • poreko
                New Member
                • Nov 2006
                • 38

                #8
                Guy,
                I have solved the problem thanks a lot. Here is the solution:
                Code:
                #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]

                Comment

                Working...