首页 > 解决方案 > 如何创建一个 SQ Lite db helper 类来创建一个简单的表

问题描述

我需要使用 SQ Lite 创建一个数据库。请帮帮我。这是正常的文字。2. 也是这样,但现在跟随一个代码块:

    Skip a line and indent eight spaces.
    That's four spaces for the list
    and four to trigger the code block.

标签: java

解决方案


我没有得到你真正想要的东西,但这是我的代码示例:)

String TableName = "UserInfo";
public static final String col1 = UserProfile.users._ID;
public static final String col2 = UserProfile.users._UserName;
public static final String col3 = UserProfile.users._DOB;
public static final String col4 = UserProfile.users._Gender;
public static final String col5 = UserProfile.users._Password;

public DBHelper(Context context) {
    super(context, "database", null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TableName + " ("+col1+" integer primary key autoincrement, "+col2+" text,"+col3+ " text,"+col4+" text,"+col5+" text)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("drop table if exists "+ TableName);
    onCreate(db);
}

public boolean addInfo(String name,String dob, String gender,String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(col2,name);
    cv.put(col3,dob);
    cv.put(col4,gender);
    cv.put(col5,password);
    int i =(int) db.insert(TableName,null,cv);
    db.close();
    if(i>0){
        return  true;
    }else{
        return false;
    }
}

public boolean updateInfor(int id,String name,String dob, String gender,String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(col2,name);
    cv.put(col3,dob);
    cv.put(col4,gender);
    cv.put(col5,password);
    int i = db.update(TableName,cv,col1+"=\""+ id + "\"",null);
    db.close();
    if(i>0){
        return  true;
    }else{
        return false;
    }
}

public Cursor readAllinfor(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cs = db.rawQuery("select * from "+TableName+"",null);
    db.close();
    return  cs;
}

public Cursor readAllinfor(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cs = db.rawQuery("select * from "+TableName+" where "+col1+"=\""+id+"\"",null);
    db.close();
    return  cs;
}

public boolean deleteInfo(int id){
    SQLiteDatabase db = this.getWritableDatabase();
    int i= db.delete(TableName,col1+"=\""+id+"\"",null);
    db.close();
    if(i>0){
        return  true;
    }else{
        return false;
    }

公共类 EditProfile 扩展 AppCompatActivity {

private EditText name;
private EditText dob;
private EditText pass;
private RadioGroup gender;
private Button update;
private Button search;
private Button delete;
private RadioButton male;
private RadioButton female;
DBHelper db;


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

    name = (EditText) findViewById(R.id.username);
    dob = (EditText) findViewById(R.id.dob);
    pass = (EditText) findViewById(R.id.pass);
    gender = (RadioGroup) findViewById(R.id.gender);
    search = (Button) findViewById(R.id.search);
    update = (Button) findViewById(R.id.edit);
    delete = (Button) findViewById(R.id.delete);
    male= (RadioButton) findViewById(R.id.Male);
    female =(RadioButton) findViewById(R.id.Female);


    final String radiobuttenid = String.valueOf(gender.getCheckedRadioButtonId());


    db = new DBHelper(this);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Cursor cs = db.readAllInfo(name.getText().toString());
            int i =cs.getCount();
            if(i<=0){
                Toast.makeText(EditProfile.this,"User not Found !",Toast.LENGTH_LONG).show();
            }
            while (cs.moveToNext()) {
                dob.setText(cs.getString(2));
                if(cs.getString(3).toString().matches("Male")){
                    male.setChecked(true);
                }else{
                    female.setChecked(true);
                }
                pass.setText(cs.getString(4));
            }
        }
    });


    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (db.updateInfo(name.getText().toString(), dob.getText().toString(), radiobuttenid, pass.getText().toString())) {
                Toast.makeText(EditProfile.this, "Data Updated", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(EditProfile.this, "Data Not Updated", Toast.LENGTH_LONG).show();
            }
        }
    });

    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (db.deleteInfo(name.getText().toString())) {
                Toast.makeText(EditProfile.this, "Data Deleted", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(EditProfile.this, "Data Not Deleted", Toast.LENGTH_LONG).show();
            }
        }
    });

    db.close();
}

推荐阅读