Contoh Coding Database Sql Lite Android java contoh kasus table login




public class myDbAdapter {
    myDbHelper myhelper;
     private static Context fContext;
    public myDbAdapter(Context context)
    {
        myhelper = new myDbHelper(context);
    }

    public long insertData(String name, String pass)
    {
        SQLiteDatabase dbb = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        long id;

            contentValues.put(myDbHelper.USERNAME, name);
            contentValues.put(myDbHelper.PASSWORD, pass);
            id = dbb.insert(myDbHelper.TABLE_NAME, null , contentValues);

        return id;
    }



    public int Login(String U,String P) {

        SQLiteDatabase db = myhelper.getReadableDatabase();
        Cursor mCursor =  db.rawQuery( "select * from "+myDbHelper.TABLE_NAME+" where U='"+U+"' and  P='"+P+"'", null );

        return mCursor.getCount();
    }



    private static class myDbHelper extends SQLiteOpenHelper
    {
        private static final String DATABASE_NAME = "DATAKU";    // Database Name        private static final String TABLE_NAME = "DATA";   // Table Name        private static final int DATABASE_Version = 2;    // Database Version        private static final String UID="_id";     // Column I (Primary Key)        private static final String USERNAME = "U";    //Column II        private static final String PASSWORD = "P";    // Column III        private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "                + USERNAME+" VARCHAR(255) ,"                + PASSWORD +" VARCHAR(225))"+

            ";";




        private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;

        private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context=context;
        }

        public void onCreate(SQLiteDatabase db) {

            try {
                db.execSQL(CREATE_TABLE);




            } catch (Exception e) {
                Message.message(context,""+e);
            }
        }

        @Override        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                Message.message(context,"OnUpgrade");
                db.execSQL(DROP_TABLE);
                onCreate(db);
            }catch (Exception e) {
                Message.message(context,""+e);
            }
        }
    }
}

Tidak ada komentar