<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: export code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 20:46:41 GMT</pubDate>
    <description>DZone Snippets: export code</description>
    <item>
      <title>export and import by php</title>
      <link>http://snippets.dzone.com/posts/show/5654</link>
      <description>There are at least three ways to backup your MySQL Database :&lt;br /&gt;&lt;br /&gt;Execute a database backup query from PHP file.&lt;br /&gt;Run mysqldump using system() function.&lt;br /&gt;Use phpMyAdmin to do the backup.&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Execute a database backup query from PHP file&lt;br /&gt;&lt;br /&gt;Below is an example of using SELECT INTO OUTFILE query for creating table backup :&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;include 'config.php';&lt;br /&gt;include 'opendb.php';&lt;br /&gt;&lt;br /&gt;$tableName  = 'mypet';&lt;br /&gt;$backupFile = 'backup/mypet.sql';&lt;br /&gt;$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";&lt;br /&gt;$result = mysql_query($query);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;include 'closedb.php';&lt;br /&gt;?&gt;&lt;br /&gt;To restore the backup you just need to run LOAD DATA INFILE query like this :&lt;br /&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;include 'config.php';&lt;br /&gt;include 'opendb.php';&lt;br /&gt;&lt;br /&gt;$tableName  = 'mypet';&lt;br /&gt;$backupFile = 'mypet.sql';&lt;br /&gt;$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";&lt;br /&gt;$result = mysql_query($query);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;include 'closedb.php';&lt;br /&gt;?&gt;&lt;br /&gt;It's a good idea to name the backup file as tablename.sql so you'll know from which table the backup file is&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Run mysqldump using system() function</description>
      <pubDate>Mon, 16 Jun 2008 07:34:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5654</guid>
      <author>ajay1kumar1 (Ajay kumar)</author>
    </item>
    <item>
      <title>Changing the Bash prompt</title>
      <link>http://snippets.dzone.com/posts/show/5349</link>
      <description>The following shell command changes the bash prompt from this "james@cryton:~/projects/pear_housekeeping2/housekeeping$" to this "james@cryton&gt; "&lt;br /&gt;&lt;code&gt;&lt;br /&gt;export PS1="\n\u@\H&gt;"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I tried the above example on Ubuntu 7.10.&lt;br /&gt;&lt;br /&gt;Reference:&lt;br /&gt;&lt;a href="http://www.ibm.com/developerworks/linux/library/l-tip-prompt/"&gt;Tip: Prompt magic&lt;/a&gt; [ibm.com]&lt;br /&gt;&lt;a href="http://tldp.org/HOWTO/Bash-Prompt-HOWTO/"&gt;Bash Prompt HOWTO&lt;/a&gt; [tldp.org]</description>
      <pubDate>Fri, 11 Apr 2008 22:43:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5349</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>PHP : Exportar CSV a mySQL / Export CSV to mySQL</title>
      <link>http://snippets.dzone.com/posts/show/4344</link>
      <description>C&#243;odigo fuente / Source code :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function exportarCSV_a_mySQL($fileCSV)&lt;br /&gt;{&lt;br /&gt;	$registros=0;&lt;br /&gt;&lt;br /&gt;	$ruta=$fileCSV['tmp_name'];&lt;br /&gt;&lt;br /&gt;	if(!file_exists($ruta))&lt;br /&gt;	{return false;}&lt;br /&gt;&lt;br /&gt;	$tabla=quitar_extension($fileCSV['name']);&lt;br /&gt;	&lt;br /&gt;	$borra_tabla="DROP TABLE `".$tabla."`";&lt;br /&gt;	mysql_query($borra_tabla);&lt;br /&gt;	$f=fopen($ruta,"r");&lt;br /&gt;	if($f)&lt;br /&gt;	{&lt;br /&gt;		echo "&lt;b&gt;Guardando CSV en la BDD :&lt;/b&gt;&lt;br /&gt;";&lt;br /&gt;		$contenido=fread($f,filesize($ruta));&lt;br /&gt;		fclose($f);&lt;br /&gt;		$contenido=ereg_replace("\r\n", "\n" , $contenido); // convertimos windows a unix&lt;br /&gt;		$lineas=explode("\n",$contenido);&lt;br /&gt;		$titulo=explode(";",$lineas[0]);&lt;br /&gt;		$NUM_CAMPOS=count($titulo);&lt;br /&gt;		$sql_generado_para_eliminar="";&lt;br /&gt;		$crear_tabla_campos="";&lt;br /&gt;		for($i=0;$i&lt;$NUM_CAMPOS;$i++)&lt;br /&gt;		{&lt;br /&gt;		$titulo[$i]=ereg_replace("\"", "" , $titulo[$i]); // kitamos comillas&lt;br /&gt;		$sql_generado_para_eliminar.=" AND `".$titulo[$i]."` =''";&lt;br /&gt;		$crear_tabla_campos.="`".$titulo[$i]."` varchar(60) NOT NULL";&lt;br /&gt;			if($i+1!=$NUM_CAMPOS)// si no es el ultimo , ponemos coma&lt;br /&gt;			{&lt;br /&gt;			$crear_tabla_campos.=",";&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		$crear_tabla="CREATE TABLE `".$tabla."` (".$crear_tabla_campos.") ENGINE=MyISAM DEFAULT CHARSET=latin1;";&lt;br /&gt;		mysql_query($crear_tabla);&lt;br /&gt;		$linea=1;&lt;br /&gt;		do&lt;br /&gt;		{&lt;br /&gt;			$insertar_titulos="";&lt;br /&gt;			$insertar_campos="";&lt;br /&gt;			$campo=explode(";",$lineas[$linea]);&lt;br /&gt;			for($i=0;$i&lt;$NUM_CAMPOS;$i++)&lt;br /&gt;			{&lt;br /&gt;			$campo[$i]=ereg_replace("\"", "" , $campo[$i]);&lt;br /&gt;			$insertar_titulos.=" `".$titulo[$i]."` ";&lt;br /&gt;			$insertar_campos.=" '".$campo[$i]."' ";&lt;br /&gt;				if($i+1!=$NUM_CAMPOS)// si no es el ultimo , ponemos coma&lt;br /&gt;				{&lt;br /&gt;				$insertar_titulos.=",";&lt;br /&gt;				$insertar_campos.=",";&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			$sql="INSERT INTO `".$tabla."` ( ".$insertar_titulos." ) VALUES ( ".$insertar_campos." );";&lt;br /&gt;			if(mysql_query($sql))&lt;br /&gt;			{&lt;br /&gt;			echo ". ";&lt;br /&gt;			$registros++;&lt;br /&gt;			}&lt;br /&gt;			else&lt;br /&gt;			{echo "X ";return false;}&lt;br /&gt;		$linea++;&lt;br /&gt;		}while(next($lineas));&lt;br /&gt;&lt;br /&gt;	$sql="DELETE FROM `".$tabla."` WHERE 1".$sql_generado_para_eliminar;mysql_query($sql);&lt;br /&gt;	echo "&lt;br /&gt;";&lt;br /&gt;	return $tabla;&lt;br /&gt;	}&lt;br /&gt;	else&lt;br /&gt;	{&lt;br /&gt;	return false;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function quitar_extension($archivo)&lt;br /&gt;{&lt;br /&gt;	$extension = strrchr($archivo,".");&lt;br /&gt;	$pos=strpos($archivo,$extension);&lt;br /&gt;	return substr($archivo,0,$pos);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;C&#243;digo ejemplo de llamada / Code exmple call :&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$tabla = exportarCSV_a_mySQL($_FILES['archivo_csv']);&lt;br /&gt;if($tabla)&lt;br /&gt;{&lt;br /&gt;echo "Export OK in mysql table : ".$tabla;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;echo "Error in export ...";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 23 Jul 2007 10:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4344</guid>
      <author>Ricardo (Ricardo m. Garc&#237;a)</author>
    </item>
    <item>
      <title>Typo export to Wordpress WXR (script version)</title>
      <link>http://snippets.dzone.com/posts/show/4326</link>
      <description>I tried to use this method here: http://snippets.dzone.com/posts/show/3264 however my typo blog was too slow and it kept timing out (which is why we're switching to wordpress anyway...).  So I stole the code and hacked it up a bit so it could be run as a script.&lt;br /&gt;&lt;br /&gt;Put this in $RAILS_ROOT/script/wp_export.rb, and run it like:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;RAILS_ENV=production ./script/wp_export.rb &gt; out.wxr&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Import it with the "Import Wordpress" tool in your wordpress blog's admin interface.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;require File.dirname(__FILE__) + '/../config/boot'&lt;br /&gt;require 'environment'&lt;br /&gt;&lt;br /&gt;require 'application'&lt;br /&gt;&lt;br /&gt;# we need a controller to generate html output&lt;br /&gt;class FakeController &lt; ApplicationController&lt;br /&gt;  include ActionView::Helpers::TextHelper&lt;br /&gt;  include ActionView::Helpers::TagHelper&lt;br /&gt;end&lt;br /&gt;fake_controller = FakeController.new&lt;br /&gt;&lt;br /&gt;articles = Article.find(:all)&lt;br /&gt;&lt;br /&gt;xml = Builder::XmlMarkup.new(:target =&gt; STDOUT, :indent =&gt; 2)&lt;br /&gt;&lt;br /&gt;xml.instruct! :xml, :version=&gt;"1.0", :encoding=&gt;"UTF-8"&lt;br /&gt;xml.rss 'version' =&gt; "2.0",&lt;br /&gt;        'xmlns:content' =&gt; "http://purl.org/rss/1.0/modules/content/",&lt;br /&gt;        'xmlns:wfw' =&gt; "http://wellformedweb.org/CommentAPI/",&lt;br /&gt;        'xmlns:dc' =&gt; "http://purl.org/dc/elements/1.1/",&lt;br /&gt;        'xmlns:wp' =&gt; "http://wordpress.org/export/1.0/" do&lt;br /&gt;  xml.channel do&lt;br /&gt;    xml.title "The Robot Co-op"&lt;br /&gt;    xml.link "http://www.robotcoop.com"&lt;br /&gt;    xml.language "en-us"&lt;br /&gt;    xml.ttl "40"&lt;br /&gt;    xml.description "Robot coop blog"&lt;br /&gt;&lt;br /&gt;    articles.each do |a|&lt;br /&gt;        xml.item do&lt;br /&gt;          xml.title a.title&lt;br /&gt;          xml.content(:encoded) { |x| x &lt;&lt; a.html(fake_controller, :all) }&lt;br /&gt;          xml.pubDate a.published_at.rfc2822&lt;br /&gt;          xml.guid "urn:uuid:{a.guid}", "isPermaLink" =&gt; "false"&lt;br /&gt;          author = a.user.name rescue a.author&lt;br /&gt;          xml.author author&lt;br /&gt;          xml.dc :creator, author&lt;br /&gt;          for category in a.categories&lt;br /&gt;            xml.category category.name&lt;br /&gt;          end&lt;br /&gt;          for tag in a.tags&lt;br /&gt;            xml.category tag.display_name&lt;br /&gt;          end&lt;br /&gt;          xml.wp :post_id, a.id&lt;br /&gt;          xml.wp :post_date, a.published_at.strftime("%Y-%m-%d %H:%M:%S")&lt;br /&gt;          xml.wp :comment_status, 'closed'&lt;br /&gt;          xml.wp :ping_status, 'closed'&lt;br /&gt;          xml.wp :post_name, a.permalink&lt;br /&gt;          xml.wp :status, 'publish'&lt;br /&gt;          xml.wp :post_parent, '0'&lt;br /&gt;          xml.wp :post_type, 'post'&lt;br /&gt;          for comment in a.comments&lt;br /&gt;            xml.wp(:comment) do&lt;br /&gt;              xml.wp :comment_id, comment.id&lt;br /&gt;              xml.wp :comment_author, comment.author&lt;br /&gt;              xml.wp :comment_author_email, comment.email&lt;br /&gt;              xml.wp :comment_author_url, comment.url&lt;br /&gt;              xml.wp :comment_author_IP, comment.ip&lt;br /&gt;              xml.wp :comment_date, comment.published_at.strftime("%Y-%m-%d %H:%M:%S")&lt;br /&gt;              xml.wp(:comment_content) { |x| x &lt;&lt; comment.body }&lt;br /&gt;              xml.wp :comment_approved, '1'&lt;br /&gt;              xml.wp :comment_parent, '0'&lt;br /&gt;            end&lt;br /&gt;          end&lt;br /&gt;       end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 18 Jul 2007 23:41:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4326</guid>
      <author>laurelfan (Laurel Fan)</author>
    </item>
    <item>
      <title>Export to Text</title>
      <link>http://snippets.dzone.com/posts/show/2531</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// insert code here..&lt;br /&gt;&lt;br /&gt;   1. Database db = DatabaseFactory.CreateDatabase();&lt;br /&gt;   2. DBCommandWrapper selectCommandWrapper = db.GetStoredProcCommandWrapper("sp_GetLatestArticles");&lt;br /&gt;   3. DataSet ds = db.ExecuteDataSet(selectCommandWrapper);&lt;br /&gt;   4. StringBuilder str = new StringBuilder();&lt;br /&gt;   5. for(int i=0;i&lt;=ds.Tables[0].Rows.Count - 1; i++)&lt;br /&gt;   6. {&lt;br /&gt;   7. for(int j=0;j&lt;=ds.Tables[0].Columns.Count - 1; j++)&lt;br /&gt;   8. {&lt;br /&gt;   9. str.Append(ds.Tables[0].Rows[i][j].ToString());&lt;br /&gt;  10. }&lt;br /&gt;  11. str.Append("&lt;BR&gt;");&lt;br /&gt;  12. }&lt;br /&gt;  13. Response.Clear();&lt;br /&gt;  14. Response.AddHeader("content-disposition", "attachment;filename=FileName.txt");&lt;br /&gt;  15. Response.Charset = "";&lt;br /&gt;  16. Response.Cache.SetCacheability(HttpCacheability.NoCache);&lt;br /&gt;  17. Response.ContentType = "application/vnd.text";&lt;br /&gt;  18. System.IO.StringWriter stringWrite = new System.IO.StringWriter();&lt;br /&gt;  19. System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);&lt;br /&gt;  20. Response.Write(str.ToString());&lt;br /&gt;&lt;br /&gt;Response.End();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Sep 2006 00:21:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2531</guid>
      <author>jdmays ()</author>
    </item>
  </channel>
</rss>
