ThirdActivity
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.tencent.formalandroid.R;
import com.tencent.formalandroid.other.DatabaseHelper;
public class ThirdActivity extends Activity implements View.OnClickListener {
private Button mAddButton;
private Button mDeleteButton;
private Button mUpdateButton;
private Button mQueryButton;
private DatabaseHelper mDatabaseHelper;
private SQLiteDatabase mSqLiteDatabase;
private static final String TAG = ThirdActivity.class.getSimpleName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
initViews();
mDatabaseHelper = new DatabaseHelper(this);
mSqLiteDatabase = mDatabaseHelper.getWritableDatabase();
}
/**
* UI控件
*/
private void initViews() {
mAddButton = findViewById(R.id.button_add);
mAddButton.setOnClickListener(this);
mDeleteButton = findViewById(R.id.button_delete);
mDeleteButton.setOnClickListener(this);
mUpdateButton = findViewById(R.id.button_update);
mUpdateButton.setOnClickListener(this);
mQueryButton = findViewById(R.id.button_query);
mQueryButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_add: {
// TODO: 待优化,需要在子线程中进行IO操作
ContentValues values = new ContentValues();
values.put(mDatabaseHelper.USER_NAME, "老张");
values.put(mDatabaseHelper.AGE, "18岁");
long index = mSqLiteDatabase.insert(mDatabaseHelper.USER_TABLE_NAME, null, values);
if (index != -1) {
Toast.makeText(this, "插入成功", Toast.LENGTH_LONG).show();
}
break;
}
case R.id.button_delete: {
String conditions = "username=?";
String[] args = {"老张"};
int numbers = mSqLiteDatabase.delete(DatabaseHelper.USER_TABLE_NAME, conditions, args);
if (numbers == 0) {
Toast.makeText(this, "没有找到符合条件的", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "找到的个数是: " + numbers, Toast.LENGTH_LONG).show();
}
break;
}
case R.id.button_update: {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.AGE, "20岁");
String conditions = "age=?";
String[] valueStr = {"18岁"};
int affectNum = mSqLiteDatabase.update(DatabaseHelper.USER_TABLE_NAME, contentValues, conditions, valueStr);
if (affectNum == 0) {
Toast.makeText(this, "没有找到符合条件的,没有修改", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "找到的个数是: " + affectNum, Toast.LENGTH_LONG).show();
}
break;
}
case R.id.button_query: {
// TODO: 待优化,需要在子线程中进行IO操作
// 游标:查找的数据集合
Cursor cursor = mSqLiteDatabase.query(mDatabaseHelper.USER_TABLE_NAME, new String[] {DatabaseHelper.USER_NAME}, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int count = cursor.getCount();
for (int i = 0; i < count; i++) {
String userName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.USER_NAME));
Log.i(TAG, i + " --> " + userName);
}
}
break;
}
}
}
DatabaseHelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String USER_TABLE_NAME = "user";
public static final String USER_NAME = "username";
public static final String AGE = "age";
public static final String DATABASE_NAME = "database.db";
public static final int VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + USER_TABLE_NAME + "(" + USER_NAME + " varchar(20) not null," + AGE + " varchar(10) not null" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
网友评论