Click to See Complete Forum and Search --> : how to create mysql database at run time...?


sreekanthks
January 18th, 2006, 08:58 AM
hi



i want to create a new database if it is not existing..and tables and Queries



thanks in advance

exterminator
January 19th, 2006, 04:14 AM
You can create the database by setting up a connection and then running the DDL (CREATE TABLE) query on that connection. It's a little different in the sense - when you specify the connection string - since you don't have the db at the moment so here you will not give that parameter. Look for connection strings here - www.connectionstrings.com.

It's simple. For a tutorial/article - refer to this - create database programmatically using ADO.net (http://www.c-sharpcorner.com/Code/2002/Mar/CreatingDBProgrammaticallyMCB.asp)

But why do you want to do this? Is this a client-server application with a centralized database? If yes, then you would better prepare an sql script with the database creation code and execute that on the db server. If this is a desktop application, fine!

If its Java that you are dealing with - have a look at HSQLDB (http://hsqldb.org/). Hope this helps. Regards.

ovidiucucu
January 21st, 2006, 08:14 AM
In MySQL, creating databases/tables only if do not exist can be easily accomplish by using IF NOT EXISTS clause in CREATE DATABASE/CREATE TABLE statements. For example

CREATE DATABASE IF NOT EXISTS MyDatabase;
USE MyDatabase;
CREATE TABLE IF NOT EXISTS cmtcfg(
CmtIdn int(11) NOT NULL auto_increment,
CmtTypIdn char(1) NOT NULL default '',
CmtHstIdn char(3) default NULL,
CmtTxt varchar(100) default NULL,
PRIMARY KEY (CmtIdn),
UNIQUE KEY CmtTxt (CmtTxt)
) TYPE=MyISAM;

What you have to do is to make an SQL script file containing the statements like above and run it using SOURCE command of mysql.exe or using mysqldump.exe utility (you can find them in the "bin" folder of mysql).
From your application you can launch mysql.exe or mysqldump.exe, for example with CreateProcess Windows SDK function.
For more info about mysql.exe and mysqldump.exe as well as for CREATE DATABASE and CREATE TABLE statements, take a look in MySQL Reference Manual (http://dev.mysql.com/doc/).