注:这是WHUT 计算机学院 移动终端应用开发课程、实验2:SQLite数据库的使用 的文章。
源码下载地址:https://download.csdn.net/download/cxh_1231/10805430
纸上得来终觉浅,绝知此事要躬行。
public class BaseDBHelper extends SQLiteOpenHelper {
private static final String CREATE_T_MEMO = “create table t_memo(”
+“id integer primary key autoincrement,”
+“memo_time text,”
+“memo_content text)”;
private Context context;
public BaseDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_T_MEMO); //创建数据库表t_memo
}
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
...
BaseDBHelper dbHelper = new BaseDBHelper(MainActivity.this, "firstdemo_db", null, 1);
SQLiteDatabase db = dbHelper.getWritableDatabase(); //创建数据库firstdemo_db
...
ContentValues values = new ContentValues();
values.put("memo_time", time);
values.put("memo_content", content);
long f = db.insert("t_memo", null, values); //向表中插入数据
Cursor cursor = db.query("t_memo", null, null, null, null, null, null); //从数据库表t_memo中读取所有数据
ContentValues values = new ContentValues();
values.put("memo_time", tv_time.getText().toString());
values.put("memo_content", et_content.getText().toString());
int is_succeeed = db.update("t_memo", values, "id = ? ", new String[]{memo_id}); //修改指定编号的记录
int is_succeed = db.delete("t_memo", "id = ? ", new String[]{memo_id}); //从表t_memo中删除指定编号的记录
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView> parent, View view, int position, long id){
... //事件处理代码
}
@Override
public void onNothingSelected(AdapterView> parent){ }
});
package com.cxhit.test2;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final String CREATE_T_USER = "create table t_user("
+"id integer primary key autoincrement,"
+"user_Name text,"
+"user_Password text,"
+"user_Tel text,"
+"user_Email text,"
+"user_Sex int)";
private Context context;
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_T_USER);//创建数据表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.cxhit.test2;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.*;
//调用
import android.widget.Toast;
import android.widget.Spinner;
import android.content.ContentValues;
import java.util.ArrayList;
import java.util.List;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static SQLiteDatabase database;//数据库对象
private List userList; //用户列表
//
private EditText getName,getPassword,getTel,getEmail;
private int getSex;
//
private Button addButton,saveButton,deleteButton,emptyButton;
private RadioGroup sexRadio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database=getDatabase();//获得数据库对象
//四个按钮的对象
addButton=(Button) findViewById(R.id.buttonAdd);
saveButton = (Button) findViewById(R.id.buttonSave);
deleteButton = (Button) findViewById(R.id.buttonDelete);
emptyButton = (Button) findViewById(R.id.buttonEmpty);
//性别选项
sexRadio = (RadioGroup) findViewById(R.id.radioSex);
Spinner spinner = (Spinner) findViewById(R.id.spinnerNowUser);
final List datas = new ArrayList<>();
datas.add("zhangsan");
datas.add("lisi");
datas.add("wangwu");
MyAdapter adapter = new MyAdapter(this);
spinner.setAdapter(adapter);
adapter.setDatas(datas);
}
//为“添加”按钮点击点击事件
public void buttonAdd_Click(View view){
getName = (EditText) findViewById(R.id.editTextUsername);
getPassword = (EditText) findViewById(R.id.editTextPassword);
getTel = (EditText) findViewById(R.id.editTextTel);
getEmail = (EditText) findViewById(R.id.editTextEmail);
getSex = sexRadio.getCheckedRadioButtonId();
if(TextUtils.isEmpty(getName.getText().toString())){
Toast.makeText(MainActivity.this,"请输入用户名!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getPassword.getText().toString())){
Toast.makeText(MainActivity.this,"请输入密码!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getTel.getText().toString())){
Toast.makeText(MainActivity.this,"请输入手机号!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getEmail.getText().toString())){
Toast.makeText(MainActivity.this,"请输入邮箱号码!",Toast.LENGTH_LONG).show();
}
else if(getSex<0){
Toast.makeText(MainActivity.this,"请选择性别!",Toast.LENGTH_LONG).show();
}else{
//将数据保存到数据库
ContentValues values = new ContentValues();
values.put("user_Name",getName.getText().toString());
values.put("user_Password",getPassword.getText().toString());
values.put("user_Tel",getTel.getText().toString());
values.put("user_Email",getEmail.getText().toString());
values.put("user_Sex",getSex);
long flag = database.insert("t_user", null, values);
if (flag > 0) {
Toast.makeText(MainActivity.this, "添加成功!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "添加失败!", Toast.LENGTH_LONG).show();
}
}
}
public void buttonSave_Click(View view){
getName = (EditText) findViewById(R.id.editTextUsername);
getPassword = (EditText) findViewById(R.id.editTextPassword);
getTel = (EditText) findViewById(R.id.editTextTel);
getEmail = (EditText) findViewById(R.id.editTextEmail);
getSex = sexRadio.getCheckedRadioButtonId();
if(TextUtils.isEmpty(getName.getText().toString())){
Toast.makeText(MainActivity.this,"请输入用户名!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getPassword.getText().toString())){
Toast.makeText(MainActivity.this,"请输入密码!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getTel.getText().toString())){
Toast.makeText(MainActivity.this,"请输入手机号!",Toast.LENGTH_LONG).show();
}
else if(TextUtils.isEmpty(getEmail.getText().toString())){
Toast.makeText(MainActivity.this,"请输入邮箱号码!",Toast.LENGTH_LONG).show();
}
else if(getSex<0){
Toast.makeText(MainActivity.this,"请选择性别!",Toast.LENGTH_LONG).show();
}else{
//将数据保存到数据库
ContentValues values = new ContentValues();
values.put("user_Name",getName.getText().toString());
values.put("user_Password",getPassword.getText().toString());
values.put("user_Tel",getTel.getText().toString());
values.put("user_Email",getEmail.getText().toString());
values.put("user_Sex",getSex);
int is_succeeed=database.update("t_user",values,"user_Name=?",new String[]{getName.getText().toString()});
if (is_succeeed > 0) {//如果要保存的用户在数据库中
Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();
} else {//如果不在数据库中,改为添加操作
long flag = database.insert("t_user", null, values);
Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();}
}
}
public void buttonDelete_Click(View view){
getName = (EditText) findViewById(R.id.editTextUsername);
int is_succeed=database.delete("t_user","user_Name=?",new String[]{getName.getText().toString()});
if(is_succeed>0)
Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_LONG).show();
}
public void buttonEmpty_Click(View view){
getName = (EditText) findViewById(R.id.editTextUsername);
getPassword = (EditText) findViewById(R.id.editTextPassword);
getTel = (EditText) findViewById(R.id.editTextTel);
getEmail = (EditText) findViewById(R.id.editTextEmail);
getSex = sexRadio.getCheckedRadioButtonId();
getName.setText("");
getPassword.setText("");
getEmail.setText("");
getTel.setText("");
Toast.makeText(MainActivity.this,"清空成功",Toast.LENGTH_LONG).show();
}
//获得数据库对象
private SQLiteDatabase getDatabase() {
DBHelper dbHelper = new DBHelper(MainActivity.this, "demo_db", null, 1);
return dbHelper.getReadableDatabase();
}
}
“添加”按钮、“保存”按钮功能基本shix实现,删除按钮未实现。仅供参考!!!
纸上得来终觉浅,绝知此事要躬行。