<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>The Ji Village News &#187; Perl</title> <atom:link href="http://www.haidongji.com/category/technology/perl/feed/" rel="self" type="application/rss+xml" /><link>http://www.haidongji.com</link> <description>季庄新闻--Haidong Ji's Blog</description> <lastBuildDate>Mon, 30 Jan 2012 02:41:37 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.2</generator> <item><title>Installing Perl DBI and DBD-mysql on Windows 64 bit</title><link>http://www.haidongji.com/2011/06/20/installing-perl-dbi-and-dbd-mysql-on-windows-64-bit/</link> <comments>http://www.haidongji.com/2011/06/20/installing-perl-dbi-and-dbd-mysql-on-windows-64-bit/#comments</comments> <pubDate>Mon, 20 Jun 2011 21:28:33 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1214</guid> <description><![CDATA[I had trouble getting Perl DBI and DBD-mysql on Windows in the past. In addition, on Windows 64-bit, you sometimes see recommendations of using 32-bit Perl. Today I got to test the latest 64-bit ActiveState Perl distro for Windows, version 5.12.3.1204. I tested it on Windows 2008 R2 64-bit. I am happy to report that [...]]]></description> <content:encoded><![CDATA[<p>I had trouble getting <a
href="http://www.haidongji.com/2009/05/13/activestate-perl-510-windows-xp-and-dbd-mysql/">Perl DBI and DBD-mysql on Windows in the past</a>. In addition, on Windows 64-bit, you sometimes see recommendations of using 32-bit Perl.</p><p>Today I got to test the latest 64-bit ActiveState Perl distro for Windows, version 5.12.3.1204. I tested it on Windows 2008 R2 64-bit. I am happy to report that it works. I am not categorically recommend FOR the installation of 64-bit Perl on Windows, though.</p><p>Here are the steps:<br
/> 1. Get the ActiveState Perl 64-bit package for Windows and install it, following all the default options;<br
/> 2. On command prompt, do:<br
/> cd c:\perl64\bin<br
/> ppm install DBI<br
/> ppm install DBD-mysql</p><p>I then tested against both Oracle&#8217;s MySQL 5.5 Community Server and MariaDb&#8217;s 5.2.7 on Windows with MaatKit&#8217;s mk-table-checksum to confirm. And it worked fine:</p><p>C:\Users\Administrator\Downloads\maatkit-7540\maatkit-7540\bin>c:\Perl64\bin\perl.exe mk-table-checksum &#8211;databases mysql h=localhost,u=root,p=password</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2011/06/20/installing-perl-dbi-and-dbd-mysql-on-windows-64-bit/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Having fun with MySQL and Python: converting MySQL character set to utf8</title><link>http://www.haidongji.com/2010/09/15/having-fun-with-mysql-and-python-converting-mysql-character-set-to-utf8/</link> <comments>http://www.haidongji.com/2010/09/15/having-fun-with-mysql-and-python-converting-mysql-character-set-to-utf8/#comments</comments> <pubDate>Thu, 16 Sep 2010 04:49:59 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=906</guid> <description><![CDATA[Lately I worked quite a bit with Python and Linux, writing monitoring and automation utilities. I am in a transition period, so I thought I ought to write some Python stuff interfacing with MySQL for fun, and start positioning myself for expanded career horizon, I hope. To get started, I thought it would be fun [...]]]></description> <content:encoded><![CDATA[<p>Lately I worked quite a bit with Python and Linux, writing monitoring and automation utilities. I am in a transition period, so I thought I ought to write some Python stuff interfacing with MySQL for fun, and start positioning myself for expanded career horizon, I hope.</p><p>To get started, I thought it would be fun to rewrite a <a
href="http://www.haidongji.com/2009/02/16/perl-script-to-convert-mysql-character-set-to-utf8/">Perl utility I wrote before</a> with Python. That script converts MySQL character sets to utf8, a very common task for wikis and blogs during an upgrade. This time, I did everything from scratch: firing up an Amazon EC2 Linux instance, hand install and configuring MySQL 5.1.50 (creating mysql user, group, wget tarball, setting directory ownership and permissions, creating symbolic to MySQL binaries, editing my.cnf, /etc/init.d/ and chkconfig automatic startup, environmental variables, the works), compiled and configured Python 2.7, compiled and configured Python easy_install, compiled and installed MySQLdb module for Python, and finally successfully rewrote and tested the utility in Python. It is listed at the bottom of this post.</p><p>The original Perl program used some SQL code generation technique based on metadata within information_schema. As is hopefully well-known by now, information_schema should be used with caution because it can leads to server lockup. In fact, I noticed it when I ran my Perl program on a shared web hosting server. See <a
href="http://www.xaprb.com/blog/2009/10/29/making-changes-to-many-tables-at-once/">Baron&#8217;s post here for an alternative way of doing it</a>. But my Python program listed below didn&#8217;t use it, just to be consistent with the one used in Perl. I had a lot of fun going through this exercise.</p><p>Here are some notes I took.<br
/> 1. It would be really nice if there is a catalog of every publicly available AMIs that contains detailed metadata that we can query, such as:<br
/> a. Version and distro of *nix;<br
/> b. What has been installed? Apache, MySQL, Python, PHP, Nagios, etc., etc.<br
/> c. For the software that has been installed, what is the version? What version(s) of MySQL was installed? What patches have been applied? Is it Python 2.4, 2.5, 2.6, 2.7, 3.0? Things like that.<br
/> d. Hardware related stuff: memory, disk size, etc.</p><p>Granted, I used AWS Management Console web interface to fire up and terminate instances, and accessed the servers via Putty. I lost my Linux workstation and haven&#8217;t built a new one yet. I do recall there were some EC2 command line tools on Linux that can do some of that when I last tried it a few months back. So it could be that things have improved and/or I just didn&#8217;t know the right way to get that out. If that is the case, I&#8217;d really appreciate it if you can give me some pointers. As it stands, the xml manifest file is too vague and not detailed enough.</p><p>2. Trying to do Python upgrade, say from 2.4 to 2.6 or 2.7, is generally not a good idea. Having multiple versions of Python side by side is probably a better way to go. Yes, it can be confusing, but I think in most cases, the cost/benefit analysis weighs against an upgrade: broken libraries and modules, failed cron jobs, you name it.</p><p>When having multiple versions of Python side by side, you can define a default version by some creative use of symbolic link. For instance, I did<br
/> ./configure &#038;&#038; make &#038;&#038; sudo make install<br
/> for Python 2.7 on a server that has 2.4 pre-installed. I then did:<br
/> ln -s /usr/local/bin/python2.7 /usr/bin/python<br
/> to make Python 2.7 the default version. If I need Python 2.4, I will just call python2.4 binary.</p><p>While on that note, I found it surprising that 2 AMI instances (ami-3e836657 and ami-01b75668) I used both had Python 2.4 installed, which in my mind was pretty old.</p><p>3. Installing MySQLdb Python module was a bit problematic. On one server,<br
/> sudo easy_install mysql-python<br
/> did not work for me. I didn&#8217;t spend time digging because the following worked:<br
/> sudo yum install MySQL-python<br
/> On another server, easy_install mysql-python did work, but when I did import MySQLdb, I got:<br
/> ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory<br
/> To fix that, do:<br
/> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib<br
/> Here is the Python code</p><pre class="brush: python">
import MySQLdb
db = MySQLdb.connect(host=&quot;localhost&quot;, user=&quot;root&quot;, passwd=&quot;&quot;, db=&quot;charset_test&quot;, unix_socket=&quot;/tmp/mysql.sock&quot;)
cursor = db.cursor()
sql = &quot;SELECT CONCAT(&#039;ALTER TABLE &#039;, table_name, &#039; MODIFY &#039;, column_name, &#039; &#039;, REPLACE(column_type, &#039;char&#039;, &#039;binary&#039;)) FROM information_schema.columns WHERE table_schema = &#039;charset_test&#039; and data_type LIKE &#039;%char%&#039;&quot;
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
        &quot;&quot;&quot;Convert char types to binary types first&quot;&quot;&quot;
        cursor.execute(row[0])
        &quot;&quot;&quot;Now we convert binary type back to char with CHARACTER SET utf8 defined&quot;&quot;&quot;
        cursor.execute(row[0].rpartition(&#039; &#039;)[0] + &#039; &#039; + row[0].rpartition(&#039; &#039;)[2].replace(&#039;binary&#039;, &#039;char&#039;) + &#039; CHARACTER SET utf8&#039;)
sql = &quot;SELECT CONCAT(&#039;ALTER TABLE &#039;, table_name, &#039; MODIFY &#039;, column_name, &#039; &#039;, REPLACE(column_type, &#039;text&#039;, &#039;blob&#039;)) FROM information_schema.columns WHERE table_schema = &#039;MyDatabase&#039; and data_type LIKE &#039;%text%&#039;&quot;
cursor.execute(sql)
results = cursor.fetchall()
for row in results:
        &quot;&quot;&quot;Convert text types to blob types first&quot;&quot;&quot;
        cursor.execute(row[0])
        &quot;&quot;&quot;Now we convert blob type back to text with CHARACTER SET utf8 defined&quot;&quot;&quot;
        cursor.execute(row[0].rpartition(&#039; &#039;)[0] + &#039; &#039; + row[0].rpartition(&#039; &#039;)[2].replace(&#039;blob&#039;, &#039;text&#039;) + &#039; CHARACTER SET utf8&#039;)
db.close()
</pre>]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2010/09/15/having-fun-with-mysql-and-python-converting-mysql-character-set-to-utf8/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Perl script to split sql script into separate files</title><link>http://www.haidongji.com/2009/05/29/perl-script-to-split-sql-script-into-separate-files/</link> <comments>http://www.haidongji.com/2009/05/29/perl-script-to-split-sql-script-into-separate-files/#comments</comments> <pubDate>Fri, 29 May 2009 21:46:31 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Perl]]></category> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=645</guid> <description><![CDATA[Below is a quick script I put together to split one single file that contains many stored procedures into many files that contain just a single stored procedure. It can be easily modified for views, user-defined functions, etc.. Notes: 1. It does put BEGIN and END before and after the procedure creation. Because some script [...]]]></description> <content:encoded><![CDATA[<p>Below is a quick script I put together to split one single file that contains many stored procedures into many files that contain just a single stored procedure. It can be easily modified for views, user-defined functions, etc..</p><p>Notes:<br
/> 1. It does put BEGIN and END before and after the procedure creation. Because some script has grant statements in it, and I want to put END right after the procedure creation, therefore there is a variable called $FirstGrant;<br
/> 2. The new files created will be named like StoredProcedureName.sql.</p><pre class="brush: perl">
#!/usr/bin/perl
use strict;
open(MYDATA, &quot;c:\\junk\\MyDb\\AllProc.sql&quot;) or
  die(&quot;Error: cannot open file &#039;AllProc.sql&#039;\n&quot;);
    my $FirstGrant = 0;
while( my $line = &lt;mydata&gt; ){
if ($line =~ /^create\s+proc/i) {
   close (FILE);
   $FirstGrant = 0;
   (my $filename) = $line =~ /create\s+proce?d?u?r?e?\s+\[?d?b?o?]?\.?\[?(\w+)/i;
   open (FILE, &quot;&gt;&quot;, &quot;c:\\junk\\MyDb\\&quot;.$filename.&quot;.sql&quot;);
   print FILE &quot;IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N&#039;[dbo].[$filename]&#039;) AND OBJECTPROPERTY(id,N&#039;IsProcedure&#039;) = 1)\n&quot;;
   print FILE &quot;BEGIN\n&quot;;
   print FILE $line;
}
else {
if (($line =~ /^grant\s+execute/i) &amp;&amp; ($FirstGrant == 0)) {
   print FILE &quot;END\n&quot;;
   $FirstGrant = 1;
}
   print FILE $line;
}
}
close MYDATA;
</pre><p></mydata></p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/05/29/perl-script-to-split-sql-script-into-separate-files/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Generate insert scripts for Sql Server tables with Perl</title><link>http://www.haidongji.com/2009/05/18/generate-insert-scripts-for-sql-server-tables-with-perl/</link> <comments>http://www.haidongji.com/2009/05/18/generate-insert-scripts-for-sql-server-tables-with-perl/#comments</comments> <pubDate>Tue, 19 May 2009 01:03:13 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Perl]]></category> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=626</guid> <description><![CDATA[Here is a quick hack with Perl to generate insert statements to reproduce data already exist in a given table. It is similar to results mysqldump generates. A few things worth mentioning here: 1. This script uses a text file, TableList, that contains a list of tables that you want insert statements created. Each line [...]]]></description> <content:encoded><![CDATA[<p>Here is a quick hack with Perl to generate insert statements to reproduce data already exist in a given table. It is similar to  results mysqldump generates.</p><p>A few things worth mentioning here:<br
/> 1. This script uses a text file, TableList, that contains a list of tables that you want insert statements created. Each line in the list contains the database name and table name, separated by space(s). This is a quick hack, one could easily pass the file name as an input;<br
/> 2. Obviously the connection information needs to be modified for your own needs;<br
/> 3. The script does escape single quote in character fields properly;<br
/> 4. Use<br
/> perl ScriptName.pl > c:\MyFolder\MyTableInsert.sql<br
/> to collect the results into c:\MyFolder\MyTableInsert.sql file.<br
/> 5. Thanks to Summit Amar and people who commented on <a
href="http://www.codeproject.com/KB/database/InsertGeneratorPack.aspx">this article</a> for sql script ideas.<br
/> 6. Update: modified the script so it handles identity field if it is present in a table.</p><p>By the way, Microsoft&#8217;s new scripting language, PowerShell, borrowed a lot of good stuff from Perl and Unix shell scripting, which is pretty powerful. My friends Yan Pan and Ananthakumar Muthusamy wrote a book, <a
href="http://www.amazon.com/Microsoft-Server-Administration-Windows-PowerShell/dp/0470477288/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1242693788&#038;sr=8-1">Microsoft SQL Server 2008 Administration with Windows PowerShell</a>, which I helped to tech-edit. If you are looking to learn more automation and scripting tricks with PowerShell, definitely check out that book, which will come out next month.</p><pre class="brush: perl">
#!/usr/bin/perl
use strict;
use Win32::SqlServer;
# Get server name from the command line
# my $ServerName = shift or die &quot;Please enter server name as the first only parameter needed&quot;;
# my $TableName = &quot;authors&quot;;
open(MYDATA, &quot;RefList.txt&quot;) or
  die(&quot;Error: cannot open file &#039;RefList.txt&#039;\n&quot;);
while( my $line = &lt;mydata&gt; ){
  $line =~ /(\w+)\s+(\w+)/;
  my $db;
  my $table;
  if ($db ne $1){
	print &quot;-----Tables in $1-----\n&quot;;
  }
  $db = $1;
  my $sqlsrv = sql_init(&quot;.&quot;, &quot;sa&quot;, &quot;s3cr3t&quot;, $1);
  my $HasIdentityColumn = $sqlsrv-&gt;sql_one(&quot;select objectproperty(object_id(&#039;$2&#039;), &#039;TableHasIdentity&#039;)&quot;);
  if ($HasIdentityColumn == 1) {
	print &quot;SET IDENTITY_INSERT $2 ON\n&quot;;
  }
# Our SQL statement.
  my $stmnt = &lt; &lt;SQLEND;
--Declare a cursor to retrieve column specific information for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type FROM information_schema.columns WHERE table_name = &#039;$2&#039;
OPEN cursCol
DECLARE \@string nvarchar(3000) --for storing the first half of INSERT statement
DECLARE \@stringData nvarchar(3000) --for storing the data (VALUES) related statement
DECLARE \@dataType nvarchar(1000) --data types returned for respective columns
SET \@string=&#039;INSERT &#039;+ &#039;$2&#039; +&#039;(&#039;
SET \@stringData=&#039;&#039;
DECLARE \@colName nvarchar(50)
FETCH NEXT FROM cursCol INTO \@colName,\@dataType
IF \@\@fetch_status&lt;&gt;0
	begin
	--print &#039;Table &#039; + $2 + &#039; not found, processing skipped.&#039;
	close curscol
	deallocate curscol
	return
END
WHILE \@\@FETCH_STATUS=0
BEGIN
IF \@dataType in (&#039;varchar&#039;,&#039;char&#039;,&#039;nchar&#039;,&#039;nvarchar&#039;)
BEGIN
        SET \@stringData=\@stringData+&#039;&#039;&#039;&#039;+&#039;&#039;&#039;+isnull(&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+REPLACE(&#039;+\@colName+&#039;,&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;)+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;NULL&#039;&#039;)+&#039;&#039;,&#039;&#039;+&#039;
END
ELSE
if \@dataType in (&#039;text&#039;,&#039;ntext&#039;) --if the datatype is text or something else
BEGIN
        SET \@stringData=\@stringData+&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;+isnull(REPLACE(cast(&#039;+\@colName+&#039; as varchar(2000)),&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;),&#039;&#039;&#039;&#039;)+&#039;&#039;&#039;&#039;&#039;&#039;,&#039;&#039;+&#039;
END
ELSE
IF \@dataType = &#039;money&#039; --because money doesn&#039;t get converted from varchar implicitly
BEGIN
	SET \@stringData=\@stringData+&#039;&#039;&#039;convert(money,&#039;&#039;&#039;&#039;&#039;&#039;+isnull(cast(&#039;+\@colName+&#039; as varchar(200)),&#039;&#039;0.0000&#039;&#039;)+&#039;&#039;&#039;&#039;&#039;&#039;),&#039;&#039;+&#039;
END
ELSE
IF \@dataType=&#039;datetime&#039;
BEGIN
	SET \@stringData=\@stringData+&#039;&#039;&#039;convert(datetime,&#039;+&#039;&#039;&#039;+isnull(&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+convert(varchar(200),&#039;+\@colName+&#039;,121)+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;NULL&#039;&#039;)+&#039;&#039;,121),&#039;&#039;+&#039;
END
ELSE
IF \@dataType=&#039;image&#039;
BEGIN
	SET \@stringData=\@stringData+&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;+isnull(cast(convert(varbinary,&#039;+\@colName+&#039;) as varchar(6)),&#039;&#039;0&#039;&#039;)+&#039;&#039;&#039;&#039;&#039;&#039;,&#039;&#039;+&#039;
END
ELSE --presuming the data type is int,bit,numeric,decimal
BEGIN
	--SET \@stringData=\@stringData+&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;&#039;+isnull(cast(&#039;+\@colName+&#039; as varchar(200)),&#039;&#039;0&#039;&#039;)+&#039;&#039;&#039;&#039;&#039;&#039;,&#039;&#039;+&#039;
	--SET \@stringData=\@stringData+&#039;&#039;&#039;convert(datetime,&#039;+&#039;&#039;&#039;+isnull(&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+convert(varchar(200),&#039;+\@colName+&#039;,121)+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;NULL&#039;&#039;)+&#039;&#039;,121),&#039;&#039;+&#039;
	SET \@stringData=\@stringData+&#039;&#039;&#039;&#039;+&#039;&#039;&#039;+isnull(&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;+convert(varchar(200),&#039;+\@colName+&#039;)+&#039;&#039;&#039;&#039;&#039;+&#039;&#039;&#039;&#039;&#039;,&#039;&#039;NULL&#039;&#039;)+&#039;&#039;,&#039;&#039;+&#039;
END
SET \@string=\@string+\@colName+&#039;,&#039;
FETCH NEXT FROM cursCol INTO \@colName,\@dataType
END
DECLARE \@Query nvarchar(4000)
SET \@query =&#039;SELECT &#039;&#039;&#039;+substring(\@string,0,len(\@string)) + &#039;) VALUES(&#039;&#039;+ &#039; + substring(\@stringData,0,len(\@stringData)-2)+&#039;&#039;&#039;+&#039;&#039;)&#039;&#039; Col1 FROM &#039;+ &#039;$2&#039;
exec sp_executesql \@query
CLOSE cursCol
DEALLOCATE cursCol
SQLEND
# Run query. The return value is a reference to an array.
  my $result = $sqlsrv-&gt;sql($stmnt);
# Print results. Each array entry is a hash reference.
  foreach my $row (@$result) {
	print &quot;$$row{Col1}\n&quot;;
  }
  if ($HasIdentityColumn == 1) {
	print &quot;SET IDENTITY_INSERT $2 OFF\n&quot;;
  }
}
close MYDATA;
</pre><p></mydata></p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/05/18/generate-insert-scripts-for-sql-server-tables-with-perl/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>ActiveState Perl 5.10, Windows XP, and DBD-mysql</title><link>http://www.haidongji.com/2009/05/13/activestate-perl-510-windows-xp-and-dbd-mysql/</link> <comments>http://www.haidongji.com/2009/05/13/activestate-perl-510-windows-xp-and-dbd-mysql/#comments</comments> <pubDate>Thu, 14 May 2009 02:54:08 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=623</guid> <description><![CDATA[A few months ago I installed ActivePerl 5.10 on a Windows XP Pro workstation. Next I tried to install DBD::mysql using CPAN, it failed. When I browsed modules via ppm, a GUI in this version of ActiveState Perl on Windows, DBD::mysql was not listed as an available module. I then downloaded the source code, manually [...]]]></description> <content:encoded><![CDATA[<p>A few months ago I installed ActivePerl 5.10 on a Windows XP Pro workstation. Next I tried to install DBD::mysql using CPAN, it failed. When I browsed modules via ppm, a GUI in this version of ActiveState Perl on Windows, DBD::mysql was not listed as an available module.</p><p>I then downloaded the source code, manually modified the MAKE file, fiddled with Visual Studio NMAKE, compiled it a few times, without success.</p><p>Google revealed the existence of <a
href="http://strawberryperl.com/">Strawberry Perl</a>. So I removed ActiveState Perl, installed Strawberry, ran ppm install DBI, ppm install DBD-mysql. And it worked.</p><p>Fast-forward a few weeks, I started playing with EPIC, a plugin for Eclipse that supposedly provides a nice IDE for Perl development and debugging. As a result, I started fiddling with PadWalker, a prerequisites for EPIC. I couldn&#8217;t remember all the details now due to frustration, but suffice it to say that I wrestled with EPIC, Eclipse, and Strawberry so much that I thought maybe I should gave ActiveState another look. Plus, I always wanted to verify for myself what the deal is with ActiveState Perl and DBD-mysql.</p><p>So I un-installed Strawberry Perl 5.10, reinstalled ActiveState Perl 5.10. And just like last time, DBD-mysql wouldn&#8217;t install. After un-installing ActiveState, reinstalling Strawberry, running ppm install DBD-mysql, I am back in business.</p><p>So, if you want to do Perl scripting with MySQL on Windows using DBD-mysql, pick Strawberry Perl, not ActiveState.</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/05/13/activestate-perl-510-windows-xp-and-dbd-mysql/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Quick way of finding version number of a Perl module</title><link>http://www.haidongji.com/2009/04/09/quick-way-of-finding-version-number-of-a-perl-module/</link> <comments>http://www.haidongji.com/2009/04/09/quick-way-of-finding-version-number-of-a-perl-module/#comments</comments> <pubDate>Fri, 10 Apr 2009 02:44:59 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Perl]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=607</guid> <description><![CDATA[Run this on command line perl -MMyModule -le &#039;print &#34;$MyModule::VERSION\n&#34;&#039; For instance, to find out the version number of DBI installed on the machine, run perl -MDBI -le &#039;print &#34;$DBI::VERSION\n&#34;&#039; To find out the version of DBD::mysql, run perl -MDBD::mysql -le &#039;print &#34;$DBD::mysql::VERSION\n&#34;&#039; One caveat: this process does not seem to work on Windows, at [...]]]></description> <content:encoded><![CDATA[<p>Run this on command line</p><pre class="brush: text">
perl -MMyModule -le &#039;print &quot;$MyModule::VERSION\n&quot;&#039;
</pre><p>For instance, to find out the version number of DBI installed on the machine, run</p><pre class="brush: text">
perl -MDBI -le &#039;print &quot;$DBI::VERSION\n&quot;&#039;
</pre><p>To find out the version of DBD::mysql, run</p><pre class="brush: text">
perl -MDBD::mysql -le &#039;print &quot;$DBD::mysql::VERSION\n&quot;&#039;
</pre><p>One caveat: this process does not seem to work on Windows, at least for my Strawberry Perl 5.10 installed on my Windows XP. I had to use the script below on Windows to get the version information. Here is the error message when I tried:</p><p>C:\junk>perl -MDBI -le &#8216;print &#8220;$DBI::VERSION\n&#8221;&#8216;<br
/> Can&#8217;t find string terminator &#8220;&#8216;&#8221; anywhere before EOF at -e line 1.</p><pre class="brush: perl">
#!/usr/bin/perl
use DBI;
use DBD::mysql;
print &quot;The version for DBI is $DBI::VERSION\n&quot;;
print &quot;The version for DBD::mysql is $DBD::mysql::VERSION\n&quot;;
</pre>]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/04/09/quick-way-of-finding-version-number-of-a-perl-module/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Perl script to convert MySQL character set to utf8</title><link>http://www.haidongji.com/2009/02/16/perl-script-to-convert-mysql-character-set-to-utf8/</link> <comments>http://www.haidongji.com/2009/02/16/perl-script-to-convert-mysql-character-set-to-utf8/#comments</comments> <pubDate>Mon, 16 Feb 2009 06:24:48 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=556</guid> <description><![CDATA[&#8220;Groovy, baby!&#8221;, as international man of mystery likes to say. It seems converting character and text data to utf8 character set is a common task for MySQL, especially during an upgrade. In fact, I had trouble with it during server and WordPress database upgrade for this blog site. I wrote about it in this post, [...]]]></description> <content:encoded><![CDATA[<p>&#8220;Groovy, baby!&#8221;, as international man of mystery likes to say.</p><p>It seems converting character and text data to utf8 character set is a common task for MySQL, especially during an upgrade. In fact, I had trouble with it during server and WordPress database upgrade for this blog site. I wrote about it <a
href="http://www.haidongji.com/2008/11/11/convert-character-set-to-utf8-in-mysql/">in this post</a>, where I explained how to do it step-by-step using a command line tool such as mysql, taking advantage of some nifty code generation trick with information_schema.</p><p>One drawback of that method is that it&#8217;s a manual process, therefore time-consuming and error-prone.</p><p>I spent some time today to cook up a simple Perl script to automate this task. See below. Remember to change MyDatabase to your database name (there are 3 places that need to be changed), and adjust the user name and password accordingly. This script also prints out sql statements used during the process so you will know what has been done to the database.</p><p>I have tested this successfully on my own WordPress blog. Hopefully it will help somebody out there. Enjoy!</p><pre class="brush: perl">
#!/usr/bin/perl
# MyUtf8Converter.pl - convert all character data to utf8 character set
use strict;
use warnings;
use DBI;
# data source name, username, password, connection attributes
my $dsn = &quot;DBI:mysql:MyDatabase:localhost&quot;;
my $user_name = &quot;MySQLUserName&quot;;
my $password = &quot;MySQLPassword&quot;;
my %conn_attrs = (RaiseError =&gt; 1, PrintError =&gt; 0, AutoCommit =&gt; 1);
# connect to database
my $dbh = DBI-&gt;connect ($dsn, $user_name, $password, \%conn_attrs);
# Convert char types first. Use information_schema to generate alter table code to convert data to binary first.
my $sth = $dbh-&gt;prepare (&quot;SELECT CONCAT(&#039;ALTER TABLE &#039;, table_name, &#039; MODIFY &#039;, column_name, &#039; &#039;, REPLACE(column_type, &#039;char&#039;, &#039;binary&#039;)) FROM information_schema.columns WHERE table_schema = &#039;MyDatabase&#039; and data_type LIKE &#039;%char%&#039;&quot;);
$sth-&gt;execute ();
# Run alter table stamement. First convert char types to binary types, then convert those to char types utf8 character set
while (my ($val) = $sth-&gt;fetchrow_array ())
    {
        print $val, &quot;\n&quot;;
        $dbh-&gt;do ($val);
        $val =~ s/binary/char/;
        $val = $val . &quot; CHARACTER SET utf8&quot;;
        print $val, &quot;\n&quot;;
        $dbh-&gt;do ($val);
    }
$sth-&gt;finish ();
# Now we convert text data. Use information_schema to generate alter table code to convert data to blob first.
$sth = $dbh-&gt;prepare (&quot;SELECT CONCAT(&#039;ALTER TABLE &#039;, table_name, &#039; MODIFY &#039;, column_name, &#039; &#039;, REPLACE(column_type, &#039;text&#039;, &#039;blob&#039;)) FROM information_schema.columns WHERE table_schema = &#039;MyDatabase&#039; and data_type LIKE &#039;%text%&#039;&quot;);
$sth-&gt;execute ();
# Run alter table stamement. First convert text types to blob types, then convert those to text types utf8 character set
while (my ($val) = $sth-&gt;fetchrow_array ())
    {
        print $val, &quot;\n&quot;;
        $dbh-&gt;do ($val);
        $val =~ s/blob/text/;
        $val = $val . &quot; CHARACTER SET utf8&quot;;
        print $val, &quot;\n&quot;;
        $dbh-&gt;do ($val);
    }
$sth-&gt;finish ();
$dbh-&gt;disconnect ();
</pre>]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/02/16/perl-script-to-convert-mysql-character-set-to-utf8/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>Identify a proxy when automatic configuration script is used</title><link>http://www.haidongji.com/2009/01/20/identify-a-proxy-when-automatic-configuration-script-is-used/</link> <comments>http://www.haidongji.com/2009/01/20/identify-a-proxy-when-automatic-configuration-script-is-used/#comments</comments> <pubDate>Wed, 21 Jan 2009 03:51:55 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[Perl]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=537</guid> <description><![CDATA[Many, if not all, corporations control and monitor employees web surfing and email activities. Sometimes a company may choose to use automatic configuration script to enable employees&#8217; web connection. This can be verified by: Internet Explorer: Tools -> Internet Options-> LAN Settings -> &#8220;Use automatic configuration script&#8221;; Firefox 3: Tools -> Options -> Advanced -> [...]]]></description> <content:encoded><![CDATA[<p>Many, if not all, corporations control and monitor employees web surfing and email activities. Sometimes a company may choose to use automatic configuration script to enable employees&#8217; web connection. This can be verified by:</p><ul><li>Internet Explorer: Tools -> Internet Options-> LAN Settings -> &#8220;Use automatic configuration script&#8221;;</li><li>Firefox 3: Tools -> Options -> Advanced -> Network -> Settings&#8230; -> Automatic proxy configuration URL:</li></ul><p>Sometimes you need to know a proxy server name or IP address. For instance, if you use Perl and would like to do an automatic binary install of a module, then you will need to define a proxy for cpan to use. This is the command to do that:</p><p>set HTTP_proxy=http://my.proxy.server:8000/</p><p>So obviously you need to know a proxy server name or IP address. To find that out, you need to copy the location where the automatic configuration script is located, put that address into your browser and open it. It will prompt you to save it as a file. Do that, then open the file up to search for proxy server names or IP addresses.</p><p>As a side, I was not able to install DBD::mysql on a Windows XP machine with cpan. I also tried to do manual installation from source code, with no success. I use ActiveState Perl 5.10 and 5.1.30-community MySQL Community Server (GPL). I stumbled into Strawberry Perl while doing research for that issue. Maybe I will check it out in the future.</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2009/01/20/identify-a-proxy-when-automatic-configuration-script-is-used/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Move Sql Server backup file and restore it with Perl</title><link>http://www.haidongji.com/2008/09/22/move-sql-server-backup-file-and-restore-it-with-perl/</link> <comments>http://www.haidongji.com/2008/09/22/move-sql-server-backup-file-and-restore-it-with-perl/#comments</comments> <pubDate>Mon, 22 Sep 2008 22:14:15 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Perl]]></category> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=348</guid> <description><![CDATA[#There are two key parts for this script. The first part copies the latest backup and paste it in the local g drive. The second part do the restore from this backup file. It&#8217;s a LightSpeed backup and restore. Linchi Shea&#8217;s SQLDBA helper Perl files are needed. This is mainly for my own safekeeping. #Part [...]]]></description> <content:encoded><![CDATA[<p>#There are two key parts for this script. The first part copies the latest backup and paste it in the local g drive. The second part do the restore from this backup file. It&#8217;s a LightSpeed backup and restore. Linchi Shea&#8217;s SQLDBA helper Perl files are needed. This is mainly for my own safekeeping.</p><pre class="brush: perl">
#Part 1
use File::Copy;
use File::stat;
use SQLDBA::Utility qw( dbaRunOsql );  # import the function
$dirname = shift or die &quot;Please provide a directory to search for&quot;;
$timediff=0;
opendir DIR, &quot;$dirname&quot;;
while (defined ($file = readdir(DIR)))
{
	if($file ne &quot;.&quot; &amp;&amp; $file ne &quot;..&quot;)
	{
		$diff = time()-stat(&quot;$dirname/$file&quot;)-&gt;mtime;
		if($timediff == 0)
                       {
                       	$timediff=$diff;
                       	$newest=$file;
                       }
                if($diff&lt; $timediff)
		       {
                       	$timediff=$diff;
                       	$newest=$file;
                       }
        }
}
print $newest,&quot;\n&quot;;
copy(&quot;$dirname/$newest&quot;, &quot;g:/$newest&quot;);
#Part 2
# Construct the SQL script
my $sql =&lt;&lt;__SQL__;
SET NOCOUNT ON
USE Master
DECLARE \@strSQL varchar(255)
PRINT &#039;Killing Users&#039;
PRINT &#039;-----------------&#039;
CREATE table #tmpUsers(
spid smallint,
ecid smallint,
status varchar(30),
loginname varchar(128),
hostname varchar(128),
blk char(5),
dbname nchar(50),
cmd nchar(16))
INSERT INTO #tmpUsers EXEC SP_WHO
DECLARE LoginCursor CURSOR
READ_ONLY
FOR SELECT spid, dbname FROM #tmpUsers WHERE dbname = &#039;MyDatabase&#039;
DECLARE \@spid varchar(10)
DECLARE \@dbname2 varchar(40)
OPEN LoginCursor
FETCH NEXT FROM LoginCursor INTO \@spid, \@dbname2
WHILE (\@\@fetch_status &lt;&gt; -1)
BEGIN
	IF (\@\@fetch_status &lt;&gt; -2)
	BEGIN
	PRINT &#039;Killing &#039; + \@spid
	SET \@strSQL = &#039;KILL &#039; + \@spid
	EXEC (\@strSQL)
	END
	FETCH NEXT FROM LoginCursor INTO  \@spid, \@dbname2
END
CLOSE LoginCursor
DEALLOCATE LoginCursor
DROP table #tmpUsers
PRINT &#039;Done&#039;
-- The line below is the command to restrict access (behine EM) to dbo (schema owner) and sysadm (sys user):
--exec sp_dboption N\@dbname, N&#039;dbo use&#039;, N&#039;true&#039;
--exec sp_dboption N\@dbname, N&#039;dbo use&#039;, N&#039;false&#039;
EXEC master.dbo.xp_restore_database \@database=&#039;MyDatabase&#039;
  , \@filename = &#039;G:\\$newest&#039;
GO
__SQL__
# Prepare the osql command line options
my $optRef = {
      &#039;-E&#039; =&gt; undef,
      &#039;-n&#039; =&gt; undef,
      &#039;-w&#039; =&gt; &#039;1024&#039;,
      &#039;-d&#039; =&gt; &#039;master&#039;,
      &#039;-l&#039; =&gt; &#039;5&#039;
   };
# Execute the SQL script on the APOLLO named instance on the local server
my $result = dbaRunOsql(&#039;MyServerName&#039;, $sql, $optRef);
if (!defined $result) {
   die &quot;***Err: failed to run osql.exe.&quot;;
}
else {
   print $result;  # Print the results
}
</pre>]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2008/09/22/move-sql-server-backup-file-and-restore-it-with-perl/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Find out total database file sizes on a Sql Server instance</title><link>http://www.haidongji.com/2008/08/07/find-out-total-database-file-sizes-on-a-sql-server-instance/</link> <comments>http://www.haidongji.com/2008/08/07/find-out-total-database-file-sizes-on-a-sql-server-instance/#comments</comments> <pubDate>Thu, 07 Aug 2008 22:03:18 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Perl]]></category> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/2008/08/07/find-out-total-database-file-sizes-on-a-sql-server-instance/</guid> <description><![CDATA[Sometimes you want to know how much space Sql Server databases on an instance are taking from a system. This should be the sum of all data files and log files. Note this is different from how much space actual data is taking. Here is one way to do it. Note it uses sysaltfiles, a [...]]]></description> <content:encoded><![CDATA[<p>Sometimes you want to know how much space Sql Server databases on an instance are taking from a system. This should be the sum of all data files and log files. Note this is different from how much space actual data is taking.</p><p>Here is one way to do it. Note it uses sysaltfiles, a system table that might be deprecated in future releases. For Sql Server 2005, it is recommended that you use sys.master_file:</p><pre class="brush: sql">
select sum(size)*8/1024 as &#039;TotalSizeInMeg&#039;, sum(size)*8/1024/1024 as &#039;TotalSizeInGig&#039; from sysaltfiles
</pre><p>Another way to get that is to use sp_helpdb. Executing sp_helpdb without any parameters will give you total size for each database in megabytes. Adding them up will give you total databases size.</p><p>Since I am digging Perl, below is the Perl script to do that. You might notice the numbers you get from sysaltfiles and sp_helpdb are different, that&#8217;s due to rounding.</p><pre class="brush: perl">
use strict;
use Win32::SqlServer;
# Get server name from the command line
my $ServerName = shift or die &quot;Please enter server name as the first only parameter needed&quot;;
# Create object and login in one step with
# integrated security.
my $sqlsrv = sql_init($ServerName, undef, undef, &#039;master&#039;);
# Our SQL statement. In this case, this is a system stored procedure.
my $stmnt = &lt; &lt;SQLEND;
sp_helpdb
SQLEND
# Run query. The return value is a reference to an array.
my $result = $sqlsrv-&gt;sql_sp($stmnt);
my $TotalSize = 0;
# Print results. Each array entry is a hash reference.
foreach my $row (@$result) {
   print &quot;$$row{name}: $$row{db_size}  \n&quot;;
   if ($$row{db_size} =~ /(\d+\.\d+)/ix) {
	$TotalSize = $TotalSize + $1;
   }
}
print &quot;Total databases size is $TotalSize meg&quot;;
</pre>]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2008/08/07/find-out-total-database-file-sizes-on-a-sql-server-instance/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
