Monday 18 June 2012

MySql Database Connectivity in android

For Mysql database connection we have to use PHP  script on server side code:-

PHP Code:-


<?php
      
     /**
      *  IMPORTANT: Please before execution of this file run SQL.sql on your MySQL server.
      *  Example database will be made with simple table and some data
      */                 
     $table =htmlspecialchars($_GET["table"]);
$rowcount =htmlspecialchars($_GET["count"]);
$rowname= htmlspecialchars($_GET["row"]);
$condition= htmlspecialchars($_GET["con"]);
 
     // Require MySQL class file
     require_once "classes/mysql.class.php";
     
     // As explained in article, connection info is needed
     // in order to object connects server
     // Server name is in 95% cases localhost, but only if 
     // script is ran localy on server. For remote access contact your
     // host provider.
     $data["Server"] = "server name";
     // This is database user. As i used WampServer to develope this example
     // default user, set during instalation is 'root'. If you use other type of server
     // please check it's doucmentation on make new user and grant him all privileges
     $data["User"] = "user ";
     // By default, password is not set on WampServer, If you use other server,
     // please check it's doucmentation on make new user with all privileges, and custom password
     $data["Password"] = "password";
     
     // Here is MySQL object being instantated and $data array is being
     // injected into clas constructor
     $mysql = new MySQL( $data );
     // This method is used to connect onto server, and as database name is being injected throuh
     // method parameter, database will be selected if database name is valid
     $mysql -> connect("database name");
     // Simple MySQL SELECT query is being executed with this method
     // MYSQL query string is passed through parameter
//echo("SELECT ".$rowname." FROM ".$table."  ".$condition);
     $mysql -> query("SELECT ".$rowname." FROM ".$table."  ".$condition);
     $in =1;
     // This while loop, outputs all data that is fethced with query
     while( $data = $mysql -> fetchArray() ){
     for($i=0;$i<$rowcount;$i++)
 {
           echo $data[$i] . "#";
 }
 echo "||";
  
     }
     
     // Use this method to disconnect from database server
     $mysql -> disconnect();
     // You may output all errors that occured during process
     // Use of this method is adviced only during development period
     //echo $mysql -> getErrors(). "<br />";
?>
Android Code For access code in android:-



public class FatchData {


public Vector<String[]> GetSelect(String Table,String Rows,String Condition,int Count)
{
Vector<String[]> v=new Vector<String[]>();
String[] str;
StringTokenizer st1 = new StringTokenizer(GET_SELECT_DATA(Table,Rows,Condition,Count), "||");
int count=0;
try{
while(st1.hasMoreTokens()){
String str1=st1.nextToken();
str=new String[Count];
StringTokenizer st2 = new StringTokenizer(str1, "#");
// while(st1.hasMoreTokens()){
for(int i=0;i<Count;i++)
{
String str2=st2.nextToken();
System.out.println(str2);
str[i]=str2;
}
// }


}
}
catch(Exception e){}

return v;
}

public String GET_SELECT_DATA(String Table,String Rows,String Condition,int Count)
{


try{
Document doc = Jsoup.connect("http://192.168.1.12:80/select.php?table="+Table+"&count="+Count+"&row="+Rows+"&con="+Condition).get();
System.out.println("jj"+doc.text());
return doc.text();



} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}






No comments:

Post a Comment