布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.samsung.android.mydb.MainActivity">
<EditText
android:id="@+id/EditName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="鹿晗"/>
<EditText
android:id="@+id/EditAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="17"/>
<Button
android:id="@+id/insert_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入"/>
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
<Button
android:id="@+id/update_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:id="@+id/query_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>
</LinearLayout>
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "my_database.db";
public static final String TABLE_NAME = "t_person";
public static final int DB_VERSION = 1;
public static final String NAME = "name";
public static final String AGE = "age";
DBHelper(Context c) {
super(c, DB_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " +
TABLE_NAME +
"(_id integer primary key autoincrement, " +
NAME + " varchar, " +
AGE + " varchar"
+ ")";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText name, age;
private Button b1;
private DBHelper mHelper;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.EditName);
age = (EditText) findViewById(R.id.EditAge);
findViewById(R.id.insert_btn).setOnClickListener(MainActivity.this);
findViewById(R.id.delete_btn).setOnClickListener(MainActivity.this);
findViewById(R.id.query_btn).setOnClickListener(MainActivity.this);
findViewById(R.id.update_btn).setOnClickListener(MainActivity.this);
mHelper = new DBHelper(this);
mDatabase = mHelper.getWritableDatabase();
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.insert_btn) {
insertData();
} else if (view.getId() == R.id.delete_btn) {
deleteData();
} else if (view.getId() == R.id.update_btn) {
updateData();
} else if (view.getId() == R.id.query_btn) {
queryData();
}
}
private void insertData() {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, name.getText().toString());
values.put(DBHelper.AGE, age.getText().toString());
mDatabase.insert(DBHelper.TABLE_NAME, null, values);
Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
}
private void deleteData() {
int count = mDatabase.delete(DBHelper.TABLE_NAME, DBHelper.NAME + " = ?", new String[]{"鹿晗"});
Toast.makeText(this, "删除数量:"+count, Toast.LENGTH_SHORT).show();
}
private void updateData() {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, "小茗同学");
values.put(DBHelper.AGE, 18);
int count = mDatabase
.update(DBHelper.TABLE_NAME, values, DBHelper.NAME + " = ?", new String[]{"鹿晗"});
Toast.makeText(this, "修改成功:" + count, Toast.LENGTH_SHORT).show();
}
private void queryData() {
Cursor cursor = mDatabase.query(DBHelper.TABLE_NAME,
new String[]{DBHelper.NAME, DBHelper.AGE},
DBHelper.AGE + " > ?",
new String[]{"16"},
null,
null,
DBHelper.AGE + " desc");// 注意空格!
int nameIndex = cursor.getColumnIndex(DBHelper.NAME);
int ageIndex = cursor.getColumnIndex(DBHelper.AGE);
while (cursor.moveToNext()) {
String name = cursor.getString(nameIndex);
String age = cursor.getString(ageIndex);
String prompt="name: " + name + ", age: " + age;
Toast.makeText(this, prompt, Toast.LENGTH_SHORT).show();
}
}
}