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());
}