Solved

import mysql database from sql file

Posted on 2006-06-28
8
347 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
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:
RQuadling 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now