|
Can this be done with MySQL? Is it possible at all?
09-25-2007, 06:06 PM
|
Can this be done with MySQL? Is it possible at all?
|
Posts: 13
Location: Cleveland, Ohio USA
|
I realize I should and do plan to someday learn MySQL but there's so much; so many applications, languages, etc. to learn and so little time! Right now, one of the main reasons my interest is once again turning toward MySQL is because there's something that comes up time and time again that I'd like to accomplish and I think a knowledge of MySQL is what might make it possible.
I'll describe what I'd like to do but, at this point, my question here on this forum isn't so much "How do I do it?" but basically "Can this be done at all?" and/or "If it can be done, is working with MySQL the way it would be done?"
If I am barking up the wrong tree, I'd appreciate if someone could kindly point me in the direction of the right tree.
As much as I love the power, options, functionality, time-savings, etc, granted up by all the blogs, forums, content management systems, etc. available and I appreciate all the work the developers have done to create them, I am often less than pleased with using their control panels. They're often slow, tedious, inefficient, time-wasters that, on occasion, don't even work at all. While I usually try to work within the systems they've created, when it comes to tasks such as uploading files or images, editing files, templates or style sheets, etc. rather than using their "file managers", installers "online text editors", etc to step through one of their control panels after another; click, wait, click, wait, click, waiting.... I find it way faster and far more efficient to simply use my trusty ol' ftp program and text editor.
What I haven't yet found a similar contol panel "workaround" for is to accomplish something like the following:
Lets say, for example, you want to create a forum category that, within it, has a board dedicated to each state (of the union, U.S. of A.). If it takes 3 0r 4 steps to create each board, that's about 200 steps. If you wanted 2 such categories that about 400 steps plus you'd have to type in each state name twice! A lot of tedious steps, a lot of waiting, and a lot of room for errors!
I'm thinking there must instead be way to export a table (or file or whatever), editing it with a text editor, combine a list of the states, using copy/paste, prefix/suffix lines, a bit of search and replace, import and voila! done in a fraction of the time!
Is this possible?
Thanks
|
|
|
|
09-25-2007, 08:30 PM
|
Re: Can this be done with MySQL? Is it possible at all?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
yes it is.
SQL is a syntaxical (not sure about that word in English) language, so everything can be scripted.
Once you have analyzed the table structure, simply write a text file that contains every query you will need.
Use your text editor, or a language like php tp go through iteration quickier and to generate your file.
Then, simply import it into the db, and the actions contained into that file will be executed.
For that, you need an direct access to the db.
Either via a shell and the mysql client, or via an application like phpMyAdmin.
From the mysql command shell, simply type to execute every commands stored into the file command.sql.
Or use the "import" feature of phpMyAdmin, it does the same thing.
To export a table, it's almost the same. With phpMyAdmin, you have a "backup" option that does that.
If you want to use native tools, you'll need either a shell access on the server or a remote tcp connection to your db, and to use the mysqldump program (it's packaged into the mysql distribution).
This tool will output back the schema and/or the datas of any tbale you specify.
For the record, on linux, to save the output rather than sending it to the screen, do
Code:
mysqldump -u username -p db_name -t table>file.sql
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
09-26-2007, 02:06 AM
|
Re: Can this be done with MySQL? Is it possible at all?
|
Posts: 13
Location: Cleveland, Ohio USA
|
Thanks tripy, I appreciate your (prompt!) response.
Though some of your explanation was a bit over my head it, at least, confirmed to me that it was possible so gave me the motivation to experiment.
I tried it and it seemed to work but seemed almost too easy! What I did wasn't exactly as described but seemed to work. I'd appreciate if someone could confirm that what I did was correct and if not, please correct me. (If it was correct, this could be helpful to others)
I created a few categories and boards in a fresh install of SMF (Simple Machines Forum).
Went into phpMyAdmin, accessed the appropriate database
Exported smf_boards as .SQL, Saved as file.
Opened the file with a text editor (BBEdit)
found the lines that corresponded to boards, copied/pasted them, modified name and ID_BOARD numbers:
Code:
INSERT INTO `smf_boards` VALUES (4, 4, 0, 0, 2, 0, 0, '-1,0,2', 'Alabama', '', 0, 0, 0, 0, 0, 0);
INSERT INTO `smf_boards` VALUES (5, 4, 0, 0, 2, 0, 0, '-1,0,2', 'Alaska', '', 0, 0, 0, 0, 0, 0);
INSERT INTO `smf_boards` VALUES (6, 4, 0, 0, 2, 0, 0, '-1,0,2', 'Arizona', '', 0, 0, 0, 0, 0, 0);
... etc...
When finished, tried to import but received an error message:
Code:
MySQL said: Documentation
#1050 - Table 'smf_boards' already exists
What I did next is what I am most unsure of.
Went back to the database, selected that table and, under action, opted to DROP Table
After informed that
Code:
Table smf_boards has been dropped
then tried again to Import and was met with success
Code:
Import has been successfully finished, 58 queries executed.
It seemed to work well enough but is this the proper or best way to do it?
Thanks 
|
|
|
|
09-26-2007, 02:35 AM
|
Re: Can this be done with MySQL? Is it possible at all?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
I'd say almost...
The problem you had was that you had the table creation command in the script.
Effectively, dropping (deleting) the table and re-creating everything is an option, but I would not have extracted the table structure and only had the datas in the file.
That way, every "double" command would simlpy not be executed (as the primary key constraint would/should prevent 2 excatly identical rows to exists) and only our new rows would be added.
But otherwise, it's exactly as simple as that...
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
|
« Reply to Can this be done with MySQL? Is it possible at all?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|