LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   i am tryin to write a script that will send data to MySQL (https://www.linuxquestions.org/questions/programming-9/i-am-tryin-to-write-a-script-that-will-send-data-to-mysql-48562/)

rhuser 03-06-2003 09:25 AM

i am tryin to write a script that will send data to MySQL
 
Hi

i am tryin to write a script that will send data to MySQL. I have images coming from a camera. i will be storing these images in a directory in my server (/home/me/images/ and each image will have a different name; the name will be the date of image. this is ok.), but i would like to send the information about the images to a database in MySQL. e.g. the name of the image, size of the image and date of image.

Any Ideas. Please any help would be aprreciated

mel

Shak 03-06-2003 10:20 AM

It would be pretty easy to do this in Perl, I suggest you have a look into the different modules and methods required:

http://www.cpan.org/ - A good place to look for modules

http://www.perl.com/ - Perl :D

Shak

crabboy 03-06-2003 10:41 AM

It's very easy in PHP.

Shak 03-06-2003 10:49 AM

PHP and Perl for this kind of thing are much of a muchness, I prefer Perl for system tasks and PHP for web based things :)

Shak

crabboy 03-06-2003 11:59 AM

I agree, PHP is best for web. Bourne shell then...

Shak 03-06-2003 12:04 PM

BASH is useful, but Perl is powerful. But always its the best tool for the job, you cant really say that any programming tool is better than any other except for how much you enjoy using them as they all do different things.

Shak

crabboy 03-06-2003 12:22 PM

What I'm really after is, I don't know Perl, so I find every way around using it. Perl is a very powerfull ( and cryptic ) and I have no doubt it could do the job.

Did we get off track?

Here is a small example of how to do something like this in C. The code below is missing pieces and will not compile as it, but you can use the syntax to build something that works for you.

Code:

#include <stdlib.h>
#include <string.h>
#include "parselog.h"
#include "logdb.h"

#include <mysql/mysql.h>

int init_db()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  MYSQL_ROW row;

  mysql_init(&mysql);

  if (!mysql_connect( &mysql, MYSQL_HOST, MYS
  {
      printf( "Failed to connect to DB, reason
              mysql_error( &mysql) );
      mysql_close( &mysql );
      return(0);
  }

  if (mysql_select_db( &mysql, MYSQL_DB ))
  {
      printf( "Failed to select table: Error:
      mysql_close(&mysql);
      return 0;
  }

}

int insert_entry( MYSQL *mysql, struct logEntr
{
  char query_str[500];
  MYSQL_RES *result = NULL;

  sprintf( query_str, "INSERT into entry "
                      "( month, day, time, hos
                      " mac, src, dst, len, to
                      " spt, dpt, urgp ) "
                      " values "
                      " %s, %d, %s, %s, %s, %s
                      " %d, %s, %s, %d, %d, %s
                      log -> szMonth,
                      log -> iDay,
                      log -> szTime,
                      log -> szHostname,
                      log -> szMessage,
                      log -> szIN,
                      log -> szOUT,
                      log -> szMAC,
                      log -> szDST,
                      log -> iLEN,
                      log -> szTOS,
                      log -> szPREC,
                      log -> iTTL,
                      log -> iID,
                      log -> szPROTO,
                      log -> iSPT,
                      log -> iDPT,
                      log -> iURGP );

  mysql_query( mysql, query_str );

  mysql_insert_id( mysql );
  result = mysql_use_result( mysql );

}

int close_db( MYSQL *mysql )
{
  mysql_close( mysql );
}

This would be main()
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parselog.h"
#include "logdb.h"

#include <mysql/mysql.h>

main()
{
  MYSQL mysql;
  MYSQL_RES *result = NULL;
  MYSQL_ROW row;

  init_db( &mysql );
  parseFile( &mysql);
  close_db( &mysql );

}

Of course the missing code parselog.cpp:parseFile (much too large to fit here) has the line:
Code:

        insert_entry( mysql, &log );

rhuser 03-06-2003 03:17 PM

thanks for the help

it is all good, iknow php is good, i have written a script that access data in PHP, but ireally need a script to run in PERL. i have a project i am working on, and the missing piece is this.

PLease help


All times are GMT -5. The time now is 02:58 PM.