Android为SQLite数据库提供了全面的支持,创建的任何数据库都会被App内部的类访问到。(不是外部)
The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database. For example:
使用一个新的SQLite数据库的方法是创建一个SQLiteOpenHelper的子类,并且重写onCreate方法,在这里面你可以执行一个SQLite命令在数据库中创建一张表。
You can then get an instance of your SQLiteOpenHelper implementation using the constructor you've defined. To write to and read from the database, call getWritableDatabase() and getReadableDatabase(), respectively. These both return a SQLiteDatabase object that represents the database and provides methods for SQLite operations.
首先实现SQLiteOpenHelper的子类:
package com.example.android_db.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { private static String name = "mydb.db"; // 表示数据库的名称 private static int version = 2; // 数据库的版本号码 public DBOpenHelper(Context context) { super(context, name, null, version); // 这里没有真正创建数据库 } // 当数据库创建的时候,是第一次被执行,完成对数据库表的创建 //数据库不存在的时候则创建 @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //支持的数据类型 整型 字符串 日期类型 二进制数据类型 String sql = "create table person(id integer primary key autoincrement, name varchar(64),address varchar(64))"; db.execSQL(sql); } //数据库版本不一致则升级,不会再去执行onCreate方法 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String sql = "alter table person add sex varchar(8)"; db.execSQL(sql); } }
这里注意:SQLIteHelper将数据库实力的创建和打开操作延迟到了第一次需要他们时,并在成功打开数据库实例后缓存他们。
对于数据库的操作有两种方式:
一 自己编写SQL语句,例如插入一条记录:
@Override public boolean addPerson(Object[] params) { // TODO Auto-generated method stub boolean flag = false; //实现对数据库的CRUD SQLiteDatabase database = null; try { String sql = "insert into person(name,address,sex) values(?,?,?)"; database = helper.getWritableDatabase(); //实现对数据库写的操作 database.execSQL(sql, params); flag = true; } catch (Exception e) { // TODO: handle exception }finally{ if (database != null){ database.close(); } } return flag; }
package com.example.android_db.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.android_db.db.DBOpenHelper; import com.example.android_db.service.PersonService2; public class PersonDao2 implements PersonService2 { private DBOpenHelper helper = null; public PersonDao2(Context context) { // TODO Auto-generated constructor stub helper = new DBOpenHelper(context); } @Override public boolean addPerson(ContentValues values) { // TODO Auto-generated method stub boolean flag = false; SQLiteDatabase database = null; long id = -1; try { database = helper.getWritableDatabase(); id = database.insert("person", null, values); flag = (id != -1); } catch (Exception e) { // TODO: handle exception } finally { if (database != null) { database.close(); } } return flag; } @Override public boolean deletePerson(String whereClause, String[] whereArgs) { // TODO Auto-generated method stub boolean flag = false; SQLiteDatabase database = null; int count = 0; try { database = helper.getWritableDatabase(); count = database.delete("person", whereClause, whereArgs); flag = (count > 0); } catch (Exception e) { // TODO: handle exception } finally { if (database != null) { database.close(); } } return flag; } @Override public boolean updatePerson(ContentValues values, String whereClause, String[] whereArgs) { // TODO Auto-generated method stub boolean flag = false; SQLiteDatabase database = null; int count = 0; // 影响数据库的行数 try { database = helper.getWritableDatabase(); count = database.update("person", values, whereClause, whereArgs); flag = (count > 0); } catch (Exception e) { // TODO: handle exception } finally { if (database != null) { database.close(); } } return flag; } @Override public Map<String, String> viewPerson(String selection, String[] selectionArgs) { // TODO Auto-generated method stub // select 列的名称 from Map<String, String> map = new HashMap<String, String>(); SQLiteDatabase database = null; Cursor cursor = null; try { database = helper.getReadableDatabase(); cursor = database.query(true, "person", null, selection, selectionArgs, null, null, null, null); int cols_len = cursor.getColumnCount(); while (cursor.moveToNext()) { for (int i = 0; i < cols_len; i++) { String cols_name = cursor.getColumnName(i); String cols_value = cursor.getString(cursor .getColumnIndex(cols_name)); if (cols_value == null) { cols_value = ""; } map.put(cols_name, cols_value); } } } catch (Exception e) { // TODO: handle exception } finally { if (database != null) { database.close(); } } return map; } @Override public List<Map<String, String>> listPersonMaps(String selection, String[] selectionArgs) { // TODO Auto-generated method stub List<Map<String, String>> list = new ArrayList<Map<String, String>>(); SQLiteDatabase database = null; Cursor cursor = null; try { database = helper.getReadableDatabase(); cursor = database.query(false, "person", null, selection, selectionArgs, null, null, null, null); int cols_len = cursor.getColumnCount(); while(cursor.moveToNext()){ Map<String, String> map = new HashMap<String, String>(); for (int i = 0; i < cols_len; i++) { String cols_name = cursor.getColumnName(i); String cols_value = cursor.getString(cursor.getColumnIndex(cols_name)); if (cols_value == null){ cols_value = ""; } map.put(cols_name, cols_value); } list.add(map); } } catch (Exception e) { // TODO: handle exception } finally { if (database != null) { database.close(); } } return list; } }