Hi Guys,
I am working on creating an install package for an application (uses Apache, MySQL, PHP). I have the table structures in a install.sql file that I need to run to create the base tables for the application.
The installer is a VB exe.
Can any one tell me how to include the install.sql file to create the base tables? As well, I need to run a sql to create new user accounts and drop the basic default accounts that come with the MySQL install.
Should I be using PHP to do this? I could work this out, but I know I can run the SQL files. I just need a pointer or two on how to do this.
TIA
programmatically run a sql file
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Well if your using php then just connect to the database, open the file, read the queries, and query the database. I seperate each query with something that i can identify thats not in any mysql query so i can split the file into seperate queries:
Then the code:
Easy as that.
Code: Select all
##--------------------------------------------
##TEMPLATES
##Holds the available templates for use on the site
##--------------------------------------------
CREATE TABLE templates (
id int(4) NOT NULL auto_increment unique,
icicle_id varchar(10) NOT NULL,
name varchar(100) NOT NULL,
path varchar(255) NOT NULL,
active tinyint(1) NOT NULL,
PRIMARY KEY (id)
);
*-----*
##--------------------------------------------
##TEMPLATE TAGS
##Holds the available tags that can be used in the template
##--------------------------------------------
CREATE TABLE template_tags (
tag varchar(50) NOT NULL unique,
icicle_id varchar(10) NOT NULL,
content varchar(100) NOT NULL,
static tinyint(1) NOT NULL default '0',
active tinyint(1) NOT NULL default '1',
PRIMARY KEY (tag)
);Code: Select all
$fp = fopen('install.sql', 'r');
while(!feof($fp)){
$sql .= fread($fp, 1024);
}
fclose($fp);
$sql = explode("*-----*", $sql);
foreach($sql as $q){
mysql_query($q);
}http://dev.mysql.com/doc/mysql/en/LOAD_DATA.html, or providing you know the host/username/password of the database, you could just dump the file to the mysql client, which would do what you want.