其实零基础去学安卓,我觉得是会欠缺很多,因为安卓的数据库语句,被安卓进行二次封装,开发者并不需要过多的去了解SQL语句,而对于一个开发着,SQL,正则表达式,版本管理器是必备。
说实话,javaee 我只是一个过客,ajax 三大框架,都没有学,基础不扎实,只能说我认识他,他不认识。
不废话了,先写数据库语句吧 不在乎就是增删改查。
创建表
create table person( _id integer primary key, name varchar(20), age integer );
添加
insert into person(name,age)values('lisi',19); --在person表里 对应的name,age 进行添加属性,lisi 19
查询
select * from person where name="zhangsan" 其中select 和where 是在sql 语句 详细的可以去看w3c的数据库API吧
删除
delete from person where _id=1 删除ID为1的
修改
update person set name='文杰1' where name='文杰' ---将name 叫 文杰--改为文杰1
对于一个安卓开发者,数据库有着和举足轻重的地位,但是大部分人不怎么想去学,不过基本的语句还是要熟悉的。
这篇文章我想用另外一种思维去看看安卓sqlite
sqlite
有一个类,专门用来创建表和执行语句的 叫做SQLiteOpenHelper
你需要去继承他实现他的构造方法,我们只需要一个属性 context
其中super语句中第二属性就是数据库名字 第一个context 第三个是curce对象 第四个版本号 如果不是一会执行update方法
其中Oncreate方法实在写入语句的时候执行
对了 其中对于一个项目来说,第一件事 考虑到 一个项目需要的包结构
db 数据库
dao DAO层是数据库访问层
bean entry类 实体类
test 测试类
PersonSQLiteOpenHelper 类的代码如下
package com.example.webqury.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class PersonSQLiteOpenHelper extends SQLiteOpenHelper { public PersonSQLiteOpenHelper(Context context) { super(context, "jdk.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { String sql="create table person(_id integer primary key,name varchar(20),age integer);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
对于PersonDao 这个类 是完全可以写在一起的,不过对于学过javaee的人来说,会习惯的写成MVC模式,所以读者讲究下。
首先对于SQLiteOpenHelper 是一个抽象类,里面有很多借口和抽象方法其中 有个跟数据库有很大关系的类,getWritableDatabase 他的执行也会执行Oncreate方法
所以要在 PersonDao 实例化PersonSQLiteOpenHelper 类 getWritableDatabase需要一个context 所以直接写在构造方法中。
<pre name="code" class="java">package com.example.webqury.dao; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.webqury.db.PersonSQLiteOpenHelper; import com.example.webqury.entities.Person; public class PersonDao { private PersonSQLiteOpenHelper helper; public PersonDao(Context context) { helper = new PersonSQLiteOpenHelper(context); } /** * 添加到person表一条数据 * * @param person */ public void insert(Person person) { SQLiteDatabase db = helper.getWritableDatabase(); if (db.isOpen()) {// 如果数据库打开, 执行添加的操作 // 执行添加到数据库的操作 // insert into person(name,age)values('lisi',19); db.execSQL("insert into person(name,age)values(?,?);", new Object[] { person.getName(), person.getAge() }); db.close(); } } /** * 更据id删除记录 * * @param id * 根据ID删除记录 */ public void delete(int id) { SQLiteDatabase db = helper.getWritableDatabase(); // 获得可写的数据库对象 if (db.isOpen()) { // delete from person where _id=1 db.execSQL("delete from person where _id=?", new Integer[] { id }); db.close(); } } /** * 根据id找到记录, 并且修改姓名 * @param id * @param name */ public void update(int id, String name) { SQLiteDatabase db = helper.getWritableDatabase(); if(db.isOpen()) { // 如果数据库打开, 执行添加的操作 // update person set name='文杰1' where name='文杰' db.execSQL("update person set name = ? where _id = ?;", new Object[]{name, id}); db.close(); // 数据库关闭 } } public List<Person> queryAll() { SQLiteDatabase db = helper.getReadableDatabase(); // 获得一个只读的数据库对象 if (db.isOpen()) { // select * from person where name="zhangsan" Cursor cursor = db.rawQuery("select _id, name, age from person;", null); if (cursor != null && cursor.getCount() > 0) { List<Person> personList = new ArrayList<Person>(); int id; String name; int age; while (cursor.moveToNext()) { id = cursor.getInt(0); // 取第0列的数据 id name = cursor.getString(1); // 取姓名 age = cursor.getInt(2); // 取年龄 personList.add(new Person(id, name, age)); } db.close(); return personList; } db.close(); } return null; } /** * 根据id查询人 * * @param id * @return */ public Person queryItem(int id) { SQLiteDatabase db = helper.getReadableDatabase(); // 获得一个只读的数据库对象 if (db.isOpen()) { Cursor cursor = db.rawQuery( "select _id, name, age from person where _id = ?;", new String[] { id + "" }); if (cursor != null && cursor.moveToFirst()) { int _id = cursor.getInt(0); String name = cursor.getString(1); int age = cursor.getInt(2); db.close(); return new Person(_id, name, age); } db.close(); } return null; } }
我在上面讲过 DAO层是数据库访问层 所以 还需要一个test调用方法,所以PersonDao里面的方法不能写死了,需要进行传值才行。 注意其中的对比。
在查询中,安卓队语句进行了专门的封装
<pre name="code" class="java">rawQuery 方法,返回的时候 Cursor,他有随机读取的能力, 也是从web 移植过来的 一行一行的读取 查询一条的时候 判断是 一直读取一条就可以停止了 接着就是 test了 <pre name="code" class="java">package com.example.webqury.test; import java.util.List; import android.test.AndroidTestCase; import android.util.Log; import com.example.webqury.dao.PersonDao; import com.example.webqury.db.PersonSQLiteOpenHelper; import com.example.webqury.entities.Person; public class test extends AndroidTestCase { private static final String TAG = "test"; public void test(){ // 数据库什么时候创建 PersonSQLiteOpenHelper openHelper = new PersonSQLiteOpenHelper(getContext()); // 第一次连接数据库时创建数据库文件. onCreate会被调用 openHelper.getReadableDatabase(); } public void testInsert() { PersonDao dao = new PersonDao(getContext()); for (int i = 0; i < 20; i++) { dao.insert(new Person(0, "冠希"+i, 28+i)); } } public void testDelete() { PersonDao dao = new PersonDao(getContext()); dao.delete(1); } public void testUpdate() { PersonDao dao = new PersonDao(getContext()); dao.update(3, "凤姐"); } public void testQueryAll() { PersonDao dao = new PersonDao(getContext()); List<Person> personList = dao.queryAll(); for (Person person : personList) { Log.i(TAG, person.toString()); } } public void testQueryItem() { PersonDao dao = new PersonDao(getContext()); Person person = dao.queryItem(4); Log.i(TAG, person.toString()); } }