ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÔÚLinuxÏÂͨ¹ýCÓïÑÔ²Ù×÷MySQLÊý¾Ý¿â---ʵÀý1

·¢²¼Ê±¼ä:2010-01-27 22:50:42À´Ô´:ºìÁª×÷Õß:longyun
ÔÚLinuxÏÂͨ¹ýCÓïÑÔ²Ù×÷MySQLÊý¾Ý¿â
2010Äê1ÔÂ27ÈÕ Íí 22£º10

×÷Õߣºlongyun(mtd527@gmail.com)

Ðø£ºÐ¡µÜ×î½üÏëѧϰÊý¾Ý¿â£¬²¢Ï뿪·¢Ò»¸ö¼òµ¥µÄÊý¾Ý¿â¹ÜÀíϵͳ£¬ÐèÒªÓõ½ GTK+2.0 , CÓïÑÔ £¬ Mysql £»ËùÒÔСµÜ²é¿´Á˺ܶàlinuxÏÂC²Ù×÷mysqlµÄ×ÊÁÏ£¬·¢ÏÖдµÄ¶¼²»ÔõôÏêϸ£¬ËùÒÔСµÜдÏÂÕâÆª¶ÌÎĹ©´ó¼ÒÒ»Æð½»Á÷ѧϰ£¡

ʵÑé»·¾³£º
Fedora 11

×é¼þ£º
libdbi-dbd-mysql-0.8.3-4.fc11.i586
mysql-5.1.42-2.fc11.i586
mysql-libs-5.1.42-2.fc11.i586
mysql-devel-5.1.42-2.fc11.i586
mysql-connector-java-5.1.8-2.fc11.i586
qt-mysql-4.5.3-9.fc11.i586
php-mysql-5.2.11-2.fc11.i586
mysql-connector-odbc-5.1.5r1144-4.fc11.i586
mysql-server-5.1.42-2.fc11.i586



µÚÒ»²½£º

Æô¶¯Ò»¸ömysql¿Í»§¶Ë
[mtd@mutiandong mysql]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.1.42 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

´´½¨Ò»¸öÊý¾Ý¿â
mysql> create database cusemysql;
Query OK, 1 row affected (0.00 sec)

²¢Ê¹ÓÃÕâ¸öÊý¾Ý¿â
mysql> use cusemysql;
Database changed
ÔÚ¸øÊý¾Ý¿âÄÚ´´½¨Ò»¸ö±í
mysql> create table children(childno int not null unique,fname varchar(20),age int);
Query OK, 0 rows affected (0.00 sec)

ÔڸñíÄÚ²åÈëÒ»ÏîÊý¾Ý
mysql> insert into children values(5,¡®jhon¡¯,10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
+---------+-------+------+
1 row in set (0.00 sec)


mysql>


µÚ¶þ²¿£º

±àд insert.c

///////////////////////////////////
/* insert.c */
#include
#include
#include
/*×¢ÒâŶ£¬ÉÏÃæÐ´µÄÊÇmysql.hµÄ¾ø¶ÔµØÖ·£¬Ò»°ãÔÚ/usr/include/mysqlÏ£¬×Ðϸ¿´¿´ÄãµÄÔÚÄÄÀÕâÖÖ·½Ê½ #include "mysql/h" ¿ÉÄÜÒ²¿ÉÒÔ*/

int main(int argc, char *argv[])
{
MYSQL my_connection;

int res;

mysql_init(&my_connection);

/*mysql_real_connect(&mysql,Ö÷»úÃû,Óû§Ãû,ÃÜÂë,Êý¾Ý¿âÃû,0,NULL,0) == NULL)*/
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into children values(11,'Anny',5)");

if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*ÀïÍ·µÄº¯Êý·µ»ØÊܱíÖÐÓ°ÏìµÄÐÐÊý*/
}
else
{
//·Ö±ð´òÓ¡³ö´íÎó´úÂë¼°ÏêϸÐÅÏ¢
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}

else
{
fprintf(stderr, "Connection failed\n");

if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
/////////////////////////////////////////////
´úÂëдÍêÁË£¬Òª±àÒëŶ
[mtd@mutiandong mysql]# gcc -o insert insert.c
/tmp/ccyHfsX2.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/ccyHfsX2.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/ccyHfsX2.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/ccyHfsX2.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/ccyHfsX2.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/ccyHfsX2.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#
Í·ÎļþºÍ¿âÎļþλÖÃûÓÐÖ¸¶¨
[mtd@mutiandong mysql]# gcc -o insert insert.c -I/usr/include/mysql/ -L/usr/lib/mysql/
/tmp/cc4gdmlp.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/cc4gdmlp.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/cc4gdmlp.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/cc4gdmlp.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/cc4gdmlp.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/cc4gdmlp.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#

gcc»¹ÊÇÕÒ²»µ½Í·Îļþ£¬ÏÂÃæÎÒÃÇ¿ÉÒÔÕâÑù,Ö¸¶¨gccרÃÅÕÒ mysqlclient Ö®ÀàµÄ¿â

[root@localhost testmysql]# gcc -o insert insert.c -lmysqlclient -I/usr/include/mysql/ -L/usrlib/mysql

ÎÒÓà -lmysqlclient Ñ¡Ïî¾Í¿ÉÒÔÁË¡£

ok,ÏÖÔÚÎÒÃÇÖ´Ðп´¿´
[mtd@mutiandong mysql]# ./insert
Connection Success
Inserted 1 rows


ºÇºÇ£¬ÕæµÄ³É¹¦ÁË£¡
²»Ðŵ½mysqlÏ¿´¿´±íchildrenÖÐÊÇ·ñ¶àÁ˸ղŲåÈëµÄÄÇÒ»ÐÐÊý¾Ý

mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
| 11 | Anny | 5 |
+---------+-------+------+
2 rows in set (0.00 sec)

ѧϰlinuxµÄÐÖµÜÃǶ¼²»ÈÝÒ×£¬»¶Ó­´ó¼Ò½»Á÷,Skype:mtd527
(ÒòΪlinuxϵÄQQºÜ²»Îȶ¨£¬¹¦ÄÜÌ«ÉÙ£¬¹ÊÁôÏÂSkype ÕʺÅ)
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. ftd505 ÓÚ 2011-04-18 15:52:21·¢±í:

    {:2_99:}

  2. oniprbl ÓÚ 2010-01-28 08:42:23·¢±í:

    Ïòһ¥ѧϰ