建立表user (id,name,password,role)
msg(title,content,submitTime)
在PowerDesigner中设计好表与表之间的关系得到 建立数据库的SQL语句如下:
/*==============================================================*/
/* DBMS name: MySQL 4.0 */
/* Created on: 2010-6-29 11:51:14 */
/*==============================================================*/
drop index Relationship_1_FK on Msg;
drop table if exists Msg;
drop table if exists User;
/*==============================================================*/
/* Table: Msg */
/*==============================================================*/
create table Msg
(
MsgID decimal not null AUTO_INCREMENT,
UserID decimal not null,
Title varchar(50) not null,
Content text not null,
SubmitTime datetime not null,
primary key (MsgID)
)
type = InnoDB;
/*==============================================================*/
/* Index: Relationship_1_FK */
/*==============================================================*/
create index Relationship_1_FK on Msg
(
UserID
);
/*==============================================================*/
/* Table: User */
/*==============================================================*/
create table User
(
UserID decimal not null AUTO_INCREMENT,
Name varchar(50) not null,
Password varchar(15) not null,
Role varchar(50) not null,
primary key (UserID)
)
type = InnoDB;
alter table Msg add constraint FK_UserHaveMsg foreign key (UserID)
references User (UserID) on delete restrict on update restrict;
添加唯一约束的语句:
alter table User add constraint uq_name UNIQUE(Name);
查询结果的处理
MYSQL_RES *result;
unsigned int num_fields;
unsigned int num_rows;
if (mysql_query(&mysql,query_string))
{
// error
}
else // query succeeded, process any data returned by it
{
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);
// retrieve rows, then call mysql_free_result(result)
}
else // mysql_store_result() returned nothing; should it have?
{
if (mysql_errno(&mysql))
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
else if (mysql_field_count(&mysql) == 0)
{
// query does not return data
// (it was not a SELECT)
num_rows = mysql_affected_rows(&mysql);
}
}
}
编译源程序的语句