<< Chapter < Page Chapter >> Page >
mysql c api

Mysql c api

Mysql

Przed uruchomianiem aplikacji natęży mieć bazę danych z tabela 'result' z polami: Id, Time, SP, PV, CV. Jeżeli sie takowej nie posiada należy ją stworzyć.

Oto kilka przydatnych do tego instrukcji:

połączenie z baza:

# mysql -u $user -p

tworzenie bazy danych:

>CREATE DATABASE control;

pracuj z tabela (wykonuj na niej dalsze operacje):

>USE control

tworzenie tabeli:

>CREATE TABLE result(Id int not null auto_increment primary key,Time TimeStamp(14) not null,SP double not null,PV double not null,CV double not null );

dodawanie danych (Id i Time uzupełniane są automatycznie):

>INSERT INTO result (SP,PV,CV) VALUES(1,2,3)

czyszczenie tabeli:

>DELETE FROM result;

czyszczenie tabeli (Id zacznie numerować od nowa):

>TRUNCATE result;

usuwanie tabeli z bazy:

>DROP result;

zamiana nazwy kolumny:

>ALTER TABLE `result` CHANGE `number` `Id` DECIMAL( 9, 0 ) DEFAULT '0' NOT NULL;

zmiana hasła:

>SET PASSWORD FOR $user=PASSWORD('password');

C api

Przed połączeniem z baza danych należy inicjalizować strukturę – identyfikator:

mysql = mysql_init((MYSQL*) 0);

Połączenie z baza danych:

mysql_real_connect( mysql, host, login, passwd, table, port,NULL,0);

Wykonanie zapytania:

mysql_real_query(mysql,query,(unsigned int) strlen(query));

Zamykanie połączenia:

mysql_close(mysql);

Jeżeli chcemy pobrać rezultaty zapytania np. po pytaniu “SHOW” lub “SELECT” należy użyć dodatkowych funkcji.

Używaj rezultatów ostatniego zapytania:

res=mysql_use_result(mysql);

Wyodrębnij nazwy kolumn:

fields = mysql_fetch_fields(res);

Pobierz wiersz spełniający ostatnie zapytanie (konwersja rezultatów w krotki):

row = mysql_fetch_row(res);

Wyodrębnij liczbe kolumn:

num_fields = mysql_num_fields(res);

Wyodrębnij szerokości kolumn:

lengths = mysql_fetch_lengths(res);

Specyfikacja zmiennych i funkcji mysql c api

MYSQL-struktura komunikacyjna:

typedef struct st_mysql {

NET net; /* Communication parameters */

gptr connector_fd; /* ConnectorFd for SSL */

char *host,*user,*passwd,*unix_socket,

*server_version,*host_info,*info,*db;

unsigned int port,client_flag,server_capabilities;

unsigned int protocol_version;

unsigned int field_count;

unsigned int server_status;

unsigned long thread_id; /* Id for connection in server */

my_ulonglong affected_rows;

my_ulonglong insert_id; /* id if insert on table with NEXTNR */

my_ulonglong extra_info; /* Used by mysqlshow */

unsigned long packet_length;

enum mysql_status status;

MYSQL_FIELD *fields;

MEM_ROOT field_alloc;

my_bool free_me; /* If free in mysql_close */

my_bool reconnect; /* set to 1 if automatic reconnect */

struct st_mysql_options options;

char scramble_buff[9];

struct charset_info_st *charset;

unsigned int server_language;

} MYSQL;

MYSQL_RES -struktura reprezentująca rezultaty zapytania

typedef struct st_mysql_res {

my_ulonglong row_count;

unsigned int field_count, current_field;

MYSQL_FIELD *fields;

MYSQL_DATA *data;

MYSQL_ROWS *data_cursor;

MEM_ROOT field_alloc;

MYSQL_ROW row; /* If unbuffered read */

MYSQL_ROW current_row; /* buffer to current row */

unsigned long *lengths; /* column lengths of current row */

MYSQL *handle; /* for unbuffered reads */

my_bool eof; /* Used my mysql_fetch_row */

} MYSQL_RES;

