Last active
August 29, 2015 14:13
-
-
Save msomu/a957e0511c4739bc074e to your computer and use it in GitHub Desktop.
Database save user and retrive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyActivity extends ActionBarActivity { | |
| SomsDatabaseAdapter somsDatabaseAdapter; | |
| EditText userName,passWord; | |
| @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); | |
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| 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