Android SQLite demo 之增、删、改、查
最终效果动画
MainActivity
package com.example.shen.sqlitetest;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.shen.sqlitetest.entity.User;
import java.util.ArrayList;
public class MainActivity extends Activity {
private SQLiteDatabase db;
private EditText etUserName,etPassword;
private ArrayList<User> alUser;
private ListView lvUser;
private UserAdapter adapter;
private TextView tvPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideSoftKeyboard();
initView();
initData();
}
private void initView(){
DatabaseHelper databaseHelper=new DatabaseHelper(MainActivity.this);
db=null;
db=databaseHelper.getReadableDatabase();
etUserName=(EditText) findViewById(R.id.et_username);
etPassword=(EditText) findViewById(R.id.et_password);
lvUser=(ListView) findViewById(R.id.lv_user);
alUser=new ArrayList<>();
adapter=new UserAdapter(this,alUser);
lvUser.setAdapter(adapter);
lvUser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
etUserName.setText(alUser.get(position).username);
etUserName.setSelection(etUserName.length());
etPassword.setText(alUser.get(position).password);
}
});
//新增
findViewById(R.id.bt_insert).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etUserName.getText().toString().isEmpty() || etPassword.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this,getString(R.string.can_not_be_empty),Toast.LENGTH_SHORT).show();
} else {
String sql = "insert into user(username,password) values ('" + etUserName.getText().toString().trim() + "','" + etPassword.getText().toString().trim() + "')";
db.execSQL(sql);
initData();
}
}
});
//删除
findViewById(R.id.bt_delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// String sql="delete from user where username='"+etUserName.getText().toString().trim()+"'";
// db.execSQL(sql);
String whereClause="username=?";
String[] whereArgs={etUserName.getText().toString().trim()};
db.delete("user",whereClause,whereArgs);
initData();
}
});
//修改
findViewById(R.id.bt_update).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sql="update user set password='"+etPassword.getText().toString().trim()+"' where username='"+etUserName.getText().toString().trim()+"'";
db.execSQL(sql);
initData();
}
});
}
//查询
private void initData(){
alUser.clear();
Cursor cursor = db.query("user",null,null,null,null,null,null);//查询并获得游标
while ((cursor.moveToNext())){
User user=new User();
user.username=cursor.getString(cursor.getColumnIndex("username"));
user.password=cursor.getString(cursor.getColumnIndex("password"));
alUser.add(user);
}
adapter.notifyDataSetChanged();
etUserName.setText("");
etUserName.requestFocus();
etPassword.setText("");
}
//隐藏软键盘
public void hideSoftKeyboard(){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
DatabaseHelper
package com.example.shen.sqlitetest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by SHEN on 2015/12/16.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb.db"; //数据库名称
private static final int version = 1; //数据库版本
public DatabaseHelper(Context context) {
super(context,DB_NAME,null,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table user(username varchar(20) not null , password varchar(60) not null );";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
SQLite数据库默认存储路径
data/data/<package_name>/databases/
SQLite Developer查看
SQLite demo下载地址:http://download.csdn.net/download/shenyuanqing/9366970
SQLite Developer下载地址:http://download.csdn.net/download/shenyuanqing/9366246