3

public class SqliteActivity extends Activity implements View.OnClickListener{
    MyDataBaseHelper myDataBaseHelper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        myDataBaseHelper = new MyDataBaseHelper(this,"BookStore3.db",null,2);
        db = myDataBaseHelper.getWritableDatabase();
        ((Button)findViewById(R.id.sql_create)).setOnClickListener(this);
        ((Button)findViewById(R.id.sql_add)).setOnClickListener(this);
        ((Button)findViewById(R.id.sql_del)).setOnClickListener(this);
        ((Button)findViewById(R.id.sql_update)).setOnClickListener(this);
        ((Button)findViewById(R.id.sql_retrive)).setOnClickListener(this);
        ((Button)findViewById(R.id.sql_ff)).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sql_create:
                db = myDataBaseHelper.getWritableDatabase();
                break;
            case R.id.sql_add:
                ContentValues values = new ContentValues();
                values.put("author","guoxinhua");

                values.put("price",115.1);
                values.put("zhe",false);
              long rowid = db.insert("Book",null,values);
                myDataBaseHelper.getWritableDatabase().execSQL("insert into Book(author,zhe,price) values('wen','false','135')");
                myDataBaseHelper.getWritableDatabase().execSQL("insert into Book(author,zhe,price) values(?,?,?)",new String[]{"nini","false","5"});
                values.clear();
                Toast.makeText(this,"add ok"+rowid,Toast.LENGTH_SHORT).show();
                break;
            case R.id.sql_del:
                db.delete("Book","author !=?",new String[]{"wen"});
                break;
            case R.id.sql_update:
                ContentValues values1 = new ContentValues();
                values1.put("zhe","true");
                db.update("Book",values1,"author=?",new String[]{"guoxinhua"});
                break;
            case R.id.sql_retrive:
                Cursor cursor=myDataBaseHelper.getWritableDatabase().query("Book",null,null,null,null,null,null,null);
                Toast.makeText(this,"count:"+cursor.getCount(),Toast.LENGTH_SHORT).show();
                if(cursor.moveToFirst()){
                    do{
                        Log.d("TT",cursor.getString(cursor.getColumnIndex("author"))+"|"+cursor.getString(cursor.getColumnIndex("ff")));
                    }while (cursor.moveToNext());
                }
                Toast.makeText(this,"query ok",Toast.LENGTH_SHORT).show();
                break;
            case R.id.sql_ff:
                db.execSQL("alter table Book add column ff text");
                break;
        }
    }

    class MyDataBaseHelper extends SQLiteOpenHelper{
        public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table Book(id integer primary key autoincrement,author text,price real,zhe blob)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists Book");
            onCreate(db);
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值