Solved

import mysql database from sql file

Posted on 2006-06-28
8
343 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
Comment Utility
BigDump will help you.
http://www.ozerov.de/bigdump.php
0
 
LVL 6

Expert Comment

by:mattjp88
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save signature to file 7 44
Php Array Diff 3 26
php documentation 4 19
Not needed 13 53
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now