SQLite

SQLite

为什么要用SQLite

  • 当数据量大、结构复杂,使用ShareFreferences和文件存储相当复杂,容易出错,效率低下。而使用SQLite就比较简单。

什么是SQLite

  • SQLite是专为嵌入式设备设计的一款轻量级数据库,SQLite数据库存储是Android系统提供的数据存储方式之一。

如何创建数据库和数据表

  • 新建类继承SQLiteOpenHelper;
    实现构造方法,重写onCreate、onUpgrade方法;
    实例化SQLiteOpenHelper的子类对象;
    调用getReadableDatabase或getWritableDatabase方法
    以下代码展示:
    创建类继承SQLiteOpenHelper,并写出数据库
public class Sql extends SQLiteOpenHelper {
    String sql = "create table student(" + "id integer primary Key autoincrement not null," + "name text," + "age integer" + ")";

    public Sql(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

.在xml文件中布局

 id="@+id/sqltest_ed"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    id="@+id/sqltest_ed2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    

SQLActivity文件代码

public class SqltestActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "SqltestActivity";
    EditText sql_ed, sql_ed2;
    Button sql_bt, sql_bt2, sql_bt3, sql_bt4;

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

    }

    private void deleteStudent() {
        String name = sql_ed.getText().toString();
        String age = sql_ed2.getText().toString();
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        sqLiteDatabase.delete("student", "name=?", new String[]{name});
    }

    private void modifyStudnt() {
        String name = sql_ed.getText().toString();
        String age = sql_ed2.getText().toString();
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "FFF");
        sqLiteDatabase.update("student", values, "name=? and age=?", new String[]{name, age + ""});
    }

    private void liststudents() {
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("student", null, null, null, null, null, null);
        cursor.moveToFirst();
        do {
            String name = cursor.getString(2);
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            Log.e(TAG, "liststudents: :" + id + "-" + name + "-" + age);
        } while (cursor.moveToNext());


    }

    private void addstudent() {
        String name = sql_ed.getText().toString();
        int age = Integer.parseInt(sql_ed2.getText().toString());

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("age", age);
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        sqLiteDatabase.insert("student", null, values);
    }

    private void binID() {
        sql_ed = findViewById(R.id.sqltest_ed);
        sql_ed2 = findViewById(R.id.sqltest_ed2);
        sql_bt = findViewById(R.id.sqltest_bt);
        sql_bt2 = findViewById(R.id.sqltest_bt2);
        sql_bt3 = findViewById(R.id.sqltest_ed3);
        sql_bt4 = findViewById(R.id.sqltest_ed4);
        sql_bt.setOnClickListener(this);
        sql_bt2.setOnClickListener(this);
        sql_bt3.setOnClickListener(this);
        sql_bt4.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sqltest_bt:
                addstudent();
                break;
            case R.id.sqltest_bt2:
                liststudents();
                break;
            case R.id.sqltest_ed3:
                modifyStudnt();
                break;
            case R.id.sqltest_ed4:
                deleteStudent();
                break;
        }
    }


}

如何添加数据

  private void addstudent() {
        String name = sql_ed.getText().toString();
        int age = Integer.parseInt(sql_ed2.getText().toString());

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("age", age);
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        sqLiteDatabase.insert("student", null, values);
    }

如何删除数据

 private void deleteStudent() {
        String name = sql_ed.getText().toString();
        String age = sql_ed2.getText().toString();
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        sqLiteDatabase.delete("student", "name=?", new String[]{name});
    }

如何修改数据

 private void modifyStudnt() {
        String name = sql_ed.getText().toString();
        String age = sql_ed2.getText().toString();
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", "FFF");
        sqLiteDatabase.update("student", values, "name=? and age=?", new String[]{name, age + ""});
    }

如何查询数据

private void liststudents() {
        Sql sql = new Sql(this, "student_db", null, 1);
        SQLiteDatabase sqLiteDatabase = sql.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("student", null, null, null, null, null, null);
        cursor.moveToFirst();
        do {
            String name = cursor.getString(2);
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            int age = cursor.getInt(cursor.getColumnIndex("age"));
            Log.e(TAG, "liststudents: :" + id + "-" + name + "-" + age);
        } while (cursor.moveToNext());


    }

你可能感兴趣的:(实训,Android)