doulvyi2172 2013-04-19 02:35
浏览 74
已采纳

如何使用PHP将sqlite转换为mysql

I have converted sqlite database into mysql.

so then i need convert sqlite+php code into mysql+php code. do you have any document which containing methods to convert sqlite to mysql.

thanks..

this is my code

<?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 table:cars

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;
  • 写回答

1条回答 默认 最新

  • dongyan6910 2013-04-19 02:37
    关注

    as you are using PDO, just create Mysql PDO object, change this part:

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

    to something like:

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

    and you're done. You dont need to change anything other.

    PDO is portable connection object, it can be used with several different database. more about PDO: http://php.net/manual/en/pdo.construct.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器