安卓中SQLite的最简单使用(—)

       在android的开发中有时候也会用到sql来存储数据,sql一种轻量级的数据库,使用c编写的嵌入式数据库引擎,SQLite数据库的引擎非常小,但是其存储能力相当了得。能够存储2TB的数据,数据结构为B-tree(多路搜索树,特点是可以减少定位记录时所经历的中间过程,从而加快存取速度)安卓中内嵌了完整的sql,因此我们使用起来也是非常方便的,话不多说,直接上代码实战!

/**
 * Created by zhoukai on 2016/1/25.
 * db.execSQL("insert into student(name,age)values('ZANK_ZHOU','18')");   sql中的增加语句
 * db.execSQL("delete from student where id = 3");                        sql中的删除语句
 * db.execSQL("updata  student set name='ZK' where id=3)");               sql中的更新语句
 * db.execSQL("select * from student",null);                              sql中的查询语句
 * ContentValues和HashTable类似都是一种存储的机制 ,只能存储基本类型的数据,像string,int之类的,不能存储对象。
 * 借助ContentValues可以高效的完成数据库的插入和更新
 */
public class SqliteDemoActivity extends Activity {

    SQLiteDatabase db = null;
    private TextView tv_db_content;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbhelper_demo);
        //创建数据库@param1是数据库的名字@param2是访问权限,此处是指只允许本程序访问,
        // @param3是游标工厂,可设为null,默认为使用系统的
        db = openOrCreateDatabase("person.db",MODE_PRIVATE,null);
        //创建表并以id为主键,自增
        db.execSQL("create table if not exists student(_id integer primary key autoincrement,name text not null,age integer not null)");
        initView();
    }
    private void initView() {
        tv_db_content = (TextView) findViewById(R.id.tv_content);
    }

    //数据库的操作无外乎增、删、改、查操作
    //增加一条记录
    public boolean insertData(){
        long index = 0;
        ContentValues values = new ContentValues();
        values.put("name","ZANK_ZHOU");
        values.put("age","18");
        index =  db.insert("student", null, values);      //index就是你增加记录的行号
        values.clear();
        if(index>0){
            return  true;
        }
        return  false;
    }

    //删除id为3的记录
    public boolean  deleteData() {
        //下面这两句作用是一样的 其中?只是占位符,后面string[]中的值对应了占位符的值
        //db.delete("student","id=3",null); count表示你删除记录的条数
        int count = db.delete("student", "name= ?", new String[]{"3"});
        if(count>0)
        {
            return true;
        }
        return false;
        //有的时候删除表中的数据不能满足需求,这是后我们可以直接干掉数据库文件,数据库文件存储路径为/data/data/"包名"/databases/"你的表明"
//       File file= new File("/data/data/"+getPackageName()+"/databases/"+"student.db" );
//       file.delete();
    }

    //将id为3的name值改为周杰伦,年龄改为20
    public void updataData(){
        ContentValues values = new ContentValues();
        values.put("name","周杰伦");
        values.put("age","20");
        db.update("student", values, "_id=3", null);
        values.clear();
    }

    //查询记录
    public String queryData(){
        StringBuilder builder = new StringBuilder();
        Cursor cursor  = db.rawQuery("select * from student",null);
        String colums[] = cursor.getColumnNames();                               //得到字段数组
        while(cursor.moveToNext()){
            String str = cursor.getString(cursor.getColumnIndex(colums[1]));     //遍历所有记录的name
            builder.append(str);
        }
        return  builder.toString();
    }

    public void click(View view) {
        switch (view.getId()){
            case R.id.insert:
             for(int i = 0;i<50;i++){
                 insertData();
             }
                tv_db_content.setText(queryData());
                break;

            case  R.id.delete:
                deleteData();
                tv_db_content.setText(queryData());
                break;

            case R.id.updata :
                updataData();
                tv_db_content.setText(queryData());   //更新数据
                break;
        }
        
    }


}


//简单的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".sqlutils.DBhelperDemoActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/insert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加数据"
            android:textColor="#000"
            android:onClick="click"
            />
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除数据"
            android:textColor="#000"
            android:onClick="click"
            />

        <Button
            android:id="@+id/updata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="updata数据"
            android:textColor="#000"
            android:onClick="click"
            />


    </LinearLayout>



    <TextView

        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="13sp"
        android:textColor="#000"

        />


</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值