How to run and link a .sql file from Perl - mysql

I am looking for a way to implement MySQL and Perl to make a program. Where I'm lost is that I have a .sql file, that creates 3 tables for the Perl program to use. How do you:
1) Execute the file fileName.sql in Perl to create the tables
2) Link those created tables into manipulable variables in Perl Program (like an example being able to add a user to one of the tables)

Execute the file fileName.sql in Perl to create the tables
Usually you would set up the database in advance and use the command line mysql client or a GUI such as PHPMyAdmin to load the .sql file.
You could use a call to system to do the former though.
Link those created tables into manipulable variables in Perl Program
Low level access to databases in Perl is usually handled via the DBI module.
Getting something along the lines of a variable per table calls for an ORM. DBIx::Class is a popular choice for this.

In Perl you use the DBI database interface. In your case, you will also be using something like the DBD::MySQL driver.
There is lots of help available on this topic (including lots of questions on this site).
As for the specific question of your .sql file, there are a few approaches you could take, depending on how fancy you want to get:
You could just copy and paste the commands into your program as you write it.
You could execute an external program that will run the .sql file (for example, by using system()).
You could programmatically read in the .sql file and send the commands from within your program. A module could help you with this (I found SQL::Script on CPAN, which looks useful, though I don't have any experience with it).
I suggest you pick an approach, try it, and ask if you have any specific problems.

Related

Importing procedures in MySQL database

I was wondering if there was a way to write the code of a SQL procedure in a file .sql (or at least I think it should be .sql) and then import that procedure in my database (importing the file .sql). I would like to write the code in a separated file using an IDE like Geany because it is visually more confortable for me.
I already searched through Google but I only found ways to import (and export) in phpmyadmin, but I don't even know what it is. I am typing the code of my procedures using the terminal line, but I'd like to do this in some other ways.
Is there a way to do it?
Use MySQL Workbench.
https://www.mysql.com/products/workbench/
It's easy - clean interface.
There is also HeidSQL (https://www.heidisql.com/) and a whole lot of others.
Personally I prefer MySQL Workbench.

Import a database to DataGrip(0xDBE)

How do I import a database just like in phpmyadmin at DataGrip?
I have the .sql exported from phpmyadmin... but those are lots of lines so that the IDE stops working when trying to run the whole .sql
In DataGrip go to File > Open and select your mysql dump file. Then right click the tab for the file to get the context menu, and select "Run [your filename...]" option. It may ask you to select your schema to apply the run to. But this is how I accomplished importing a dump from phpMyadmin using DataGrip.
Jetbrains documentation on running SQL scripts does not provide a ton of information on processing large insert statements. There is a discussion in the Datagrip community forums and apparently upcoming features to make working with large scripts easier.
Quote from thread:
Huge SQL files can be executed from Files view (use a context menu action).
I assume you are attempting to import a database export which is a series of SQL statements saved to a file. There could be a memory issue if you are attempting to run a large SQL file in memory. Try the following.
Insert commit statements in your SQL file in a text editor. This can even be done from within datagrip. Every couple of hundred statements you can place the line
commit;
which should purge the previous statements from memory. I strongly recommend saving the file which you edit separately from the export script. This method is not applicable if you need an all or nothing import, meaning if even one statement or block fails you want all of the statement to be rolled back.
1 - Going to View->Tool Windows->Files
2 - Going to schema folder and open it in windows explorer after that past your dump file in my example i will past MyDump.dmp .
3 - Right click on the MyDump.dmp and run it .
To import data from a script file, run the file as it is described in Run database code. In addition to script files, you can import a CSV, TSV, or any other text file that contains delimiter-separated values.
https://www.jetbrains.com/help/datagrip/import-data.html

What is best approach for using inno-setup to load a MySQL database

Am a bit new to inno-setup and using it to create my java executable file and using MySQL as a database file. i just have two question:
First, If a user already has MySQL and i want to detect and load a database. do i use MySQL open database connectivity(odbc)
Second, which approach would be the best for loading/running the script. using a batch file or simply run the script within inno-setup?
i would really appreciate some code snippets
thanks

Zend export database for backup

I want to create a cronjob for making a backup (sql dump) from my database and e-mail it to me. Setting up the cronjob and stuff works great and I'm able to use parts of my zend application :)
Unfortunately I cannot use exec() or system() on my server so now I'm looking for a way to get the same result. I searched everywhere with all possible descriptions I could think of, but without any results.
So in short:
I want to backup my databaseup
Preferably in .sql format (like export in phpmyadmin)
Using the Zend framework (so I can use my already loaded
application.ini settings for the database)
I cannot use exec() or system()
I'm completely stuck so really anything would help! Thanks in advance!
The solution by David Walsh looks like what you want:
http://davidwalsh.name/backup-mysql-database-php
A php script that retrieves the tables in a database and saves the data in a .sql file.

How does the phpMyAdmin export feature work?

If I were to want to create a PHP function that does the same thing as the Export tab in phpMyAdmin, how could I do it? I don't know if there is a MySQL function that does this or if phpMyAdmin just builds the export file (in SQL that is) manually. Without shell access. Just using PHP.
I tried the documentation for mysqldump, but that seemed to require using the shell. I'm not quite sure what that even is -- maybe my question is: how do you use shell?
My silly idea is to allow non-technical users to build a site on one server (say a localhost) using MySQL then export the site, database and all, to another server (eg. a remote server).
I think I'm pretty clear on the Import process.
You can check the phpMyAdmin source code (an advantage of open-source software). Check the export.php script and the supporting functions in the libraries/export/sql.php script file.
In summary, what phpMyAdmin does is:
get a list of the tables in the given database (SHOW TABLES FROM...),
get the create query for each table (SHOW CREATE TABLE...),
parse it and extract column definitions from it,
get all data (SELECT * FROM...)
build a query according to column data.
I've written similar code for my own apps (for backup purposes, when the GPL license of phpMyAdmin doesn't allow me to use it), however I use DESCRIBE to get column definitions. I think they rather parse the SHOW CREATE TABLE output because contains more information than DESCRIBE output.
This way to generate SQL sentences requires a bit of care to handle the escaping but it allows for some flexibility, as you can convert types, filter or sanitize data, etc. It is also a lot slower than using a tool like mysqldump and you should take care of not consuming all available memory (write soon, write often, don't keep everything in memory).
If you will implement a migration process (from server to server) maybe it would be easier to do it with some shell scripting and calling mysqldump directly, unless you will do everything with PHP.