package com.hanqi.cunchu; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class shujuku1 extends AppCompatActivity { EditText name, id, sex, age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shujuku1); id = (EditText) findViewById(R.id.i1); name = (EditText) findViewById(R.id.i2); sex = (EditText) findViewById(R.id.i3); age = (EditText) findViewById(R.id.i4); } public void b1(View view){ m mdb = new m("test2",1); //得到连接 SQLiteDatabase sqLiteDatabase= mdb.getWritableDatabase(); //关闭连接 sqLiteDatabase.close(); } public void b2(View view){ //使用工具类得到数据库对象 m mdb = new m("test2",4); //得到连接 SQLiteDatabase sqLiteDatabase= mdb.getWritableDatabase(); //关闭连接 sqLiteDatabase.close(); } public void b3(View view){ SQLiteDatabase sd=new m("test2",4).getReadableDatabase(); ContentValues cv=new ContentValues(); cv.put("_id",id.getText().toString()); cv.put("name",name.getText().toString()); cv.put("age",age.getText().toString()); cv.put("sex",sex.getText().toString()); sd.insert("NewTable", null, cv); sd.close(); } public void b5(View view){ SQLiteDatabase sd=new m("test",4).getReadableDatabase(); String s="1=1"; if(id.getText().length()>0){ s+=" and _id="+id.getText().toString(); } if(name.getText().length()>0){ s+=" and name="+name.getText().toString(); } if(sex.getText().length()>0){ s+=" and sex="+sex.getText().toString(); } if(age.getText().length()>0){ s+=" and age="+age.getText().toString(); } Cursor c = sd.query("NewTable", null, s, null, null, null, null); while (c.moveToNext()){ String ss=c.getString(c.getColumnIndex("_id"))+" "+c.getString(1); } c.close(); sd.close(); } public void b6(View v){ SQLiteDatabase sd=new m("test2",4).getReadableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",name.getText().toString()); cv.put("age",age.getText().toString()); cv.put("sex",sex.getText().toString()); sd.update("NewTable", cv, "_id=?", new String[]{id.getText().toString()}); sd.close(); } public void b7(View v){ SQLiteDatabase sd=new m("test2",4).getReadableDatabase(); sd.delete("NewTable", "_id=?", new String[]{id.getText().toString()}); } public void b8(View v){ SQLiteDatabase sd=new m("test2",4).getReadableDatabase(); sd.beginTransaction(); try { ContentValues cv = new ContentValues(); cv.put("name","呵呵哒"); ContentValues cv2 = new ContentValues(); cv2.put("name", "呵呵哒2"); int i= sd.update("NewTable",cv,"_id=1",null); // if(i>0){ // throw new RuntimeException("呵呵哒"); // } int k= sd.update("NewTable",cv,"_id=1",null); sd.setTransactionSuccessful(); Toast.makeText(shujuku1.this, "改变的记录条数"+(i+k), Toast.LENGTH_SHORT).show(); } catch (Exception e){ e.printStackTrace(); } finally { sd.endTransaction(); sd.close(); } } class m extends SQLiteOpenHelper{ public m(String dbname,int ver){ super(shujuku1.this,dbname,null,ver); } @Override public void onCreate(SQLiteDatabase db) { String sql="CREATE TABLE "main"."NewTable" ("_id" INTEGER NOT NULL," + ""name" varchar(20),"sex" char(1)," + ""age" INTEGER,PRIMARY KEY ("_id") " + ") " + ";"; db.execSQL(sql); Log.e("tag", "创建完成"); //2.执行初始化数据的语句,insert语句 ContentValues cv=new ContentValues(); cv.put("name","张三"); cv.put("sex","男"); cv.put("age",15); //z执行插入 long l= db.insert("NewTable",null,cv); Log.e("tag", "初始化数据" + l); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(newVersion==4){ ContentValues cv=new ContentValues(); cv.put("name","李四"); String sql="update NewTable set name='李四' where _id=1"; String s[]={"1","18"}; //调用db的更新方法 int i= db.update("NewTable",cv,"_id=? and age<?",s); Log.e("tag", "升级数据 数据条数=" + i); db.execSQL(sql); } } @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); } } }