如何在android

时间:2016-12-22 19:07:05

标签: java android sqlite

这是我的代码     Databasehandler.java

       package com.example.mybucky.myapplication;

      import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.prefs.Preferences;

/**
 * Created by Satyajeet Sen on 20/12/2016.
 */

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "Register.db" ;
    private static final int DATABASE_VERSION = 1 ;
    private static final String TABLE_REGISTER = "REGISTER";
    private static final String COL_ID = "id";
    private static final String COL_NAME = "name";
    private static final String COL_USERNAME = "username";
    private static final String COL_PASSWORD = "password";
    private static final String COL_AGE = "age";
    SQLiteDatabase db;


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //3rd argument to be passed is CursorFactory instance
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_REGISTER_TABLE = "CREATE TABLE " + TABLE_REGISTER + "("
                + COL_ID + " INTEGER PRIMARY KEY ," + COL_NAME + " TEXT ,"
                + COL_USERNAME + " TEXT ," + COL_PASSWORD + " TEXT ," + COL_AGE + " INTEGER "+")";
        db.execSQL(CREATE_REGISTER_TABLE);
        this.db=db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGISTER);

        // Create tables again
        this.onCreate(db);
    }

    public void insertDetails(LoginRegisterBean l){
        db=this.getWritableDatabase();
        ContentValues values = new ContentValues();
        String query ="SELECT * FROM "+TABLE_REGISTER;
        Cursor cursor =db.rawQuery(query,null);
        int count=cursor.getCount();

        values.put(COL_ID,count);
        // Contact Name
        values.put(COL_NAME, l.getName());
        values.put(COL_USERNAME, l.getUsername());
        values.put(COL_PASSWORD, l.getPassword());
        values.put(COL_AGE, l.getAge());

        db.insert(TABLE_REGISTER,null,values);
        db.close();
      }

    public String searchPass(String uname,String pass){
              uname=COL_USERNAME;
              pass=COL_PASSWORD;
        db=this.getReadableDatabase();

        String query=("SELECT " +uname+ "," +pass+ " from " + TABLE_REGISTER +"");


        Cursor cursor = db.rawQuery(query,null);
        String a,b ;
        b="not found";
        if(cursor.moveToFirst()){

            do{
                a=cursor.getString(0);
                b=cursor.getString(1);

                if(a.equals(COL_USERNAME)){

                    b=cursor.getString(1);
                    break;
                }
            }
            while(cursor.moveToNext());
        }


        return b;
      }

    public String searchUser(String uname,String pass){
        uname=COL_USERNAME;
        pass=COL_PASSWORD;
        db=this.getReadableDatabase();

        String query=("SELECT " +uname+ "," +pass+ " from " + TABLE_REGISTER +"");


        Cursor cursor = db.rawQuery(query,null);
        String a,b ;
        a="not found";
        if(cursor.moveToFirst()){

            do{
                a=cursor.getString(0);
                b=cursor.getString(1);

                if(a.equals(COL_USERNAME)){

                    b=cursor.getString(1);
                    break;
                }
            }
            while(cursor.moveToNext());
        }


        return a;
    }

    LoginRegisterBean getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_REGISTER, new String[] { COL_ID,
                        COL_USERNAME,COL_AGE }, COL_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        LoginRegisterBean retrievecontact = new LoginRegisterBean(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), Integer.parseInt(cursor.getString(2)));
        // return contact
        return retrievecontact;
    }
       }


        UserAreaActivity.java   2edit texts one for  age and other for username. Aim:-to      display values of age nd username from database to edittext

      package com.example.mybucky.myapplication;

     import android.os.Bundle;
     import android.support.v7.app.AppCompatActivity;
     import android.widget.EditText;
     import android.widget.TextView;

     import com.example.mybucky.myapplication.R;

     import java.util.ArrayList;
     import java.util.List;

     public class UserAreaActivity extends AppCompatActivity {

    DatabaseHandler helper = new DatabaseHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);


        final EditText etUsername = (EditText) findViewById(R.id.username);
        final EditText etAge = (EditText) findViewById(R.id.age);
        final TextView tvwelcome = (TextView) findViewById(R.id.tvwelcome);

        public void retrievefromdb(LoginRegisterBean l){
            List<LoginRegisterBean> lst =new ArrayList<LoginRegisterBean>();
            for(int i=0;i<3;i++){
               lst.add( helper.getContact(i));
            }

    }


       }
     }

我有一个方法在Databasehandler类中返回单个联系人,但我不需要显示密码。在活动UserAreaActivity中仅显示字段的年龄和用户名。

欢迎用户!您的年龄是 年龄 ---编辑领域------ 用户名 ---编辑字段-----

0 个答案:

没有答案