xiaoliuzi / user_demo

1 stars 0 forks source link

C语言连接MySQL数据库,实现记录插入功能 #1

Closed xiaoliuzi closed 9 years ago

xiaoliuzi commented 9 years ago

安装MySQL、并实现基本的C语言连接数据库,并实现增删改查其中一项功能,这里实现的是插入记录

xiaoliuzi commented 9 years ago

1.在Ubuntu1404上安装MySQL数据库

$sudo apt-get install mysql-server-5.6

弹出如下窗口,请输入密码,比如nfs123 mysql-1

回车继续,再次输入密码。

mysql-2

2.安装mysql-client

sudo apt-get install mysql-client-5.6

报错如下

dpkg:error processing /var/cache/apt/archives/mysql-client5.6....0ubuntu_all_amd64.deb

参考链接: http://askubuntu.com/questions/148383/how-to-resolve-dpkg-error-processing-var-cache-apt-archives-python-apport-2-0

Solution:

You need to force overwriting of the files causing the errors:

$sudo dpkg -i --force-overwrite <filename>

In your case it's going to be:

sudo dpkg -i --force-overwrite /var/cache/apt/archives/python-problem-report_2.0.1-0ubuntu9_all.deb

Then run:

sudo apt-get -f install

Source: http://www.absolutelytech.com/2010/06/30/solved-error-dpkg-error-processing-filename-unpack-trying-to-overwrite/

将其中的python....deb替换为,报错中的mysql-client....deb

xiaoliuzi commented 9 years ago

3.安装libmysqlclient*-dev

$sudo apt-get install libmysqlclient18-dev

如果不安装此库,当编写C语言程序的时候,会报出如下错误

fatal error: mysql.h: No such file or directory
compilation terminated.
xiaoliuzi commented 9 years ago

4.创建数据库,及表

当安装完毕以后,输入如下命令运行数据库

$mysql -uroot -p

然后输入密码,本人为:nfs123

然后发现数据库可以启动,并且提示符变为如下格式:

mysql>

然后创建一个名字为foo的数据库:

mysql>CREATE DATABASE foo;

使用如下命令然后进入foo数据库中:

mysql>USE foo;

然后该创建表了,由于创建表的SQL语句较多,我们可以重新打开一个terminal,然后进入当前目录下,也就是~/test/user_demo下,使用vi创建名为create_children.sql的文件,并输入如下内容:

CREATE TABLE children(
    childno int(11) NOT NULL auto_increment,
    fname varchar(30),
    age int(11),
    PRIMARY KEY (childno)
    );

INSERT INTO children(childno,fname,age) VALUES(1,'Jenny',21);
INSERT INTO children(childno,fname,age) VALUES(2,'Andrew',17);
INSERT INTO children(childno,fname,age) VALUES(3,'Gavin',8);
INSERT INTO children(childno,fname,age) VALUES(4,'Duncan',6);
INSERT INTO children(childno,fname,age) VALUES(5,'Emma',4);
INSERT INTO children(childno,fname,age) VALUES(6,'Alex',15);
INSERT INTO children(childno,fname,age) VALUES(7,'Adrian',9);

保存退出vi 然后再在刚才的数据库目录下,执行如下命令:

mysql>\. create_children.sql

使用如下命令,查询,得到如下图,表明创建表成功。

mysql>SELECT childno,fname,age FROM children;

20151201193651

xiaoliuzi commented 9 years ago

5.数据库连接测试

在~/test/user_demo下编写如下代码,我的文件名字叫test_connect.c,生成的可执行程序叫test_connect:

#include <stdlib.h>
#include <stdio.h>

#include <mysql.h>

int main(int argc,char *argv[])
{
  MYSQL *conn_ptr;
  conn_ptr = mysql_init(NULL);

  if(!conn_ptr)
  {
    fprintf(stderr,"mysql_init failed\n");
    return EXIT_FAILURE;
  }

  conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","nfs123","foo",0,NULL,0);

  if(conn_ptr)
    printf("Connection success\n");
  else
    printf("Connection failed\n");

  mysql_close(conn_ptr);

  return EXIT_SUCCESS;
}
xiaoliuzi commented 9 years ago

6.编译并运行

$gcc -I /usr/include/mysql test_connect.c -L /usr/lib/mysql -lmysqlclient -o test_connect

可以看到当前目录生成可执行程序: test_connect 然后运行

$./test_connect

得如下图,表明运行成功

20151201194405

xiaoliuzi commented 9 years ago

在解决问题中,遇到了很多问题,参考了如下链接:

http://www.111cn.net/database/mysql/44142.htm

http://sharadchhetri.com/2014/05/07/install-mysql-server-5-6-ubuntu-14-04-lts-trusty-tahr/

http://blog.csdn.net/xiajun07061225/article/details/8505987

xiaoliuzi commented 9 years ago

7.向表中插入数据

#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>

int main()
{
    MYSQL my_connecyion;
    int res;

    mysql_init(&my_connecyion);

    if(mysql_real_connect(&my_connecyion,"localhost", "root", "nfs123", "foo", 0, NULL, 0))
    {
        printf("Connection success\n");

        // execute the SQL cmd
        res = mysql_query(&my_connecyion, "INSERT INTO children(fname, age) VALUES('Ann', 3)");

        if (!res)
            printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(&my_connecyion));
        else
            fprintf(stderr, "Insert error %d : %s \n", mysql_errno(&my_connecyion), mysql_error(&my_connecyion));

        mysql_close(&my_connecyion);
    }   

    else {
        fprintf(stderr, "Connection failed\n");
        if (mysql_errno(&my_connecyion));
            fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connecyion), mysql_error(&my_connecyion));
    }
    return EXIT_SUCCESS;

}
xiaoliuzi commented 9 years ago

再调用上面的SELECT查询语句查询。

mysql>SELECT childno,fname,age FROM children;