标签:button instance onupgrade vat str 删除 table class cal
<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="查询"/>
package net.bwie.localdata;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import net.bwie.localdata.sqlite.DBHelper;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
protected Button mInsertBtn;
protected Button mDeleteBtn;
protected Button mUpdateBtn;
protected Button mQueryBtn;
private DBHelper mHelper;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
initView();
mHelper = new DBHelper(this);
mDatabase = mHelper.getWritableDatabase();
}
private void initView() {
mInsertBtn = (Button) findViewById(R.id.insert_btn);
mInsertBtn.setOnClickListener(MainActivity.this);
mDeleteBtn = (Button) findViewById(R.id.delete_btn);
mDeleteBtn.setOnClickListener(MainActivity.this);
mUpdateBtn = (Button) findViewById(R.id.update_btn);
mUpdateBtn.setOnClickListener(MainActivity.this);
mQueryBtn = (Button) findViewById(R.id.query_btn);
mQueryBtn.setOnClickListener(MainActivity.this);
}
@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();
}
}
// 表名
// null。数据库如果插入的数据为null,会引起数据库不稳定。为了防止崩溃,需要传入第二个参数要求的对象
// 如果插入的数据不为null,没有必要传入第二个参数避免崩溃,所以为null
// 插入的数据
private void insertData() {
ContentValues values = new ContentValues();
values.put(DBHelper.NAME, "鹿晗");
values.put(DBHelper.AGE, 17);
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);
Log.d("1507", "name: " + name + ", age: " + age);
}
}
}
package net.bwie.localdata.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
// 当数据库文件创建时,执行初始化操作,并且只执行一次
@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) {
}
}
标签:button instance onupgrade vat str 删除 table class cal
原文地址:https://www.cnblogs.com/hyhy904/p/10995900.html