2013-04-19 9 views
1

sqlite 데이터베이스를 mysql로 ​​변환했습니다.PHP를 사용하여 sqlite를 mysql로 ​​변환하는 방법

그렇다면 MySQL + PHP 코드로 sqlite + PHP 코드를 변환해야합니다. sqlite를 mysql로 ​​변환하는 메소드를 포함하는 문서가 있습니까? 이 내 코드

<?php 

try { 
    //open the database 
    $db = new PDO('sqlite:cars.sqlite'); //will create the file in current directory. Current directory must be writable 

    //create the database if does not exist 
    $db->exec("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY, manufacturer TEXT, year INTEGER, price INTEGER)"); 

    //select all data from the table 
    $select = $db->prepare('SELECT * FROM cars ORDER BY id ASC LIMIT 100'); 
    $select->execute(); 

    $out = array(
    'cars' => $select->fetchAll(PDO::FETCH_ASSOC) 
); 
    echo json_encode($out); 

    // close the database connection 
    $db = NULL; 
} 
catch (PDOException $e) { 
    print 'Exception : ' . $e->getMessage(); 
} 
?> 

MySQL의 테이블

감사 .. : 자동차

CREATE TABLE IF NOT EXISTS `cars` (
    `id` int(11) NOT NULL DEFAULT '0', 
    `manufacturer` text, 
    `year` int(11) DEFAULT '0', 
    `price` text, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

답변

2

당신이 PDO를 사용하는 등, 단지 MySQL의를 만들 PDO obje 같은 것으로

$db = new PDO('sqlite:cars.sqlite'); 

: CT,이 부분을 변경

$dsn = 'mysql:dbname=yourdbname;host=127.0.0.1'; 
$user = 'yourdbuser'; 
$password = 'yourdbpass'; 
$dbh = new PDO($dsn, $user, $password); 

하면됩니다. 다른 것을 바꾸지 않아도됩니다.

PDO는 휴대용 연결 개체이므로 여러 다른 데이터베이스에서 사용할 수 있습니다. PDO에 대한 추가 정보 :

+0

DB 연결, $ con = mysql_connect ("localhost", "root", "") 또는 die ("error1"); $ db = mysql_select_db ("decsys", $ con) 또는 die ("error2") –

+0

아니요, 이미 PDO를 사용하고 있습니다. 그것을 계속 사용하십시오! mysql_connect와 친구는 더 이상 사용되지 않으며 곧 삭제 될 것입니다. 업데이트 된 답변보기 – egig

+0

고마워 Mr.Charlie. 너는 내 하루를 구했다 !! –

관련 문제