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

本文介绍如何使用PHP将PowerDesigner导出的SQL文件导入MySQL数据库。通过解析SQL文件内容并逐条执行SQL语句,实现数据库结构的创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们知道用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能顺利执行!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值