Page 1 of 1

How to create database in programm?

Posted: Mon 17 Oct 2011 16:41
by imobile
Я перерыл весь интернет и не нашел, как с помощью MyDac создать базу данных. Подскажите форум использующих ваш продукт.

Когда я переношу свою программу написанную на builder xe на другой компьютер, то там базы нет. Как правильно проверить существует ли база, а в этой базе таблица? и как создать таблицу программно?

Posted: Mon 17 Oct 2011 20:44
by imobile
in computer where i run program stay mysql5.5
if i run on computer where database is created->ok. Also i can create database using

Code: Select all

MyConnection2->ExecSQL("CREATE DATABASE IF NOT EXISTS `u_db` CHARACTER SET utf8 COLLATE utf8_general_ci",NULL,0);
if database not create on computer, code not work-> erorr. If databasename set"" -> i have erorr 'erorr no database selected' Haw create database on clear mysql using MyDac. Create database special programm don't tell.
Sorry, for my bad Inglish.

Posted: Thu 20 Oct 2011 14:42
by Dimon
To solve the problem you should connect to the Mysql system database before creating database, create new database and after this connect to new database, like this:

Code: Select all

MyConnection2->Database = "mysql";
MyConnection2->Connect();
MyConnection2->ExecSQL("CREATE DATABASE ...");
MyConnection2->Database = "new_database";
MyConnection2->Connect();

Posted: Fri 21 Oct 2011 18:07
by imobile
Thanks.

if in next relise MySql without mysql database? users see erorr :(
I write:

Code: Select all

try{
   Form2->MyConnection2->Database="u_db";
   Form2->MyConnection2->Connected=true;
}
catch(...)
{
MyServerControl1->CreateDatabase("u_db",true,"UTF8","DEFAULT");
2 day - work OK.
I'am look other database, have problem too. Fine if you write new VCL component TCreateDatabase :) . On new computers when i stay my program, these be easy.

Code: Select all

CreateDatabase1->Create("mydatabase",innodb,utf8,default);
No connection requaid

Posted: Mon 24 Oct 2011 12:31
by AndreyZ
The point is that MySQL doesn't allow creating a new database without connecting to an existent database. The mysql database always exists (because it is a system database). Therefore you can use the code Dimon gave you.

Posted: Tue 08 Nov 2011 06:16
by kevinjaap
I totally agreed with AndreyZ, Even I am thinking about that only and wanna give same advice but it already gets covered!!!