public class MainActivity extends AppCompatActivity {
EditText editText_name,editText_age;
ListView listView;
//操作数据库游标适配器
SimpleCursorAdapter cursorAdapter;
SQLiteDatabase db;
MyDBHelper dbHelper;
Cursor cursor;//游标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText_name = findViewById(R.id.editText_name);
editText_age = findViewById(R.id.editText_age);
listView = findViewById(R.id.listView);
dbHelper = new MyDBHelper(this,"person.db",null,1);
db = dbHelper.getWritableDatabase();//在应用私有目录下建表person.db
//cursor = db.rawQuery("select *from person",null);
cursor = db.query("person",null,null,null,null,null,null);
cursorAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{"name","age"},new int[]{android.R.id.text1,android.R.id.text2},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(cursorAdapter);
}
public void add(View v){
//添加数据
// db.execSQL(“insert into person(name,age)values(?,?)”,new Object[]{editText_name.getText().toString(),editText_age.getText().toString()});
// cursor = db.query(“person”,null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);
// 不用Ssql语句添加数据
ContentValues contentValues = new ContentValues();
contentValues.put("name",editText_name.getText().toString());
contentValues.put("age",editText_age.getText().toString());
db.insert("person",null,contentValues);
//cursor = db.rawQuery("select * from person",null);
cursor = db.query("person",null,null,null,null,null,null);
cursorAdapter.swapCursor(cursor);
clearEdit();
}
public void delete(View v){
//删除数据
// db.execSQL(“delete from person where name = ?”,new String[]{editText_name.getText().toString()});
//
// //cursor = db.rawQuery(“select * from person”,null);
// cursor = db.query(“person”,null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);
//不用sql语句删除数据
db.delete("person","name = ?",new String[]{editText_name.getText().toString()});
//cursor = db.rawQuery("select * from person",null);
cursor = db.query("person",null,null,null,null,null,null);
cursorAdapter.swapCursor(cursor);
clearEdit();
}
public void update(View v){
//更新数据
// db.execSQL(“update person set age = ? where name = ?”,new Object[]{editText_age.getText().toString(),editText_name.getText().toString()});
//
// // cursor = db.rawQuery(“select * from person”,null);
// cursor = db.query(“person”,null,null,null,null,null,null);
// cursorAdapter.swapCursor(cursor);
//不用sql更新数据
ContentValues contentValues = new ContentValues();
contentValues.put("age",editText_age.getText().toString());
db.update("person",contentValues,"name = ?",new String[]{editText_name.getText().toString()});
// cursor = db.rawQuery("select * from person",null);
cursor = db.query("person",null,null,null,null,null,null);
cursorAdapter.swapCursor(cursor);
clearEdit();
}
public void query(View v){
//查询
// cursor = db.query(“person”,null,null,null,null,null,null);
cursor = db.query(“person”,null,”name like ?”,new String[]{“%”+editText_name.getText().toString()+”%”},null,null,null);
cursorAdapter.swapCursor(cursor);
}
public void clearEdit(){
editText_name.setText("");
editText_age.setText("");
editText_name.requestFocus();
}
}
package com.someone.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper{
public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement,name text,age integer )");//建表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}