在编程中经常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,能够支持Windows/Linux/Unix等主流操作系统,同时能够跟很多程序语言如C#、PHP、Java等相结合.下面先回顾SQL的基本语句,再讲述Android的基本操作.

一. adb shell回顾SQL语句

    首先,我感觉自己整个大学印象最深的几门课就包括《数据库》,所以想先回顾SQL增删改查的基本语句.而在Android SDK中adb是自带的调试工具,它存放在sdk的platform-tools目录下,通过adb shell可以进入设备控制台,操作SQL语句.

G:cd G:\software\Program software\Android\adt-bundle-windows-x86_64-20140321\sdk\platform-toolsadb shellcd /data/data/com.example.sqliteaction/databases/sqlite3 StuDatabase.db.table.schema
    如下所示我先创建了SQLiteAction工程,同时在工程中创建了StuDatabase.db数据库.输入adb shell进入设备控制台,调用"sqlite3+数据库名"打开数据库,如果没有db文件则创建.


    然后如下图所示,可以输入SQL语句执行增删改查.注意很容易写错SQL语句,如忘记")"或结束";"导致cmd中调用出错.
--创建Teacher表create table Teacher (id integer primary key, name text);--向表中插入数据insert into Teacher (id,name) values('10001', 'Mr Wang');insert into Teacher (id,name) values('10002', 'Mr Yang');--查询数据select * from Teacher;--更新数据update Teacher set name='Yang XZ' where id=10002;--删除数据delete from Teacher where id=10001;


二. SQLite数据库操作

   下面讲解使用SQLite操作数据库:
    1.创建打开数据库
    使用openOrCreateDatabase函数实现,它会自动检测是否存在该数据库,如果存在则打开,否则创建一个数据库,并返回一个SQLiteDatabase对象.
    2.创建表
    通过定义建表的SQL语句,再调用execSQL方法执行该SQL语句实现建立表.

//创建学生表(学号,姓名,电话,身高) 主键学号public static final String createTableStu = "create table Student (" +"id integer primary key, " +"name text, " +"tel text, " +"height real)";//SQLiteDatabase定义db变量db.execSQL(createTableStu);
    3.插入数据
    使用insert方法添加数据,其实ContentValues就是一个Map,Key字段名称,Value值.
    SQLiteDatabase.insert(
        String table,         //添加数据的表名
        String nullColumnHack,//为某些空的列自动复制NULL
        ContentValues values  //ContentValues的put()方法添加数据
    );

//方法一SQLiteDatabase db = sqlHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("id", "10001");values.put("name", "Eastmount");values.put("tel", "15201610000");values.put("height", "172.5");db.insert("Student", null, values);//方法二public static final String insertData = "insert into Student (" +"id, name, tel, height) values('10002','XiaoMing','110','175')";db.execSQL(insertData);
    4.删除数据
    使用delete方法删除表中数据,其中sqlHelper是继承SQLiteDatabase自定义类的实例.
    SQLiteDatabase.delete(
        String table,       //表名
        String whereClause, //约束删除行,不指定默认删除所有行
        String[] whereArgs  //对应数据
    );

//方法一 删除身高>175cmSQLiteDatabase db = sqlHelper.getWritableDatabase();db.delete("Student", "height > ?", new String[] {"175"});//方法二String deleteData = "DELETE FROM Student WHERE height>175";db.execSQL(deleteData);
    5.更新数据
    使用update方法可以修改数据,SQL+execSQL方法就不在叙述.
//小明的身高修改为180SQLiteDatabase db = sqlHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("height", "180");db.update("Student", values, "name = ?", new String[] {"XiaoMing"});
    6.其他操作

    下面是关于数据库的其他操作,其中包括使用SQL语句执行,而查询数据Query方法由于涉及ListView显示,请见具体实例.

//关闭数据库SQLiteDatabase.close();//删除表 执行SQL语句SQLiteDatabase.execSQL("DROP TABLE Student");//删除数据库this.deleteDatabase("StuDatabase.db");//查询数据SQLiteDatabase.query();

三. 数据库操作简单实例

    显示效果如下图所示:
  
    首先,添加activity_main.xml文件布局如下:

                                                                                                                        
    然后是在res/layout中添加ListView显示的stu_item.xml:

<?xml version="1.0" encoding="utf-8"?>             

    再次,添加自定义类MySQLiteOpenHelper:

//添加自定义类 继承SQLiteOpenHelperpublic class MySQLiteOpenHelper extends SQLiteOpenHelper {public Context mContext;//创建学生表(学号,姓名,电话,身高) 主键学号public static final String createTableStu = "create table Student (" +"id integer primary key, " +"name text, " +"tel text, " +"height real)";//抽象类 必须定义显示的构造函数 重写方法 public MySQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {super(context, name, factory, version);mContext = context;}@Overridepublic void onCreate(SQLiteDatabase arg0) {// TODO Auto-generated method stubarg0.execSQL(createTableStu);Toast.makeText(mContext, "Created", Toast.LENGTH_SHORT).show();}@Overridepublic void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {// TODO Auto-generated method stubarg0.execSQL("drop table if exists Student");onCreate(arg0);Toast.makeText(mContext, "Upgraged", Toast.LENGTH_SHORT).show();}}
    最后是MainActivity.java文件,代码如下:
