Sponsored Content
Top Forums Web Development met problem with do() in MySQL Post 302582334 by radoulov on Thursday 15th of December 2011 02:22:26 PM
Old 12-15-2011
Quote:
Originally Posted by yifangt
Thanks!
Do you mean the $SysDatabase and $dbh->do("use $DataBaseName") are needed in order to have the code run?
I am trying to catch the points as a beginner. It seems the way is
Code:
1. call $SysDatabase (mysql) first, connect to the system, 
2. then create the new  $DataBaseName, 
3. then use $DataBaseName and insert the $Table.

Not quite sure.
You need to connect to some database as the syntax of DBD::mysql forces you to do so (it seems so at least, after a quick look at the module's documentation).
The mysql system database will be always there, so that's a good start.
This could be problematic if you don't have the right privileges to use it,
but that doesn't seem a problem in your case.

Quote:
Yes, the code is running now, but the table is empty (Need to figure out the other part of the code and the format of the data too. let me put them aside at this moment).
There is another problem emerging as I can not run this script for a second time because the database and table were already created. They need be deleted before the second run. My colleague suggest not using this method by embedding the CREATE DATABASE or CREATE TABLE in the perl script. His point is these two commands only need once at the beginning to create database. But, my original point is to avoid switching back and forth between mysql console and the terminal console, especially when there are many columns in the table. Now compare the two strategies:
Code:
One: Create DATABASE/TABLES under >mysql console, then code the perl script to insert data;
Two: embed everything with perl script, but delete the database/tables before any second try when needed;

Which is the professional way? Thanks again! Yifangt
Well, as an Oracle DBA I agree with your colleague Smilie
Run the DDL once (well, unless you're trying to automate software installation or some sort of upgrade, of course).

Anyway, in this case mysql offers the following syntactic sugar that will resolve this issue:

Code:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database /*!32312 IF NOT EXISTS*/  test_db;
Query OK, 1 row affected (0.00 sec)


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| test_db            |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database /*!32312 IF NOT EXISTS*/  test_db;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> use test_db;
Database changed
mysql> create table /*!32312 IF NOT EXISTS*/ t(x int);
Query OK, 0 rows affected (0.06 sec)

mysql> desc t;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x     | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> create table /*!32312 IF NOT EXISTS*/ t(x int);
Query OK, 0 rows affected, 1 warning (0.00 sec)

You get just a warning, not an error:

Code:
mysql> create table  t(x int);
ERROR 1050 (42S01): Table 't' already exists


Last edited by radoulov; 12-15-2011 at 03:34 PM..
 

10 More Discussions You Might Find Interesting

1. Cybersecurity

Need someone to help, MySQL problem, no root access

Hi! I have a website, and it's been down for a long time now, because MySQL died (www.3dkingdom.org). I need someone who can get me the root password of the server (I will find someone to take care of the MySQL database, because there is about 20 sites there and none of them have MySQL databases... (6 Replies)
Discussion started by: _hp_
6 Replies

2. UNIX for Advanced & Expert Users

MySQL problem >> missing mysql.sock

MySQL on my server is down.... I figured out that the mysqld process isn't running. When I try to run it, it says it can't find mysql.sock Any suggestions? Here's what I can't do: can't be root don't have physical access (do stuff via SSH) reinstall MySQL (need to keep the current MySQL... (8 Replies)
Discussion started by: _hp_
8 Replies

3. Shell Programming and Scripting

Problem with PHP and MySQL

Okay, I'm new to this PHP and MySQL stuff, so help would be VERY much appreciated. :) On my iMac runnning Panther, it has MySQL and PHP installed. Yet when I view a PHP file from the iMac or another computer at my house, I get the source code. What's wrong? (11 Replies)
Discussion started by: Danny_10
11 Replies

4. Shell Programming and Scripting

Mysql problem

No help guys??? :( Hi guys, i've got a question here... my intention is to write a script that would access a mysql db and extract some info and paste it into a file... so the first move would be, to access the mysql interface right? hence... #! /usr/bin/ksh echo "Job Begin: " `date` ... (1 Reply)
Discussion started by: 12yearold
1 Replies

5. Linux

MySQL server connetction problem

Hello,thanks for your help. I can't connect my MYSQLserver , the note from my Linux System is 'Error 1130 (00000): Host 'my ip address' is not allowed to connect to this MySQL server. What could I do on this? Thanks again! (4 Replies)
Discussion started by: lancepanda
4 Replies

6. Web Development

Deleting databases in MYSQL problem asking...

Hi, I'm the new user of mysql. I facing one problem to delete one of the database in mysql. Hope can get all of your suggestion and advice. Input: mysql> show databases; +-------------------------+ | Database | +-------------------------+ | information_schema | |... (3 Replies)
Discussion started by: patrick87
3 Replies

7. Shell Programming and Scripting

Mysql import problem

Dear folks, i am importing a database since yesterday that is innodb tablespace, the space of dump file is 4GB, almost 24 pass out it is not yet complete. What should i do to import it fastly. I have disable foreign key check because it have some issues so thats why i am importing it with source... (2 Replies)
Discussion started by: learnbash
2 Replies

8. UNIX and Linux Applications

mysql conflicts problem

:confused: hi all, RHEL 4 has installed on the server, when i use rpm -i command to install mysql5.1 enterprise version, error failed dependencies occurs as below: mysql conflicts with mysql-4.1.22-2.el4.i386 mysql conflicts with mysql-4.1.22-2.el4.x86_64 have tried rpm -e which was... (2 Replies)
Discussion started by: maxlee24
2 Replies

9. AIX

Configuring MySQL problem

Hi, Trying to setup mediawiki on a system but having problems configuring mysql: CC=/usr/bin ./configure checking build system type... powerpc-ibm-aix6.1.0.0 checking host system type... powerpc-ibm-aix6.1.0.0 checking target system type... powerpc-ibm-aix6.1.0.0 checking for a... (9 Replies)
Discussion started by: vpundit
9 Replies

10. Red Hat

Problem in installing mysql

Hi i am trying to install mysql rpm package on my linux machine but getting below error : warning: MySQL-embedded-5.5.28-1.rhel5.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5 error: Failed dependencies: MySQL-devel is needed by MySQL-embedded-5.5.28-1.rhel5.i386 ... (9 Replies)
Discussion started by: mukulverma2408
9 Replies
All times are GMT -4. The time now is 11:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy