28. november 2003 - 14:55Der er
54 kommentarer og 1 løsning
c og mysql
Hej E
jeg har fundet ------------------------------------------------- #include <mysql.h> /* Headers for MySQL usage */ #include <stdio.h> #include <stdlib.h> #include <string.h>
static MYSQL demo_db;
int main(int argc, char **argv){ int insert_id; char *encdata, *query; int datasize; MYSQL_RES *res; /* To be used to fetch information into */ MYSQL_ROW row;
if(argc<2){ printf("Please supply a string for insertion into the database\n"); exit(0); }
if(!mysql_connect(&demo_db, "192.168.1.15", "bob", "")){ /* Make connection with mysql_connect(MYSQL db, char *host, char *username, char *password */ printf(mysql_error(&demo_db)); exit(1); }
if(mysql_select_db(&demo_db, "demodb")){ /* Select the database we want to use */ printf(mysql_error(&demo_db)); exit(1); }
encdata=malloc(strlen(argv[1])+1); /* Create a buffer to write our slash-encoded data int; it must be at least twice the size of the input string plus one byte for the null terminator */ datasize=mysql_real_escape_string(&demo_db, encdata, argv[1], strlen(argv[1])); /* Escape any MySQL-unsafe characters */ query=malloc(datasize+255); /*Make sure we have enough space for the query */ sprintf(query, "INSERT INTO demotable(demodata) VALUES('%s')", encdata); /* Build query */ if(mysql_real_query(&demo_db, query, strlen(query)+255)){ /* Make query */ printf(mysql_error(&demo_db)); exit(1); } free(query);
insert_id=mysql_insert_id(&demo_db); /* Find what id that data was given */
query=malloc(255); sprintf(query, "SELECT demodata FROM demotable WHERE id='%d'", insert_id); if(mysql_real_query(&demo_db, query, 255)){ /* Make query */ printf(mysql_error(&demo_db)); exit(1); }
res=mysql_store_result(&demo_db); /* Download result from server */ row=mysql_fetch_row(res); /* Get a row from the results */ printf("You inserted \"%s\".\n", row[0]); mysql_free_result(res); /* Release memory used to store results. */ mysql_close(&demo_db);
[root@dell c]# gcc -Wall -c my.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient gcc: -lmysqlclient: linker input file unused because linking not done
[root@dell c]# gcc -o my my.o -lm my.o(.text+0x45): In function `main': : undefined reference to `mysql_connect' my.o(.text+0x59): In function `main': : undefined reference to `mysql_error' my.o(.text+0x81): In function `main': : undefined reference to `mysql_select_db' my.o(.text+0x95): In function `main': : undefined reference to `mysql_error' my.o(.text+0xf4): In function `main': : undefined reference to `mysql_real_escape_string' my.o(.text+0x14b): In function `main': : undefined reference to `mysql_real_query' my.o(.text+0x15f): In function `main': : undefined reference to `mysql_error' my.o(.text+0x190): In function `main': : undefined reference to `mysql_insert_id' my.o(.text+0x1d4): In function `main': : undefined reference to `mysql_real_query' my.o(.text+0x1e8): In function `main': : undefined reference to `mysql_error' my.o(.text+0x20b): In function `main': : undefined reference to `mysql_store_result' my.o(.text+0x21c): In function `main': : undefined reference to `mysql_fetch_row' my.o(.text+0x242): In function `main': : undefined reference to `mysql_free_result' my.o(.text+0x252): In function `main': : undefined reference to `mysql_close' collect2: ld returned 1 exit status
* Created 14-11-2003 Modified 29-11-2003 Project Model Company Author Version Database mySQL 4.0 */
Create table Kunde ( kunde_id Int UNSIGNED NOT NULL AUTO_INCREMENT, post_nr Int UNSIGNED NOT NULL , firmanavn Varchar(30) NOT NULL , vejnavn Varchar(50) NOT NULL , telefon Int UNSIGNED NOT NULL , mobil Int UNSIGNED , fax Int UNSIGNED , email Varchar(50) NOT NULL , password Varchar(32) NOT NULL , Primary Key (kunde_id)) TYPE = MyISAM;
Create table postnummer ( post_nr Int UNSIGNED NOT NULL , bynavn Varchar(30) NOT NULL , Primary Key (post_nr)) TYPE = MyISAM;
Create table producent ( producent_id Int UNSIGNED NOT NULL AUTO_INCREMENT, navn Char(30) NOT NULL , Primary Key (producent_id)) TYPE = MyISAM;
Create table produkter ( produkt_id Int UNSIGNED NOT NULL AUTO_INCREMENT, navn Varchar(50) NOT NULL , producent_id Int UNSIGNED NOT NULL , Primary Key (produkt_id)) TYPE = MyISAM;
Create table ord ( ord_id Int UNSIGNED NOT NULL AUTO_INCREMENT, ord Varchar(30) NOT NULL , Primary Key (ord_id)) TYPE = MyISAM;
Create table produkter_ord ( ord_id Int UNSIGNED NOT NULL , produkt_id Int UNSIGNED NOT NULL ) TYPE = MyISAM;
Create table kunde_produkter ( kunde_id Int UNSIGNED NOT NULL , produkt_id Int UNSIGNED NOT NULL ) TYPE = MyISAM;
Create table email ( email_id Int UNSIGNED NOT NULL AUTO_INCREMENT, produkt_id Int UNSIGNED NOT NULL , e_from Varchar(100) NOT NULL , e_subject Varchar(100) NOT NULL , e_date Varchar(100) NOT NULL , Primary Key (email_id)) TYPE = MyISAM;
Alter table kunde_produkter add Index IX_kunde_knudeprodukter (kunde_id); Alter table kunde_produkter add Foreign Key (kunde_id) references Kunde (kunde_id); Alter table Kunde add Index IX_kunde_postnummer (post_nr); Alter table Kunde add Foreign Key (post_nr) references postnummer (post_nr); Alter table produkter add Index IX_produkter_producent (producent_id); Alter table produkter add Foreign Key (producent_id) references producent (producent_id); Alter table produkter_ord add Index IX_produkter_hit (produkt_id); Alter table produkter_ord add Foreign Key (produkt_id) references produkter (produkt_id); Alter table kunde_produkter add Index IX_produkter_kundeprodukter (produkt_id); Alter table kunde_produkter add Foreign Key (produkt_id) references produkter (produkt_id); Alter table email add Index IX_produkt_email (produkt_id); Alter table email add Foreign Key (produkt_id) references produkter (produkt_id); Alter table produkter_ord add Index IX_ord_hit (ord_id); Alter table produkter_ord add Foreign Key (ord_id) references ord (ord_id);
int main() { MYSQL *handle; int id; char sqlcmd[1000]; char from[100] = "fro_test"; char subject[100] = "sub_test"; char date[100] = "dat_test"; int ii = 1;
/*************************************************************\ * Auhter: * * Projekt: Incident * * Date: 08-12-03 * * * * Execute: ./%programname% < %textfile% * * * * Description: takes a textfile as input. * * scans it for keywords (FROM, SUBJECT, DATE). * * When found take the rest of the line as VAR. * * puts VAR in a MySQL database. * * * \*************************************************************/
/* Program START */ int main () { /* Program Variables */ char inlin[linsize]; char from[] = "From: ", subject[] = "Subject: ", date[] = "Date: "; char *p_from, *p_subject, *p_date;
/* MySQL Variables */ MYSQL *handle; char sqlcmd[1000]; char d_from[100] = "fro_test"; char d_subject[100] = "sub_test"; char d_date[100] = "dat_test";
handle= mysql_init(NULL);
if(isatty(fileno(stdin))){ printf("You must \"PIPE\" a file to the program. Contact admin for instructions.\n"); return 1; }
/* Read %textfile% line by line*/ while (!feof(stdin)) { fgets(inlin, linsize, stdin); /* Copy the rest of the line to variabel if found */ p_from = strstr(inlin,from); p_subject = strstr(inlin,subject); p_date = strstr(inlin,date); /* IF "From: " found */ if(p_from != NULL) { p_from += strlen(from); } /* IF "Subject: " found */ if(p_subject != NULL) { p_subject += strlen(subject); } /* IF "Date: " found */ if(p_date != NULL) { p_date += strlen(date); } }
/* Init connection to MySQL Database */ if(handle == NULL) { printf("MySQL error1: %s", mysql_error(handle)); exit(1); }
/* Make connection to MySQL Database */ if(!mysql_real_connect(handle, "localhost", "**", "**", "**", 0, NULL, 0)) { printf("MySQL error2: %s", mysql_error(handle)); exit(1); }
/* Insert data in email.incident in Mysql Database */ sprintf(sqlcmd,"insert into email(e_from, e_subject, e_date) values('%s', '%s', '%s')",d_from,d_subject,d_date); if(!mysql_query(handle, sqlcmd)) { printf(sqlcmd); printf("MySQL error3: %s", mysql_error(handle)); }
/* Close connection to MySQL Database */ mysql_close(handle);
hvis jeg nu ønsker at udtrække nogle værdier(ord) fra en tabel(ord) mysql_query(handle, "SELECT * FROM ord"); result = mysql_store_result(handle); nfields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { l = (int *)mysql_fetch_lengths(result); for (i=0; i<nfields; i++) { printf(" %.*s", l[i], row[i] ? row[i] : "NULL"); } printf("\n"); }
men istedet for at printe det hele ønsker jeg at gemme row[2] over i en var så jeg kan teste den imod noget af min anden kode.
i mit prog indlæser jeg en linie fra en tekst fil. så skal jeg teste om mit ord fra db er i linien. så tester jeg på næste ord fra db........................... ...... ...... så indlæser jeg næste linie af tekstfilen nu skal jeg så teste på de samme værdier fra db igen.
Når jeg nu søger en tekstfil igennem og indsætter (From, Subject, Date) i en tabel, hvor jeg har et id felt der er auto_increment. Så når jeg finder nogle ord(fra en anden tabel) der skal høre sammen med den første. hvordan får jeg så det %nr% id feldtet er kommet til.
fx.
Tabel email id from subject date 23 me@me.me dette er en test 03-12-12
(og når der ikke er nogen %d eller %s er der jo slet ikke grund til sprintf)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.