<< 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

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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