MYSQL_ROW-struktura reprezentująca danych w krotce.

typedef struct st_mysql_field {

char *name; /* Name of column */

char *table; /* Table of column if column was a field */

char *def; /* Default value (set by mysql_list_fields) */

enum enum_field_types type; /* Type of field. Se mysql_com.h for types */

unsigned int length; /* Width of column */

unsigned int max_length; /* Max width of selected set */

unsigned int flags; /* Div flags */

unsigned int decimals; /* Number of decimals in field */

} MYSQL_FIELD;

Plik nagłówkowy mysql znajduje sie w /usr/include/mysql :

#include<mysql/mysql.h>

Funkcje:

MYSQL *STDCALL mysql_init(MYSQL *mysql);

MYSQL *STDCALL mysql_real_connect(MYSQL *mysql, const char *host,

const char *user,

const char *passwd,

const char *db,

unsigned int port,

const char *unix_socket,

unsigned int clientflag);

void STDCALL mysql_close(MYSQL *sock);

int STDCALL mysql_real_query(MYSQL *mysql, const char *q,unsigned long length);

MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql);

MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);

unsigned long * STDCALL mysql_fetch_lengths(MYSQL_RES *result);

MYSQL_FIELD *STDCALL mysql_fetch_field(MYSQL_RES *result);

unsigned int STDCALL mysql_num_fields(MYSQL_RES *res);

Literatura

http://mysql.com/

http://www.linuxfocus.org/Polish/September2003/article304.shtml#304lfindex2

Uwagi

kompilacja:

# gcc -lm -lmysqlclient zbiornik_mysql.c regul.c mylib.c -o zbior

katalog zbr2

zawiera zrodal programu ktory wykonuje:

  • polczenie z baza
  • symulacje zbiornik-regulator
  • zapis przebiegu Temperatury do bazy
  • odczytanie i wypisanie na ekran zpisanych danych z bazy

katalog zbr3

zawiera zrodal programu ktory wykonuje:- polczenie z baza

  • symulacje zbiornik-regulator
  • zapis przebiegu Temperatury do bazy
  • odczytanie i wypisanie na ekran zpisanych danych z baz
  • mozliwosc samodzielnego wykonania zapyt. do bazy- skł MySQL

Uwaga odczyt rezultatow nie jest doskonaly, np wysypuje sie przy pytaniu SLECT... zadanym gdy tabela jest pusta

Questions & Answers

how do you translate this in Algebraic Expressions
linda Reply
why surface tension is zero at critical temperature
Shanjida
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
AMJAD
what is system testing
AMJAD
what is the application of nanotechnology?
Stotaw
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
Azam
anybody can imagine what will be happen after 100 years from now in nano tech world
Prasenjit
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
Azam
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
Prasenjit
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
Damian
silver nanoparticles could handle the job?
Damian
not now but maybe in future only AgNP maybe any other nanomaterials
Azam
Hello
Uday
I'm interested in Nanotube
Uday
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
Prasenjit
can nanotechnology change the direction of the face of the world
Prasenjit Reply
At high concentrations (>0.01 M), the relation between absorptivity coefficient and absorbance is no longer linear. This is due to the electrostatic interactions between the quantum dots in close proximity. If the concentration of the solution is high, another effect that is seen is the scattering of light from the large number of quantum dots. This assumption only works at low concentrations of the analyte. Presence of stray light.
Ali Reply
the Beer law works very well for dilute solutions but fails for very high concentrations. why?
bamidele Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Algorytmy i struktury komputerowych systemów sterowania. OpenStax CNX. May 22, 2007 Download for free at http://cnx.org/content/col10420/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Algorytmy i struktury komputerowych systemów sterowania' conversation and receive update notifications?

Ask