public class MainActivity extends Activity {//继承SQLiteOpenHelper类private MySQLiteOpenHelper sqlHelper;private ListView listview;private EditText edit1;private EditText edit2;private EditText edit3;private EditText edit4;@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sqlHelper = new MySQLiteOpenHelper(this, "StuDatabase.db", null, 2);        //建立新表        Button createBn = (Button) findViewById(R.id.button1);        createBn.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {        sqlHelper.getWritableDatabase();        }        });        //插入数据        Button insertBn = (Button) findViewById(R.id.button2);        edit1 = (EditText) findViewById(R.id.edit_id);        edit2 = (EditText) findViewById(R.id.edit_name);        edit3 = (EditText) findViewById(R.id.edit_tel);        edit4 = (EditText) findViewById(R.id.edit_height);        insertBn.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {        SQLiteDatabase db = sqlHelper.getWritableDatabase();        ContentValues values = new ContentValues();        /*        //插入第一组数据        values.put("id", "10001");        values.put("name", "Eastmount");        values.put("tel", "15201610000");        values.put("height", "172.5");        db.insert("Student", null, values);        */        values.put("id", edit1.getText().toString());        values.put("name", edit2.getText().toString());        values.put("tel", edit3.getText().toString());        values.put("height", edit4.getText().toString());        db.insert("Student", null, values);        Toast.makeText(MainActivity.this, "数据插入成功", Toast.LENGTH_SHORT).show();        edit1.setText("");        edit2.setText("");        edit3.setText("");        edit4.setText("");        }        });        //删除数据        Button deleteBn = (Button) findViewById(R.id.button3);        deleteBn.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {        SQLiteDatabase db = sqlHelper.getWritableDatabase();        db.delete("Student", "height > ?", new String[] {"180"});        Toast.makeText(MainActivity.this, "删除数据", Toast.LENGTH_SHORT).show();        }        });        //更新数据        Button updateBn = (Button) findViewById(R.id.button4);        updateBn.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {        SQLiteDatabase db = sqlHelper.getWritableDatabase();        ContentValues values = new ContentValues();        values.put("height", "180");        db.update("Student", values, "name = ?", new String[] {"XiaoMing"});        Toast.makeText(MainActivity.this, "更新数据", Toast.LENGTH_SHORT).show();        }        });        //查询数据        listview = (ListView) findViewById(R.id.listview1);        Button selectBn = (Button) findViewById(R.id.button5);        selectBn.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {        try {        SQLiteDatabase db = sqlHelper.getWritableDatabase();        //游标查询每条数据        Cursor cursor = db.query("Student", null, null, null, null, null, null);        //定义list存储数据        List> list = new ArrayList>();        //适配器SimpleAdapter数据绑定        //错误:构造函数SimpleAdapter未定义 需把this修改为MainActivity.this        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.stu_item,        new String[]{"id", "name", "tel", "height"},         new int[]{R.id.stu_id, R.id.stu_name, R.id.stu_tel, R.id.stu_height});        //读取数据 游标移动到下一行        while(cursor.moveToNext()) {        Map map = new HashMap();        map.put( "id", cursor.getString(cursor.getColumnIndex("id")) );        map.put( "name", cursor.getString(cursor.getColumnIndex("name")) );        map.put( "tel", cursor.getString(cursor.getColumnIndex("tel")) );        map.put( "height", cursor.getString(cursor.getColumnIndex("height")) );        list.add(map);        }        listview.setAdapter(adapter);        }        catch (Exception e){        Log.i("exception", e.toString());        }        }        });    }}
    PS:希望文章对大家有所帮助,文章是关于SQLite的基础操作,而且没有涉及到数据库的触发器、存储过程、事务、索引等知识,网上也有很多相关的资料.同时现在有门课程《数据库高级技术与开发》,故作者当个在线笔记及基础讲解吧!这篇文章有一些不足之处,但作为基础文章还是不错的.
    下载地址:http://download.csdn.net/detail/eastmount/8159881
    主要参考:
    1.郭霖大神的《第一行代码Android》
    2.
android中的数据库操作By:nieweilin
(By:Eastmount 2014-11-15 夜2点
http://blog.csdn.net/eastmount/)

更多相关文章

  1. “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
  2. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  3. 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
  4. 内容提供器Content Providers
  5. Android(安卓)数据传输方式 WIFI 蓝牙 USB
  6. android SQLite使用SQLiteOpenHelper类对数据库进行操作
  7. Android(安卓)中的缓存机制与实现
  8. eoe源码阅读(一)
  9. 仿豌豆荚实现android连接pc方法

随机推荐

  1. 阿里深度树匹配召回体系演进
  2. 多媒体内容理解在美图社区的应用实践
  3. 详谈pg逻辑复制系列之逻辑复制的搭建(二)
  4. 机器人的眼睛,需要一个Spartan-6 FPGA
  5. 数据驱动的京东精细化消费者运营平台
  6. Opal Kelly公司推出面向FPGA模块的SYZYGY
  7. 移动商城首页的页眉和页脚的布局和flex项
  8. 第三课 选择器、模块化组件、伪类选择
  9. CSS盒模型以及CSS定位
  10. flex项目属性,实战案例。