您的位置:首页 > 编程语言 > PHP开发

怎么把powerdesigner导出的sql用PHP导入MysQL数据库

2010-04-26 13:31 791 查看
我们知道用powerdesigner导出的sql文件后缀为'.sql';用phpmyadmin很容易导入MysQL数据库,但是用PHP怎么导入数据库呢?

我用powerdesigner设计一个数据库后导出sql文件(一个投票系统)为'vote.sql';

文件内容为(一些sql语句和注释):

/*==============================================================*/

/* DBMS name: MySQL 5.0 */

/* Created on: 2010-4-22 17:11:50 */

/*==============================================================*/

drop table if exists account_charge;

drop table if exists account_cost;

drop table if exists account_info;

drop table if exists charge_way;

drop table if exists user_askanswer;

drop table if exists user_info;

drop table if exists vote_fake;

drop table if exists vote_info;

drop table if exists vote_item;

drop table if exists vote_title;

drop table if exists vote_type;

/*==============================================================*/

/* Table: account_charge */

/*==============================================================*/

create table account_charge

(

chargeid int not null,

chargewayid int not null,

accountid int not null,

chargetotal numeric(8,2) not null,

chargetime datetime,

chargeway tinyint,

chargeip char(15),

primary key (chargeid)

);

/*==============================================================*/

/* Table: account_cost */

/*==============================================================*/

create table account_cost

(

costid int not null,

accountid int not null,

costtime datetime,

costtotal numeric(8,2) not null,

costip char(15),

primary key (costid)

);

/*==============================================================*/

/* Table: account_info */

/*==============================================================*/

create table account_info

(

accountid int not null,

userid int not null,

accountbalance numeric(8,2) not null,

accounttotal numeric(8,2) not null,

primary key (accountid)

);

/*==============================================================*/

/* Table: charge_way */

/*==============================================================*/

create table charge_way

(

chargewayid int not null,

chargename varchar(20) not null,

chargewaydes varchar(200),

primary key (chargewayid)

);

/*==============================================================*/

/* Table: user_askanswer */

/*==============================================================*/

create table user_askanswer

(

askanswerid int not null,

userid int not null,

ask varchar(20) not null,

answer varchar(20) not null,

primary key (askanswerid)

);

/*==============================================================*/

/* Table: user_info */

/*==============================================================*/

create table user_info

(

userid int not null,

accountid int,

username varchar(20) not null,

password varchar(20) not null,

sex char(1),

phone varchar(12) not null,

email varchar(50) not null,

regtime datetime,

lastlogtime datetime,

lastlogip char(15),

status char(1),

primary key (userid)

);

/*==============================================================*/

/* Table: vote_fake */

/*==============================================================*/

create table vote_fake

(

votefakeid bigint not null,

voteitemid bigint not null,

votefaketotal int not null,

votefaketime datetime,

primary key (votefakeid)

);

/*==============================================================*/

/* Table: vote_info */

/*==============================================================*/

create table vote_info

(

voteinfoid bigint not null,

userid int not null,

votetypeid tinyint not null,

voteitemid bigint not null,

voteinfotime datetime,

voteinfoip char(15),

voteinfototal int default 1,

primary key (voteinfoid)

);

/*==============================================================*/

/* Table: vote_item */

/*==============================================================*/

create table vote_item

(

voteitemid bigint not null,

votetitleid int not null,

voteitemname varchar(50) not null,

voteitemtotal bigint,

primary key (voteitemid)

);

/*==============================================================*/

/* Table: vote_title */

/*==============================================================*/

create table vote_title

(

votetitleid int not null,

votename varchar(50) not null,

votestarttime datetime,

voteendtime datetime,

votetime datetime,

votetitletype char(1),

primary key (votetitleid)

);

/*==============================================================*/

/* Table: vote_type */

/*==============================================================*/

create table vote_type

(

votetypeid tinyint not null,

votetypename varchar(50) not null,

primary key (votetypeid)

);

alter table account_charge add constraint FK_Relationship_3 foreign key (accountid)

references account_info (accountid) on delete restrict on update restrict;

alter table account_charge add constraint FK_Relationship_8 foreign key (chargewayid)

references charge_way (chargewayid) on delete restrict on update restrict;

alter table account_cost add constraint FK_Relationship_4 foreign key (accountid)

references account_info (accountid) on delete restrict on update restrict;

alter table account_info add constraint FK_Relationship_2 foreign key (userid)

references user_info (userid) on delete restrict on update restrict;

alter table user_askanswer add constraint FK_Relationship_5 foreign key (userid)

references user_info (userid) on delete restrict on update restrict;

alter table user_info add constraint FK_Relationship_1 foreign key (accountid)

references account_info (accountid) on delete restrict on update restrict;

alter table vote_fake add constraint FK_Relationship_7 foreign key (voteitemid)

references vote_item (voteitemid) on delete restrict on update restrict;

alter table vote_info add constraint FK_Relationship_10 foreign key (userid)

references user_info (userid) on delete restrict on update restrict;

alter table vote_info add constraint FK_Relationship_11 foreign key (votetypeid)

references vote_type (votetypeid) on delete restrict on update restrict;

alter table vote_info add constraint FK_Relationship_9 foreign key (voteitemid)

references vote_item (voteitemid) on delete restrict on update restrict;

alter table vote_item add constraint FK_Relationship_6 foreign key (votetitleid)

references vote_title (votetitleid) on delete restrict on update restrict;

此时我把整个语句当成一个sql语句用PHP的mysql_query来执行,结果错误。经过查阅一些资料得到
mysql_query每次只能执行一条语句!

于是用下面的循环执行即可:

$file='vote.sql';

$fh=fopen($file,'rt');

$data=fread($fh,filesize($file));//读取文件中的全部内容

$sqlarray=explode(';',$data);//每条sql语句由';'隔开

foreach($sqlarray as $sqlvalue)

{

mysql_query($sqlvalue);

}

此时PHP能顺利执行!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