Tuesday, November 6, 2007

Connecting to Sqlserver from linux platform

Phew this was really tough job for me.But i accomplised it after much hardships.Thanks to my sysadmin who had no knowledge about it and i did his job.

Below are the stepd to get this job done

first an d for all u require unixodbc installed on ur pc.This can be found at http://www.unixodbc.org/

download it and get it installed

then download freetds from www.freetds.org

and install it as below
./configure --prefix=/usr/local/freetds --sysconfdir=/usr/local/ freetds --with-tdsver=7.0 --with-unixodbc=/usr/local

make

make install

then open vim /usr/local/freetds/freetds.conf
then put ur pc details where u have sql server installed in that file
[rohit2k]
host = 192.156.78.9
port =1433
tds version = 7.0
here [rohit2k] is the pc name

then test the connection by executing the below command

tsql –S ‘rohit2k’ –U ‘sa’ –P ‘password’

if u see a 1> then ur connection is done.

The FreeTDS ODBC driver should be installed in /usr/local/freetds/lib - look for libtdsodbc.so.

Configuration

Registering the ODBC Driver With unixODBC

unixODBC needs to know about all ODBC drivers you intend to use. The best way to accomplished this is using the ODBCConfig graphical program which comes with unixODBC. An alternative method is to use the 'odbcinst' command which also comes with unixODBC. We will focus on using the odbcinst command. Create a file named tds.driver.template with a few lines describing the driver.
[FreeTDS]
Description = v0.63 with protocol v8.0
Driver = /usr/local/freetds/lib/libtdsodbc.so
tds.driver.template

Execute odbcinst, telling it to install a driver entry using the tds.driver.template file. Note that you must leave a space between the '-f' switch and the template file name.

# odbcinst -i -d -f tds.driver.template

register ODBC driver

Creating an ODBC Data Source Name

ODBC client applications will typically work with ODBC Data Source Names (DSN). The best way to create, edit and remove a DSN is to use the ODBCConfig tool. Again we will use the odbcinst command instead. We will do this because the ODBCConfig program is fairly self explanatory and because not all users will have the unixODBC GUI tools installed. Again, we start by creating a template file - this one is called tds.datasource.template and contains some options such as the default database and UID. Note; you will want to use your own Server address.
[MSSQLTestServer]
Driver = FreeTDS
Description = Northwind sample database
Trace = No
Server = 192.168.1.25
Port = 1433
Database = Northwind
tds.datasource.template

Valid attributes for use in odbc.ini (or the connection string) can be found at: http://www.freetds.org/userguide/odbcconnattr.htm
[SybaseTestServer]
Driver = FreeTDS
Description = Test Sybase Database with FreeTDS
Trace = No
Server = 192.168.1.25
Port = 5050
TDS Version = 5.0
Database = testdb
tds.datasource.template

Note: Do NOT use "Servername" in an ODBC-only configuration! Use of "Server" should be preferred over "Address" (see http://lists.ibiblio.org/pipermail/freetds/2004q2/016086.html)

Note; we have executed previous commands as root (denoted by leading '#' character on given commands) but here we execute the command as a regular user. This is significant. All users of the system share FreeTDS and the ODBC Drivers but each user has his/her own list of DSN's (view odbcinst output for help on registering as a system DSN available to all users). So create the DSN as the user who is going to be using it.

$ odbcinst -i -s -f tds.datasource.template

create ODBC data source

Now, to make this work with PHP is very easy. Assuming you have a package manager, download and install the odbc extension for PHP if it isn't already installed. If you don't have a package manager, you will have to compile odbc.so. To enable odbc in PHP, just modify the php.ini file, usually located in /etc. Add this line anywhere in the file (convention dictates that it should be placed with the other extension calls, so search your file for the word "extension").

extension = odbc.so

php.ini

Now, reload apache as root to make the changes effective.

# apachectl graceful

reloading apache as root

Test

unixODBC comes with a variety of tools which allow you to test. We will use the command line tool 'isql'. isql allows us to submit commands (typically SQL statements) to the DSN and see the results.

$ isql -v MSSQLTestServer
SQL>

test using isql
You should see a connected message and an SQL prompt. If this fails then you may have a configuration problem or you may simply be using the incorrect UserName and PWD.

Now try a simple SQL statement.

SQL> SELECT contactname FROM customers

simple sql statement

hey its finally over.hope ur serach for this problem ends here

Monday, November 5, 2007

Perl file upload

hey guys

it has been long since i have posted any code online.Got a bit busy with my work @ my office.But people i am back and wanted to do something even better this time .so i thought i should open a blog where all my codes can reside.i have done lot of file uploading in php and then i bumped into a project where i was required to do file uploading in perl.Long back my freind was trying to do the same and he told me that its tough to do so.but i never looked into it at that time and now i started writing the code for the same with a small fear in mind(Thanks to my freind) that the code will take a toll on my grey cells.but it didnot.Its so simple.
Here it goes

here is the html code for file upload
<form action="upload.cgi" method="post" enctype="multipart/form-data" onsubmit="return checkfile();">

<table align="center" border="0" cellpadding="0" cellspacing="0">
<tbody><tr><td align="center"><b>File upload by rohit d'souza</b></td></tr>
<tr><td align="center">
</td></tr>
<tr><td>
</td></tr>
<tr><td>
</td></tr>
<tr><td align="center">
File to Upload: <input name="filerohit" type="file">


<input name="Submit" value="Submit Form" type="submit">
</td>
</tr>
<tr><td>
</td></tr>
<tr><td>
</td></tr>
</tbody></table>
</form>

here checkfile is a javascript function to check if the user has actaully selected a file before submitting the form.

function checkfile()
{

if(document.forms[0].filerohit.value=="")
{
alert("please select a file to upload");
return false;

}


}


here is the server side code in upload.cgi
#!/usr/bin/perl


use CGI;
use strict;
my $query = new CGI;
my $upload_dir="/opt/myuploads";
my $filename = $query->param("filerohit");
$filename =~ s/.*[\/\\](.*)/$1/;
my $upload_filehandle = $query->upload("filerohit");

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;


and now u can do anything u want with the file which is at the location $upload_dir/$filename

Thats all.hope this code helps u.u could send me ur feedbacks at rajdsouza@yahoo.com