怎么把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能顺利执行!
我用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能顺利执行!
相关文章推荐
- php 用命令行导出和导入MySQL数据库
- PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
- MYSQL数据库导入sql的时候报错:[Err] 1046 - No database selected 今天把之前其它电脑导出的sql,导入本机的MySQL数据库时候出现了报错。原因是没有先新建
- Windows 7和SUSE 11系统使用命令行导入导出mysql数据库sql脚本
- 如何导入导出MySQL数据库*.sql文件操作
- mysql数据库sql文件导入导出命令
- PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
- 如何导入导出MySQL数据库----*.sql文件操作
- 6.(Mysql数据管理相关)连接MYSQL,修改密码,增加新用户,数据库相关命令,表操作相关命令,数据相关命令,数据库sql导入和导出,备份数据库,查看不到mysql数据库的解决办法
- M| SQL 导入导出的时候数据库表的主键和自动编号丢失 怎么办
- 如何导入导出MySQL数据库----*.sql文件操作
- Linux命令导出、导入MySQL数据库的.sql文件
- php mysql数据库导出sql文件
- Mysql数据库导入导出SQL文件的基本常用命令
- PowerDesigner对MySql数据库的Excel导入导出
- 远程linux服务器mysql数据库导入和导出.sql文件
- powerdesigner,建立好模型后,怎么导出sql脚本。
- 详解Windows和Linux下从数据库导出表结构,以及Linux下如何导入.sql文件到MySQL数据库
- Powerdesigner中导入sql脚本,以及导出当前pdm工程为sql脚本
- Mysql数据库导入导出SQL文件的基本常用命令