Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Android SQLite - small I/O difficulty
- public class DBExampleActivity extends Activity implements OnClickListener{
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // Declare the database object and open.
- MyDBManager db = new MyDBManager(this);
- db.open();
- // Take names from EditTexts and store in string.
- first_name = fname.getText().toString();
- last_name = lname.getText().toString();
- }
- @Override
- public void onClick(View view) {
- // In the onclick method, add values to database, call next activity
- db.insertData(first_name, last_name);
- Intent intent = new Intent(this, Display.class);
- startActivity(intent);
- }}
- public class Display extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Cursor myCursor = db.getDetails();
- // Get relevant data from columns in cursor.
- username = c.getString(c.getColumnIndex("first_name"));
- password = c.getString(c.getColumnIndex("last_name"));
- // set textview values.
- fname.setText(first_name);
- lname.setText(last_name);
- }}
- public class MyDBManager {
- public static final String KEY_ROWID = "id";
- public static final String KEY_USERNAME = "firstname";
- public static final String KEY_PASSWORD = "lastname";
- private static final String DATABASE_NAME = "User_Information";
- private static final String DATABASE_TABLE = "User_name";
- private static final int DATABASE_VERSION = 1;
- private static final String DATABASE_CREATE =
- "create table " + DATABASE_TABLE +
- " (_id integer primary key autoincrement, " +
- "username text not null, " +
- "password text not null); " ;
- private final Context context;
- private DatabaseHelper DBHelper;
- private SQLiteDatabase db;
- public MyDBManager(Context context){
- this.context = context;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper{
- DatabaseHelper(Context context){
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- // Creates the database
- @Override
- public void onCreate(SQLiteDatabase db){
- db.execSQL(DATABASE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
- // on triggered if the database version has increased.
- // used if you want to change structure of column: eg add new column.
- }
- } // end helper class
- // Methods used to access and change data in DB:
- public MyDBManager open() throws SQLException{
- db = DBHelper.getWritableDatabase();
- return this;
- }
- public void close(){
- DBHelper.close();
- }
- public long insertData(String username, String password){
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_USERNAME, username);
- initialValues.put(KEY_PASSWORD, password);
- return db.insert(DATABASE_TABLE, null, initialValues);
- }
- public Cursor getDetails(long rowId){
- Cursor c = db.query(true, DATABASE_TABLE, new String[]{
- KEY_ROWID,
- KEY_USERNAME,
- KEY_PASSWORD},
- KEY_ROWID + "=" + rowId,
- null,
- null,
- null,
- null,
- null);
- if(c != null){
- c.moveToFirst();
- }
- return c;
- }}// End myDBManager Class.
- TextView fname = (TextView) findViewById(R.id.mytextview1);
- TextView lname = (TextView) findViewById(R.id.mytextview2);
- // set textview values.
- fname.setText(first_name);
- lname.setText(last_name);
Add Comment
Please, Sign In to add comment