Closed xiaoliuzi closed 9 years ago
$sudo apt-get install mysql-server-5.6
弹出如下窗口,请输入密码,比如nfs123
回车继续,再次输入密码。
sudo apt-get install mysql-client-5.6
报错如下
dpkg:error processing /var/cache/apt/archives/mysql-client5.6....0ubuntu_all_amd64.deb
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
将其中的python....deb替换为,报错中的mysql-client....deb
$sudo apt-get install libmysqlclient18-dev
如果不安装此库,当编写C语言程序的时候,会报出如下错误
fatal error: mysql.h: No such file or directory
compilation terminated.
当安装完毕以后,输入如下命令运行数据库
$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;
在~/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;
}
$gcc -I /usr/include/mysql test_connect.c -L /usr/lib/mysql -lmysqlclient -o test_connect
可以看到当前目录生成可执行程序: test_connect 然后运行
$./test_connect
得如下图,表明运行成功
#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;
}
再调用上面的SELECT查询语句查询。
mysql>SELECT childno,fname,age FROM children;
安装MySQL、并实现基本的C语言连接数据库,并实现增删改查其中一项功能,这里实现的是插入记录