Skip to content

Instantly share code, notes, and snippets.

@msomu
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save msomu/82dd8245e7a3a332fba5 to your computer and use it in GitHub Desktop.

Select an option

Save msomu/82dd8245e7a3a332fba5 to your computer and use it in GitHub Desktop.
save data and process querry
package com.soms.mytestingapplications;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
SomsDatabaseAdapter somsDatabaseAdapter;
EditText userName,passWord,name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
userName = (EditText) findViewById(R.id.edittext_username);
passWord = (EditText) findViewById(R.id.edittext_password);
name = (EditText) findViewById(R.id.edittext_name);
somsDatabaseAdapter = new SomsDatabaseAdapter(this);
}
public void addUser(View view){
String user = userName.getText().toString();
String pass = passWord.getText().toString();
long id = somsDatabaseAdapter.insertData(user,pass);
if(id<0){
Message.message(this,"Un Successfull");
}else{
Message.message(this,"Successfull inserted a row, row number "+id);
}
}
public void viewDetails(View view){
String data = somsDatabaseAdapter.getAllData();
Message.message(this,data);
}
public void getDetails(View view){
String s1=name.getText().toString();
//somu abc
String sub1=s1.substring(0,s1.indexOf(" "));
String sub2=s1.substring(s1.indexOf(" ")+1);
String s3=somsDatabaseAdapter.getDAta(sub1,sub2);
Message.message(this,s3);
}
public void update(View view){
somsDatabaseAdapter.updateName("somu","muthu");
}
public void delete(View view){
int count =somsDatabaseAdapter.deleteRow();
Message.message(this,""+count);
}
}
package com.soms.mytestingapplications;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by msomu on 14/1/15.
*/
public class SomsDatabaseAdapter{
SomsHelper helper;
public SomsDatabaseAdapter(Context context){
helper= new SomsHelper(context);
}
public long insertData(String name,String password){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SomsHelper.NAME, name);
contentValues.put(SomsHelper.PASSWORD, password);
long id =db.insert(SomsHelper.TABLE_NAME,null,contentValues);
return id;
}
public String getAllData(){
SQLiteDatabase db = helper.getWritableDatabase();
//select -id,NAME,Password from SomuHelper
String[] columns={SomsHelper.UID, SomsHelper.NAME, SomsHelper.PASSWORD};
Cursor cursor = db.query(SomsHelper.TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext()){
//int index1 = cursor.getColumnIndex(SomsHelper.UID);
int cid = cursor.getInt(0);
String name = cursor.getString(1);
String password = cursor.getString(2);
buffer.append(cid+" "+name+" "+password+"\n");
}
return buffer.toString();
}
public String getDAta(String name,String password){
//select _id from somutable where name=? and password=?
SQLiteDatabase db = helper.getWritableDatabase();
//select -id,NAME,Password from SomuHelper
String[] columns={SomsHelper.UID};
String[] selectionArgs={name,password};
Cursor cursor = db.query(SomsHelper.TABLE_NAME, columns, SomsHelper.NAME+ " =? AND "+SomsHelper.PASSWORD+" =?", selectionArgs, null, null, null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext()){
int index0 = cursor.getColumnIndex(SomsHelper.UID);
int personId = cursor.getInt(index0);
buffer.append(personId+"\n");
}
return buffer.toString();
/*//select name ,password,from SomuTable where name="somu";
SQLiteDatabase db = helper.getWritableDatabase();
//select -id,NAME,Password from SomuHelper
String[] columns={SomsHelper.NAME, SomsHelper.PASSWORD};
Cursor cursor = db.query(SomsHelper.TABLE_NAME, columns, SomsHelper.NAME+" = '"+name+"'", null, null, null, null);
StringBuffer buffer = new StringBuffer();
while(cursor.moveToNext()){
//int index1 = cursor.getColumnIndex(SomsHelper.UID);
int index1 = cursor.getColumnIndex(SomsHelper.NAME);
int index2 = cursor.getColumnIndex(SomsHelper.PASSWORD);
String personName = cursor.getString(index1);
String password = cursor.getString(index2);
buffer.append(" "+name+" "+password+"\n");
}
return buffer.toString(); */
}
public int updateName(String oldName,String newName){
//update SOMUTABLE set name='muthu' where Name='somu'
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SomsHelper.NAME,newName);
String[] whereArgs={oldName};
int count = db.update(SomsHelper.TABLE_NAME,contentValues,SomsHelper.NAME+" =? ",whereArgs);
return count;
}
public int deleteRow (){
//DElete * from somutable where Name='somu'
SQLiteDatabase db = helper.getWritableDatabase();
String[] whereArgs={"somu"};
int count =db.delete(SomsHelper.TABLE_NAME,SomsHelper.NAME+" =?",whereArgs);
return count;
}
static class SomsHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "somsdatabase";
private static final String TABLE_NAME = "SOMSTABLE";
private static final String UID = "_id";
private static final String NAME = "Name";
private static final String PASSWORD = "Password";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+PASSWORD+" VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;
private static final int DATABASE_VERSION = 7;
private Context context;
public SomsHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context,"constructor called");
}
@Override
public void onCreate(SQLiteDatabase db) {
//first time called when db is created
//CREATE TABLE SOMSTABLE (_id INTEGER PRIMARYKEY AUTOINCREMENT, Name VARCHAR(255));
try {
db.execSQL(CREATE_TABLE);
Message.message(context,"OnCreate called");
} catch (SQLException e) {
Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context,"OnUpgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Message.message(context,""+e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment