Sunday, June 28, 2009

Writing SED Script

Testing and Saving Output

Follow this 3 steps

- Makes a copy of the input line.

- Modifies that copy in the pattern space.

- Outputs the copy to standard output

Script and Command

$ sed -f sedscr paulfile

$ sed -f sedscr paulfile > newpaulfile

$ diff paulfile newpaulfile

for x
do
sed -f sedscr $x > tmp.$x
done

Monday, June 22, 2009

Connection of JAVA to Oracle

// You need to import the java.sql package to use JDBC (Java Database Connection)
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.*;
import oracle.jdbc.xa.OracleXid;
import oracle.jdbc.xa.OracleXAException;
import oracle.jdbc.xa.client.*;
import javax.transaction.xa.*;

class XA4
{
public static void main (String args [])
throws SQLException
{

try
{
String URL1 = "jdbc:oracle:oci8:@";
String URL2 ="jdbc:oracle:thin:@(description=(address=(host=dlsun991)
(protocol=tcp)(port=5521))(connect_data=(sid=rdbms2)))";

DriverManager.registerDriver(new OracleDriver());

// You can put a database name after the to the connection URL.
Connection conna =
DriverManager.getConnection (URL1, "ip", "host");

// Prepare a statement to create the table
Statement stmta = conna.createStatement ();

Connection connb =
DriverManager.getConnection (URL2, "user", "pass");

// Prepare a statement to create the table
Statement stmtb = connb.createStatement ();

try
{
// Drop the test table
stmta.execute ("drop table table1");
}
catch (SQLException e)
{
// Ignore an error here
}

try
{
// Create a test table
stmta.execute ("create table my_table (col1 int)");
}
catch (SQLException e)
{
// Ignore an error here too
}

try
{
// Drop the test table
stmtb.execute ("drop table table1");
}
catch (SQLException e)
{
// Ignore error
}

try
{
// Create a test table
stmtb.execute ("create table my_tab (col1 char(30))");
}
catch (SQLException e)
{
// Ignore an error here too
}

// Create XADataSource instances and set properties.
OracleXADataSource oxds1 = new OracleXADataSource();
oxds1.setURL("jdbc:oracle:oci8:@");
oxds1.setUser("scott");
oxds1.setPassword("tiger");

OracleXADataSource oxds2 = new OracleXADataSource();

oxds2.setURL("jdbc:oracle:thin:@(description=(address=(host=dlsun991)
(protocol=tcp)(port=5521))(connect_data=(sid=rdbms2)))");
oxds2.setUser("scott");
oxds2.setPassword("tiger");

// Get XA connections to the underlying data sources
XAConnection pc1 = oxds1.getXAConnection();
XAConnection pc2 = oxds2.getXAConnection();

// Get the physical connections
Connection conn1 = pc1.getConnection();
Connection conn2 = pc2.getConnection();

// Get the XA resources
XAResource oxar1 = pc1.getXAResource();
XAResource oxar2 = pc2.getXAResource();

// Create the Xids With the Same Global Ids
Xid xid1 = createXid(1);
Xid xid2 = createXid(2);

// Start the Resources
oxar1.start (xid1, XAResource.TMNOFLAGS);
oxar2.start (xid2, XAResource.TMNOFLAGS);

// Execute SQL operations with conn1 and conn2
doSomeWork1 (conn1);
doSomeWork2 (conn2);

// END both the branches -- IMPORTANT
oxar1.end(xid1, XAResource.TMSUCCESS);
oxar2.end(xid2, XAResource.TMSUCCESS);

// Prepare the RMs
int prp1 = oxar1.prepare (xid1);
int prp2 = oxar2.prepare (xid2);

System.out.println("Return value prepare 1 is " + prp1);
System.out.println("Return value prepare 2 is " + prp2);

boolean do_commit = true;
}

Thursday, June 18, 2009

BUBBLE SORT IN C++

include
template
void bubbleSort(Iterator first, Iterator last)
{Iterator jp, m;
for (jp = first; jp != last; ++jp)
for (j = first; m < jp; ++m)
if (*jp < *j)
std::iter_swap(i, j);}
{Iterator jp, m;
for (jp = first; m != last; ++i)
for (m = first; m < jp; ++j)
if (compare(*jp, *m))
std::iter_swap(jp, m);}

DOJO Tuorial

DOJO Tutorial

How to call ajax google apps into DOJO script


Code:

//Connect to Goolge Apps
src="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/dojo.xd.js">

dojo.addOnLoad(function(){
setTimeout(function(){
//Connect with Google analytics
dojo.require("dojox.analytics.Urchin")
dojo.addOnLoad(function(){
var tracker = new dojox.analytics.Urchin({
acct: "UA-XXXXXX-X" // your tracking # here
})
})
}, 100)
})

SYMBIAN C++ Tutorial

Symbian C++ in developing GUI APPS

Syntax


RESOURCE {
= ;
[ = ; ...]
}

Here the is defined in files included from some .rh files. For example we can have MENU_BAR, MENU_PANE and so on.

Each can have a scalar value, a list value or a struct value. A scalar value is a number or a string. A list value is a sequence enclosed in braces. Examples:

name = "Hello" ;
list = { 1 , 2, 3 };

A struct value has this syntax:

{
= ;
[ = ; ...]
}

Where can be a scalar, list or structure value, so the structure can nest. Here is a complete (and real) example:

RESOURCE MENU_PANE r_simplemenu_menu_pane
{
items =
{
MENU_ITEM
{
command = ESimpleMenuCmdNewGame;
txt = NEW_GAME_TEXT;
cascade = r_simplemenu_game_submenu_menu_pane;
},
MENU_ITEM
{
command = CmdExit;
txt = EXIT_TEXT;
}
};
}

Creating a MENU in Symbian C++

RESOURCE MENU_BAR r_demo_menubar {
titles = {
MENU_TITLE {
menu_pane = r_demo_menu;
}
};
}

Then the menu pane:

RESOURCE MENU_PANE r_demo_menu {
{
items = {
MENU_ITEM {
command = EHelloWorldCommand1;
txt = COMMAND_ONE;
},
MENU_ITEM {
command = EAknCmdExit;
txt = text_softkey_exit;
}
};
}
}

Note the EHelloWorldCommand1 coming from the .hrh and COMMAND_ONE included from .loc



Creating a LISTBOX

RESOURCE LISTBOX r_list_listbox {
array_id = r_list_items;
flags = EAknListBoxSelectionList;
}
RESOURCE ARRAY r_list_items {
items =
{
LBUF { txt = "tFirst"; },
LBUF { txt = "xtSecond"; },
LBUF { txt = "tThird"; }
};
}

Now the code to create a list object. In the class we declare CAknColumnListBox* iListBox;, then in the HelloAppView::ConstrucL(), we create and load resource is it as follows:

TResourceReader reader;
iEikonEnv->CreateResourceReaderLC(reader, R_LIST_LISTBOX);
iListBox->ConstructFromResourceL(reader);
CleanupStack:PopAndDestroy(); // reader

The following is the code needed to display the listbox in the view.

void CHelloAppView::SizeChanged() {
iListBox->SetExtent (TPoint(0,0), iListBox->MinimumSize());
}
TInt CHelloAppView::CountComponentControls() const {
return 1;
}
CCoeControl* CHelloAppView::ComponentControl(TInt aIndex) const {
switch(aIndex) {
case 0: return iListBox;
default: return NULL;
}
}


#define M_SHOP_REMOVE_PROMPT "Are you sure to remove?"

The resource for a confirmation query is:

RESOURCE DIALOG r_shop_confirmation_query
{
flags=EGeneralQueryFlags;
buttons = R_AVKON_SOFTKEYS_YES_NO;
items =
{
DLG_LINE
{
type = EAknCtQuery;
id = EListDlgCId1;
control = AVKON_CONFIRMATION_QUERY
{
layout = EConfirmationQueryLayout;
label = M_SHOP_REMOVE_PROMPT;
};
}
};
}
RESOURCE TBUF r_shop_data_query_prompt
{
buf = M_SHOP_ADD_PROMPT;
}

Bluetooth GPS(Global Positioning System) device using C++ Mobile on a Symbian Series 60 cellphone

MY CODE

void Cbttest1Container::OniBtDiscovery1DeviceFound( CBase * aDiscovery )
{
_LIT(KDeviceFound, "Device Found");
iEikLabel1->SetTextL(KDeviceFound);
this->DrawDeferred();

CSdpSearchPattern * list = CSdpSearchPattern::NewL( );
list->AddL( 0x1101 );
iBtSdpQuery1->SetSearchPattern(list);
CSdpAttrIdMatchList * matchList = CSdpAttrIdMatchList::NewL( );
matchList->AddL( TAttrRange( KSdpAttrIdProtocolDescriptorList ) );
iBtSdpQuery1->SetAttrIdMatchList(matchList);
iBtSdpQuery1->StartNextSdpL(iBtDiscovery1->BDAddr());
}

PYNET TUTORIAL

MSN Gateway using PYNET


msnp.py is an implementation of the MSN instant messaging protocol in the Python programming language. This pure Python package makes it extremely easy, quick, and efficient to build applications that need to interact with the MSN messaging service.

MY CODE:

import msnp
import time

class MsnListener(msnp.SessionCallbacks):
def state_changed(self, state):
if state == msnp.States.ONLINE:
print 'online kn ^_^!'

msn = msnp.Session(MsnListener())
msn.login('johnpaul86@hotmail.com', 'jpmama')

while True:
msn.process()
time.sleep(1)



IM - Instant Messaging

#using msnp.py - msn protocol

import msnp
import time

class MsnChatListener(msnp.ChatCallbacks):
def message_received(self, passport_id, display_name, text, charset):
print '%s: %s' % (passport_id, text)
self.chat.send_message(text, charset)

class MsnListener(msnp.SessionCallbacks):
def chat_started(self, chat):
callbacks = MsnChatListener()
chat.callbacks = callbacks
callbacks.chat = chat

msn = msnp.Session(MsnListener())
msn.login('johnpaul86@hotmail.com', 'jpmama')

while True:
msn.process(chats = True)
time.sleep(1)

Wednesday, June 17, 2009

CREATING PYTHON SCRIPT WITH MYSQL

mysql> SELECT * FROM name;
+----------+----------+
| name | Lastname |
+----------+----------+
| John Paul| Mam |
| Will | Smith |
| Asyong | Taga |
+----------+----------+
5 rows in set (0.01 sec)

and then consider this short Python script, which connects to the database and prints out the data within the table.

#!/usr/bin/python

# import MySQL module
import MySQLdb

# connect
db = MySQLdb.connect(host="localhost", user="paul", passwd="jpm1986",
db="db56a")

# create a cursor
cursor = db.cursor()

# execute SQL statement
cursor.execute("SELECT * FROM name")

# get the resultset as a tuple
result = cursor.fetchall()

# iterate through resultset
for record in result:
print record[0] , "-->", record[1]


The first step is to import the MySQLdb module, via Python's "import" function.

# import MySQL module
import MySQLdb

You can open up a connection to the MySQL database server, by passing the module's connect() method a series of connection parameters - the server name, the database user name and password, and the database name.

# connect
db = MySQLdb.connect(host="localhost", user="paul", passwd="jpm1986",
db="db1")

A successful connection returns a Connection object, which you can use to create a cursor.

# create a cursor
cursor = db.cursor()

This cursor is needed to execute an SQL statement, and to retrieve the generated resultset.

# execute SQL statement
cursor.execute("SELECT * FROM name")

# get the resultset as a tuple
result = cursor.fetchall()

A number of methods are available to retrieve the SQL resultset - the one used here is the fetchall() method, which returns a tuple of tuples, each inner tuple representing a row of the resultset. This tuple can then be iterated over with a regular "for" loop, and its elements printed to the standard output.

# iterate through resultset
for record in result:
print record[0] , "-->", record[1]

PYTHON SCRIPT

#!/usr/bin/python

# import MySQL module
import MySQLdb

# connect
db = MySQLdb.connect(host="localhost", user="paul", passwd="jpm1986",
db="db56a")

# create a cursor
cursor = db.cursor()

# execute SQL statement
cursor.execute("SELECT * FROM name")

# get the number of rows in the resultset
numrows = int(cursor.rowcount)

# get and display one row at a time
for x in range(0,numrows):
row = cursor.fetchone()
print row[0], "-->", row[1]