Last active
August 29, 2015 14:13
-
-
Save msomu/7dbfc11808f92859dcc7 to your computer and use it in GitHub Desktop.
Insert into Database
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); | |
| } | |
| } | |
| } |
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; | |
| } | |
| 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