mysql - Display database value in android app, how? -


i working on small application android, built mysql database , want display table values on screen.is there tutorial or template this?

how can it..

i have following .java concerning database:

databahsehander.java

    package com.minilotto.library;      import java.util.hashmap;      import android.content.contentvalues;     import android.content.context;     import android.database.cursor;     import android.database.sqlite.sqlitedatabase;     import android.database.sqlite.sqliteopenhelper;      public class databasehandler extends sqliteopenhelper {  // static variables // database version private static final int database_version = 1;  // database name private static final string database_name = "android_api";  // login table name private static final string table_login = "login"; private static final string table_session = "session";  // login table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_email = "email"; private static final string key_uid = "uid"; private static final string key_created_at = "created_at"; private static final string key_sid = "sid"; private static final string key_sname = "sname";     private static final string key_smax = "smax"; private static final string key_sbet = "sbet";  public databasehandler(context context) {     super(context, database_name, null, database_version); }  // creating tables @override public void oncreate(sqlitedatabase db) {     string create_login_table = "create table " + table_login + "("             + key_id + " integer primary key,"             + key_name + " text,"             + key_email + " text unique,"             + key_uid + " text,"             + key_created_at + " text" + ")";     db.execsql(create_login_table);      string create_session_table = "create table " + table_session + "("             + key_sid + " integer primary key,"             + key_sname + " text,"             + key_smax + " text,"             + key_sbet + " text,"             + key_created_at + " text" + ")";     db.execsql(create_session_table); }    // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     // drop older table if existed     db.execsql("drop table if exists " + table_login);     db.execsql("drop table if exists " + table_session);     // create tables again     oncreate(db); }  /**  * storing session details in database  * */ public void addsession(string sname, string smax, string sbet, string created_at) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(key_sname, sname); // name     values.put(key_smax, smax); // email     values.put(key_sbet, sbet); // email     values.put(key_created_at, created_at); // created @      // inserting row     db.insert(table_session, null, values);     db.close(); // closing database connection }    /**  * storing user details in database  * */ public void adduser(string name, string email, string uid, string created_at) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(key_name, name); // name     values.put(key_email, email); // email     values.put(key_uid, uid); // email     values.put(key_created_at, created_at); // created @      // inserting row     db.insert(table_login, null, values);     db.close(); // closing database connection }  /**  * getting user data database  * */ public hashmap<string, string> getuserdetails(){     hashmap<string,string> user = new hashmap<string,string>();     string selectquery = "select  * " + table_login;      sqlitedatabase db = this.getreadabledatabase();     cursor cursor = db.rawquery(selectquery, null);     // move first row     cursor.movetofirst();     if(cursor.getcount() > 0){         user.put("name", cursor.getstring(1));         user.put("email", cursor.getstring(2));         user.put("uid", cursor.getstring(3));         user.put("created_at", cursor.getstring(4));     }     cursor.close();     db.close();     // return user     return user; }  /**  * getting user login status  * return true if rows there in table  * */ public int getrowcount() {     string countquery = "select  * " + table_login;     sqlitedatabase db = this.getreadabledatabase();     cursor cursor = db.rawquery(countquery, null);     int rowcount = cursor.getcount();     db.close();     cursor.close();      // return row count     return rowcount; }  /**  * re crate database  * delete tables , create them again  * */ public void resettables(){     sqlitedatabase db = this.getwritabledatabase();     // delete rows     db.delete(table_login, null, null);     db.close(); }     } 

userfunctions.java

package com.minilotto.library;  import java.util.arraylist; import java.util.list;  import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonobject;  import android.content.context;  public class userfunctions {  private jsonparser jsonparser;  // testing in localhost using wamp or xampp  // use http://10.0.2.2/ connect localhost ie http://localhost/ private static string loginurl = "http://androidprojekt.esy.es/android_login_api/"; private static string registerurl = "http://androidprojekt.esy.es/android_login_api/"; private static string sessionurl = "http://androidprojekt.esy.es/android_login_api/";  private static string login_tag = "login"; private static string register_tag = "register"; private static string session_tag = "start_session";  // constructor public userfunctions(){     jsonparser = new jsonparser(); }  public jsonobject opensession(string sname, string smax, string sbet){     // building parameters     list<namevaluepair> params = new arraylist<namevaluepair>();     params.add(new basicnamevaluepair("tag", session_tag));     params.add(new basicnamevaluepair("sname", sname));     params.add(new basicnamevaluepair("smax", smax));     params.add(new basicnamevaluepair("sbet", sbet));     jsonobject json = jsonparser.getjsonfromurl(sessionurl, params);     // return json     // log.e("json", json.tostring());     return json; }  /**  * function make login request  * @param email  * @param password  * */ public jsonobject loginuser(string email, string password){     // building parameters     list<namevaluepair> params = new arraylist<namevaluepair>();     params.add(new basicnamevaluepair("tag", login_tag));     params.add(new basicnamevaluepair("email", email));     params.add(new basicnamevaluepair("password", password));     jsonobject json = jsonparser.getjsonfromurl(loginurl, params);     // return json     // log.e("json", json.tostring());     return json; }  /**  * function make login request  * @param name  * @param email  * @param password  * */ public jsonobject registeruser(string name, string email, string password){     // building parameters     list<namevaluepair> params = new arraylist<namevaluepair>();     params.add(new basicnamevaluepair("tag", register_tag));     params.add(new basicnamevaluepair("name", name));     params.add(new basicnamevaluepair("email", email));     params.add(new basicnamevaluepair("password", password));      // getting json object     jsonobject json = jsonparser.getjsonfromurl(registerurl, params);     // return json     return json; }  /**  * function login status  * */ public boolean isuserloggedin(context context){     databasehandler db = new databasehandler(context);     int count = db.getrowcount();     if(count > 0){         // user logged in         return true;     }     return false; }  /**  * function logout user  * reset database  * */ public boolean logoutuser(context context){     databasehandler db = new databasehandler(context);     db.resettables();     return true; }  /**  * function end session  * reset database  * */ public boolean logoutsession(context context){     databasehandler db = new databasehandler(context);     db.resettables();     return true; }     } 

index.php on server

<?php        /**        * file handle api requests        * accepts , post        *         * each request identified tag        * response json data          /**        * check post request         */       if (isset($_post['tag']) && $_post['tag'] != '') { // tag $tag = $_post['tag'];  // include db handler require_once 'include/db_functions.php'; $db = new db_functions();  // response array $response = array("tag" => $tag, "success" => 0, "error" => 0);  // check tag type if ($tag == 'login') {     // request type check login     $email = $_post['email'];     $password = $_post['password'];      // check user     $user = $db->getuserbyemailandpassword($email, $password);     if ($user != false) {         // user found         // echo json success = 1         $response["success"] = 1;         $response["uid"] = $user["unique_id"];         $response["user"]["name"] = $user["name"];         $response["user"]["email"] = $user["email"];         $response["user"]["created_at"] = $user["created_at"];         $response["user"]["updated_at"] = $user["updated_at"];         echo json_encode($response);     } else {         // user not found         // echo json error = 1         $response["error"] = 1;         $response["error_msg"] = "incorrect email or password!";         echo json_encode($response);     } }  else if ($tag == 'register') {     // request type register new user     $name = $_post['name'];     $email = $_post['email'];     $password = $_post['password'];      // check if user existed     if ($db->isuserexisted($sname)) {         // user existed - error response         $response["error"] = 2;         $response["error_msg"] = "user existed";         echo json_encode($response);     } else {         // store user         $user = $db->storeuser($name, $email, $password);         if ($user) {             // user stored             $response["success"] = 1;             $response["uid"] = $user["unique_id"];             $response["user"]["name"] = $user["name"];             $response["user"]["email"] = $user["email"];             $response["user"]["created_at"] = $user["created_at"];             $response["user"]["updated_at"] = $user["updated_at"];             echo json_encode($response);         } else {             // user failed store             $response["error"] = 1;             $response["error_msg"] = "error occured in registartion";             echo json_encode($response);         }     }     }  else if  ($tag == 'start_session') {     // request type register new session     $sname = $_post['sname'];     $smax = $_post['smax'];     $sbet = $_post['sbet'];      // check if user existed     if ($db->issessionexisted($sname)) {         // user existed - error response         $response["error"] = 3;         $response["error_msg"] = "session existed";         echo json_encode($response);     } else {         // store user         $session = $db->storesession($sname, $smax, $sbet);         if ($session=!false) {             // user stored             $response["success"] = 1;             $response["sid"] = $session["unique_id"];             $response["session"]["sname"] = $session["sname"];             $response["session"]["smax"] = $session["smax"];             $response["session"]["sbet"] = $session["sbet"];             $response["session"]["created_at"] = $session["created_at"];             $response["session"]["updated_at"] = $session["updated_at"];             echo json_encode($response);         } else {             // user failed store             $response["error"] = 1;             $response["error_msg"] = "session error occured in registartion";             echo json_encode($response);           }     } } else {     echo "invalid request"; }       } else { echo "access denied";       }       ?> 

db_functions.php

<?php  class db_functions {  private $db;  //put code here // constructor function __construct() {     require_once 'db_connect.php';     // connecting database     $this->db = new db_connect();     $this->db->connect(); }  // destructor function __destruct() {  }  /**  * storing new user  * returns user details  */ public function storeuser($name, $email, $password) {     $uuid = uniqid('', true);     $hash = $this->hashssha($password);     $encrypted_password = $hash["encrypted"]; // encrypted password     $salt = $hash["salt"]; // salt     $result = mysql_query("insert users(unique_id, name, email, encrypted_password, salt, created_at) values('$uuid', '$name', '$email', '$encrypted_password', '$salt', now())");     // check successful store     if ($result) {         // user details          $uid = mysql_insert_id(); // last inserted id         $result = mysql_query("select * users uid = $uid");         // return user details         return mysql_fetch_array($result);     } else {         return false;     } }       /**  * storing new session  * returns session details  */ public function storesession($sname, $smax, $sbet) {     $usid = uniqid('', true);     $result = mysql_query("insert session(unique_sid, sname, smax, sbet, created_at) values('$usid', '$sname', '$smax', '$sbet', now())");     // check successful store     if ($result) {         // user details          $sid = mysql_insert_id(); // last inserted id         $result = mysql_query("select * session uid = $sid");         // return user details         return mysql_fetch_array($result);     } else {         return false;     } }  /**  * user email , password  */ public function getuserbyemailandpassword($email, $password) {     $result = mysql_query("select * users email = '$email'") or die(mysql_error());     // check result      $no_of_rows = mysql_num_rows($result);     if ($no_of_rows > 0) {         $result = mysql_fetch_array($result);         $salt = $result['salt'];         $encrypted_password = $result['encrypted_password'];         $hash = $this->checkhashssha($salt, $password);         // check password equality         if ($encrypted_password == $hash) {             // user authentication details correct             return $result;         }     } else {         // user not found         return false;     } }  /**  * check user existed or not  */ public function isuserexisted($email) {     $result = mysql_query("select email users email = '$email'");     $no_of_rows = mysql_num_rows($result);     if ($no_of_rows > 0) {         // user existed          return true;     } else {         // user not existed         return false;     } }   /**  * check session existed or not  */ public function issessionexisted($sname) {     $result = mysql_query("select sname session sname = '$sname'");     $no_of_rows = mysql_num_rows($result);     if ($no_of_rows > 0) {         // user existed          return true;     } else {         // user not existed         return false;     } }  /**  * encrypting password  * @param password  * returns salt , encrypted password  */ public function hashssha($password) {      $salt = sha1(rand());     $salt = substr($salt, 0, 10);     $encrypted = base64_encode(sha1($password . $salt, true) . $salt);     $hash = array("salt" => $salt, "encrypted" => $encrypted);     return $hash; }  /**  * decrypting password  * @param salt, password  * returns hash string  */ public function checkhashssha($salt, $password) {      $hash = base64_encode(sha1($password . $salt, true) . $salt);      return $hash; } 

}

?>

i have table following rows: unique_sid, sname, smax, sbet, created_at

i want show on application values sname database - have add in code can show sname in .xml

yes there first should decide how want display data in application, mean want show them in list-view or grid-view or show them text on text-view.

here simple 1 helps fetch data database , show them application visit here

hope you


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -