Results 1 to 2 of 2
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    274
    Rep Power
    3

    Default any other Way for : oncreate save data from server database to SQLite at 1st launch ?

    Hi friends!

    i am trying to "save data from server database to SQLite " while first time launching of my application..,

    is their any way to do that...,

    Thank you for your Guide..,
    Have A Happy Day..,
    - Raj

  2. #2
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    274
    Rep Power
    3

    Default Re: any other Way for : oncreate save data from server database to SQLite at 1st laun

    Hi anyone can guide me..,

    i am trying to do the above concept. But it making problem..,

    this is my database helper class

    Java Code:
    package com.raj.serrendlist;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class SqliteDbHelper extends SQLiteOpenHelper {
    	private static final String DB_name = "cp";
    	private static final int DB_version = 1;
    	private static final String DB_table = "abr2";
    	private static final String db_key_id = "id";
    	private static final String db_abr = "AbbVarCode";
    	private static final String db_msg	 = "AbbVarMsg";
    	String DB_PATH="/data/data/com.raj.serrendlist/databases/";
    
    	public SqliteDbHelper(Context context) {
    		super(context, DB_name, null, DB_version);
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	public void onCreate(SQLiteDatabase db) {
    		// TODO Auto-generated method stub
    		db.execSQL(" CREATE TABLE " + DB_table + " (" +
    				db_key_id + "INTEGER PRIMARY KEY, " +
    				db_abr + " TEXT NOT NULL, " +
    				db_msg + " TEXT NOT NULL);");
    	}
    
    	@Override
    	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    		// TODO Auto-generated method stub
    		db.execSQL("DROP TABLE IF EXISTS " + DB_table);
    		onCreate(db);
    	}
    
    	public void addAbr(AdrDbModel abrm) {
    		SQLiteDatabase db = this.getWritableDatabase();
    		ContentValues val = new ContentValues();
    		val.put(db_abr, abrm.getAbr());
    		val.put(db_msg, abrm.getMsg());
    		db.insert(DB_table, null, val);
    		db.close();
    	}
    
    	public List<AdrDbModel> getAllAbr()
    	{
    		List<AdrDbModel> AbrList=new ArrayList<AdrDbModel>();
    		String selectQuery = "SELECT  * FROM " + DB_table;
    		SQLiteDatabase db=this.getWritableDatabase();
    		Cursor cursor=db.rawQuery(selectQuery, null);
    		if(cursor.moveToFirst())
    		do
    		{
    			AdrDbModel abrm = new AdrDbModel();
    			abrm.setId(Integer.parseInt(cursor.getString(0)));
    			abrm.setAbr(cursor.getString(1));
    			abrm.setMsg(cursor.getString(2));
    			AbrList.add(abrm);
    		}
    		while(cursor.moveToNext());
    		return AbrList;
    		
    	}
    	public boolean checkDataBase(){
    		 
        	SQLiteDatabase checkDB = null;
     
        	try{
        		String myPath = DB_PATH + DB_name;
        		checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
     
        	}catch(SQLiteException e){
     
        		//database does't exist yet.
     
        	}
     
        	if(checkDB != null){
     
        		checkDB.close();
     
        	}
     
        	return checkDB != null ? true : false;
        }
    }

    this is my bean class

    Java Code:
    package com.raj.serrendlist;
    
    public class AdrDbModel {
    	int id;
    	String abr = "abbreviation";
    	String msg = "definition";
    	private boolean selectred;
    
    	public AdrDbModel() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public AdrDbModel(int id, String abr, String msg, boolean selectred) {
    		super();
    		this.id = id;
    		this.abr = abr;
    		this.msg = msg;
    		this.selectred = selectred;
    	}
    
    	public AdrDbModel(String abr, String msg, boolean selectred) {
    		super();
    		this.abr = abr;
    		this.msg = msg;
    		this.selectred = selectred;
    	}
    
    	public boolean isSelectred() {
    		return selectred;
    	}
    
    	public void setSelectred(boolean selectred) {
    		this.selectred = selectred;
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getAbr() {
    		return abr;
    	}
    
    	public void setAbr(String abr) {
    		this.abr = abr;
    	}
    
    	public String getMsg() {
    		return msg;
    	}
    
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    
    }
    My main class

    Java Code:
    package com.raj.serrendlist;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import com.raj.mylist.AlertDialogManager;
    import com.raj.mylist.ConnectionDetector;
    import com.raj.mylist.JSONParser;
    import com.raj.mylist.ListofServiceRend;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends ListActivity implements OnClickListener,
    		OnItemClickListener{
    	ConnectionDetector cd;
    
    	AlertDialogManager alert = new AlertDialogManager();
    
    	private ProgressDialog pDialog;
    
    	JSONParser jsonParser = new JSONParser();
    
    	ArrayList<HashMap<String, String>> tracksList;
    	SqliteDbHelper sdbh;
    
    	// AbrManager a_manager;
    
    	private List<AdrDbModel> ckmod;
    
    	public static final String PREF_NAME = "mycklist";
    	private CallReptAdapter adaptr = null;
    	Button sltChk;
    	CheckBox cb;
    	TextView abmsg;
    	String abr, msg;
    	private static final String Tag_url = "http://androidcrm.urspaly.com/abbreviations.php";
    	private static final String Tag_AbbVarCode = "AbbVarCode";
    	private static final String Tag_AbbVarMsg = "AbbVarMsg";
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		cb = (CheckBox) findViewById(R.id.checkBox1);
    		sltChk = (Button) findViewById(R.id.chechedlist);
    		abmsg = (TextView) findViewById(R.id.textView2);
    		sltChk.setOnClickListener(this);
    		sdbh = new SqliteDbHelper(this);
    		// a_manager = new AbrManager(getApplicationContext());
    		// Toast.makeText(getApplicationContext(),"Abr_Ststus "
    		// +a_manager.getStatus(), Toast.LENGTH_SHORT).show();
    		checkExtDB();
    		
    	}
    	public void checkExtDB()
    	{
    
    		if(sdbh.checkDataBase())
    		{
    			//fillData();
    		}
    		else
    		{
    			cd = new ConnectionDetector(getApplicationContext());
    			if (!cd.isConnectingToInternet()) {
    				alert.showAlertDialog(MainActivity.this,
    						"Internet Connection Error",
    						"Please connect to working Internet connection", false);
    				return;
    			}
    			tracksList = new ArrayList<HashMap<String, String>>();
    			ckmod = new ArrayList<AdrDbModel>();
    
    			new LoadSrChkList().execute();
    		}
    	}
    
    	private void fillData(String messsage, String ckbr, boolean isSelected) {
    		final AdrDbModel model = new AdrDbModel();
    		//List<AdrDbModel> abrm = sdbh.getAllAbr();//
    		model.setMsg(messsage);
    		Log.d("messsage", messsage);
    		model.setSelectred(isSelected);
    		model.setAbr(ckbr);
    		Log.d("ckbr", ckbr);
    		ckmod.add(model);
    		sdbh.addAbr(model);
    	}
    
    	class LoadSrChkList extends AsyncTask<String, String, String> {
    
    		@Override
    		protected void onPreExecute() {
    			// TODO Auto-generated method stub
    			super.onPreExecute();
    			pDialog = new ProgressDialog(MainActivity.this);
    			pDialog.setMessage("Please Wait...");
    			pDialog.setIndeterminate(false);
    			pDialog.setCancelable(false);
    			pDialog.show();
    		}
    
    		@Override
    		protected String doInBackground(String... arg0) {
    			// TODO Auto-generated method stub
    			List<NameValuePair> params = new ArrayList<NameValuePair>();
    			String json = jsonParser.makeHttpRequest(Tag_url, "GET", params);
    			Log.d("Chk List", json);
    			try {
    				JSONArray jObj = new JSONArray(json);
    				// DatabaseHandler db = new
    				// DatabaseHandler(getApplicationContext());
    				//ab=new AdrDbModel();
    				if (jObj != null) {
    					for (int i = 0; i < jObj.length(); i++) {
    						JSONObject c = jObj.getJSONObject(i);
    						String abrr = c.getString(Tag_AbbVarCode);
    						String msgg = c.getString(Tag_AbbVarMsg);
    						Log.d("tag_abr", abrr);
    						Log.d("tag_msg", msgg);
    						/*ab.setAbr(abrr);
    						ab.setMsg(msgg);*/
    						//sdbh.addAbr(new AdrDbModel(abrr, msgg));
    						//db.addAbr(abrr, msgg);
    						fillData(msgg, abrr, false);
    						Log.d("tak", "stop");
    					}
    //					fillData();
    				}
    
    			} catch (JSONException d) {
    				d.printStackTrace();
    			}
    
    			return null;
    		}
    
    		@Override
    		protected void onPostExecute(String result) {
    			// TODO Auto-generated method stub
    			pDialog.dismiss();
    			runOnUiThread(new Runnable() {
    				public void run() {
    					adaptr = new CallReptAdapter(MainActivity.this,
    							R.layout.serrenchkbx, ckmod);
    
    					setListAdapter(adaptr);
    				}
    			});
    		}
    	}
    
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		StringBuffer responseText = new StringBuffer();
    		List<AdrDbModel> stateList = sdbh.getAllAbr();//adaptr.crmodel;
    		for (AdrDbModel employeeModel : stateList)
    
    			if (employeeModel.isSelectred()) {
    				responseText.append("\n" + employeeModel.getMsg());
    			}
    		String rl = responseText.toString();
    		// a_manager.createSession(rl);
    		ListofServiceRend lrend = new ListofServiceRend();
    		lrend.setSr(rl);
    		Toast.makeText(getApplicationContext(), "getted val " + rl,
    				Toast.LENGTH_LONG).show();
    		// finish();
    	}
    
    	@Override
    	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    		// TODO Auto-generated method stub
    
    	}
    }
    while the main class is created it check whether the Database is exist / not ..,

    then it create the data base as blow shown. But the i got the value from the server database as JSON but unable store on the local database..,

    any other Way for : oncreate save data from server database to SQLite at 1st launch ?-test.png

    Anyone can guide me friends..,

    Thank You Have A Happy Day..,
    - Raj

Similar Threads

  1. Replies: 2
    Last Post: 01-22-2013, 02:37 PM
  2. Embedded Database : SQLite or Derby Database
    By vishnubrett in forum NetBeans
    Replies: 3
    Last Post: 03-26-2012, 10:50 AM
  3. SQLite database at FTP server
    By Matko in forum JDBC
    Replies: 11
    Last Post: 02-14-2012, 09:42 AM
  4. Replies: 1
    Last Post: 02-13-2012, 12:45 PM
  5. Replies: 1
    Last Post: 12-18-2009, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •