package helloworld.dongnaoedu.com.imoocsqlitedemo; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Environment; import android.support.annotation.IdRes; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.RadioGroup; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText nameEdt,ageEdt,idEdt; private RadioGroup genderGp; private String genderStr = "男"; private SQLiteDatabase db; private ListView stuList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //添加操作 //参数:环境上下文;数据库名称(如果只有一个数据库名称,那么这个数据库的位置会是在私有目录data中); // 游标工厂,通过什么游标去操作数据,传null给我一个默认选择;版本号 //如果带SD卡路径,那么数据库位置则在指定的路径下 //~ String path = Environment.getExternalStorageDirectory() + "/stu.db"; SQLiteOpenHelper helper = new SQLiteOpenHelper(this,path,null,1) { @Override public void onCreate(SQLiteDatabase db) { //创建 Toast.makeText(MainActivity.this,"数据库创建",Toast.LENGTH_SHORT).show(); //如果数据库不存在,则会调用onCreate方法,那么我们可以将表的创建工作放在这里完成 String sql = "create table info_db (_id integer primary key autoincrement," + "name varchar(20)," + "age integer," + "gender varhcar(4))"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //升级 Toast.makeText(MainActivity.this,"数据库升级",Toast.LENGTH_SHORT).show(); } }; //1、数据库存在则直接打开数据库;(所谓的打开指的是获得数据库对象) //2、数据库不存在则创建再打开, //3、数据库存在但版本号升高,则调用升级方法。 db = helper.getReadableDatabase();//用于获取数据库对象 nameEdt = (EditText) findViewById(R.id.name_edt); ageEdt = (EditText) findViewById(R.id.age_edt); idEdt = (EditText) findViewById(R.id.id_edt); genderGp = (RadioGroup) findViewById(R.id.gender_gp); genderGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { if(checkedId == R.id.male) {genderStr = "男";} else{ genderStr = "女";} } }); stuList = (ListView) findViewById(R.id.stu_list); } public void operate(View v){ String nameStr = nameEdt.getText().toString(); String ageStr = ageEdt.getText().toString(); String idStr = idEdt.getText().toString(); switch (v.getId()){ case R.id.insert_btn: //db.rawQuery(); 查询 select * from 表名 //db.execSQL(); 添加,删除,修改,查找 //String sql = "insert into info_db(name,age,gender) values('张',12,'男')"; String sql = "insert into info_db (name,age,gender) values(?,?,?)"; db.execSQL(sql,new String[]{nameStr,ageStr,genderStr}); break; case R.id.select_btn: // db.rawQuery(sql) //select * from 表名 where _id = ? String sql2 = "select * from info_db"; if(!idStr.equals("")){ sql2 += " where _id=" + idStr; } //查询结果 Cursor c = db.rawQuery(sql2,null); //SimpleCursorAdapter //SimpleAdapter a = new SimpleAdapter //参数3:数据源;4、表头,即键值对;5、要填充的位置;6、通知将变化的数据反映到页面上 //数据源要有下划线 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item, c,new String[]{"_id","name","age","gender"}, new int[]{R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item}); stuList.setAdapter(adapter); break; case R.id.delete_btn: Log.d("MainActivity", "删除 "); String sql3 = "delete from info_tb where _id="+idStr+""; db.execSQL(sql3); break; case R.id.update_btn: Log.d("MainActivity", "更新 "); String sql4 = "update info_tb set name=?,age=?,gender=? where _id=?"; db.execSQL(sql4,new String[]{nameStr,ageStr,genderStr,idStr}); break; } } }
曾经的曾经去哪了
相关分类