Solved

import mysql database from sql file

Posted on 2006-06-28
8
359 Views
Last Modified: 2010-04-30
I have a .sql file, and I want to import data from that file to my mysql database (somthing like phpmyadmin import data function)

How i'm gonna do that?
0
Comment
Question by:huy302
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:vmyth
ID: 17005965
BigDump will help you.
http://www.ozerov.de/bigdump.php
0
 
LVL 6

Expert Comment

by:mattjp88
ID: 17006868
not a PHP app, but pretty cool app I found in my searches: http://www.navicat.com/ .  Not free, but seems to be worth the cost.

-Matt
0
 
LVL 12

Expert Comment

by:str_kani
ID: 17007001
yes you can easily import this using phpmyadmin...
1) select the import option
2) open the sql file in your favourite editor
3) copy and paste the sql commands from your editor into the textarea in the import option
4) or specify the sql in the import option and click go...

5) that's it.
let me know if you need more help using phpmyadmin. :)
Good luck!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 17007409
Or ...

<?php
$s_SQL = file_get_contents($s_your_sql_file_name_here);
$r_conn = mysql_pconnect($s_server, $s_username, $s_password) or die('Could not connect : ' . mysql_errno() . ':' . mysql_error());
$r_results = mysql_query($s_SQL) or die('Could not execute query : ' . mysql_errno() . ':' . mysql_error());
mysql_free_results($r_results);
?>

0
 

Author Comment

by:huy302
ID: 17012548
sorry I didn't state myself clearly but, I mean using PHP code to import data, not using programs to do it
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 250 total points
ID: 17016136
Well. The PHP code I supplied would do that.

I use MSSQL. I have an old Sage Retrieve 4GL DB which I need to convert to SQL server for offline reporting.

I have a PHP script which converts the Sage DB to a SQL database, extracts all the data and then pushes the whole lot into SQL server.

All using PHP.

This is a snippet.

// Connect to the SQL Server.
      if (!in_array('VALIDATE', $argv))
            {
            $rConn = mssql_connect('richardquadling','sa','sa') or die("Unable to connect to the master database server.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            $sCreateDB = str_replace("
GO

",";

", file_get_contents('./TripleC.SQL'));
            mssql_query($sCreateDB, $rConn) or die("Unable to create the database.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            mssql_close($rConn);
            $rConn = @mssql_connect('richardquadling','KPI_User','KPI_User') or die("Unable to connect to the database server.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            @mssql_select_db('TripleCDataConversion',$rConn) or die("Unable to connect to the database.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
// Handle additional SQL.
            mssql_query("CREATE VIEW dbo.Distributor_CasingsReceived_CasingsAccepted AS SELECT TOP 100 PERCENT CT.DistributorCode, DI.Name, DI.AddressLines_1, DI.AddressLines_2, DI.AddressLines_3, DI.AddressLines_4, DI.Postcode, CT.TicketPeriod, COUNT(TC.IOCCode) AS CasingsReceived, COUNT(TC.AcceptCode) AS CasingsAccepted FROM dbo.CollectionTickets CT INNER JOIN dbo.TicketCasings TC ON CT.UniqueID = TC.ParentID INNER JOIN dbo.Distributors DI ON CT.DistributorCode = DI.DistributorCode WHERE (TC.IOCCode <> 'N') GROUP BY CT.DistributorCode, DI.Name, DI.AddressLines_1, DI.AddressLines_2, DI.AddressLines_3, DI.AddressLines_4, DI.Postcode, CT.TicketPeriod ORDER BY CT.DistributorCode, CT.TicketPeriod", $rConn) or die("Unable to process additional SQL - CREATE VIEW dbo.Distributor_CasingsReceived_CasingsAccepted.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            mssql_query("CREATE VIEW dbo.TTL_Casings AS SELECT TOP 100 PERCENT CT.TicketNumber, CU.HeadOfficeCode AS CustomerHeadOfficeCode, CT.CustomerCode, DI.Name AS DistributorName, CT.TicketDate, CT.TicketPeriod, TC.CasingSizeCode, TC.MakeCode, TC.SerialNumber, TC.IOCCode, TC.AcceptCode, TC.RejectCode, TC.VehicleNumber, TC.MinTreadDepth, TC.MaxTreadDepth, CASE Regroove WHEN 1 THEN 'Yes' ELSE 'No' END AS Regroove, CM.Description AS CommentDescription, TC.StudLocation, TC.GroupCode FROM dbo.CollectionTickets CT LEFT OUTER JOIN dbo.Customers CU ON CT.CustomerCode = CU.CustomerCode LEFT OUTER JOIN dbo.Distributors DI ON CT.DistributorCode = DI.DistributorCode LEFT OUTER JOIN dbo.TicketCasings TC ON CT.UniqueID = TC.ParentID LEFT OUTER JOIN dbo.CommentCodes CM ON TC.CommentCode = CM.CommentCode WHERE (CT.CustomerCode = '198') ORDER BY CT.TicketDate, CT.TicketNumber, TC.CasingSizeCode", $rConn) or die("Unable to process additional SQL - CREATE VIEW dbo.TTL_Casings.\nMicrosoft SQL Server Error : " . mssql_get_last_message() . "\n");
            }

So, yes, you CAN use PHP code to completely build your DB. The SQL script I have is like the script produced by MSSQL when you ask it to dump the DB ...


/****** Object:  Database TripleCDataConversion    Script Date: 02/02/2006 10:02:30 AM ******/
use [master]
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'TripleCDataConversion')
      DROP DATABASE [TripleCDataConversion]
GO

CREATE DATABASE [TripleCDataConversion]  ON (NAME = N'TripleCDataConversion_Data', FILENAME = N'D:\Data\SQL 2000\MSSQL\Data\TripleCDataConversion_Data.MDF' , SIZE = 1024, FILEGROWTH = 10%) LOG ON (NAME = N'TripleCDataConversion_Log', FILENAME = N'D:\Data\SQL 2000\MSSQL\Data\TripleCDataConversion_Log.LDF' , SIZE = 20, FILEGROWTH = 10%)
 COLLATE Latin1_General_CI_AS
GO

exec sp_dboption N'TripleCDataConversion', N'autoclose', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'bulkcopy', N'false'
GO

exec sp_dboption N'TripleCDataConversion', N'trunc. log', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'torn page detection', N'true'
GO

exec sp_dboption N'TripleCDataConversion', N'read only', N'false'
GO

exec sp_dboption N'TripleCDataConversion', N'dbo use', N'false'
GO

etc...

So. All in code.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question