|
1.在程序中如何使用自定义的数据库
在程序外部获取到数据库以后,将这个数据库放到res/raw目录下。如果数据库过大,可以考虑将数据库的后缀名改为.jpg这样做的目的是系统会帮你压缩这个数据库。
当第一次打开应用程序的时候,数据库会被加载到data/data目录下。这个需要在模拟器环境下才可以看到。在测试的时候,可以检查该目录下是否成功生成数据库。也可以在DDMS里面把数据库取出来,使用SqliteDev来查看表结构等等。
需要在程序中执行一些数据库操作的时候,可以先在SqliteDev里面用sql语句进行测试。如果成功执行了,再写到代码里面。这样效率会比较高。
public class DataBaseHelper extends SQLiteOpenHelper
{
public final static String DB_NAME = "telloc.db";
public DataBaseHelper(Context context, String dbName)//使用自定义的构造器
{
super(context, dbName, null, 3);
// 先执行 判断 数据库文件是否存在, 不存在则从本地文件拷贝至数据库位置
CopyDB(context, false);
}
/**
* 第一次执行程序的时候,判断是否存在数据库,不存在就加入
*
* */
@Override
public void onCreate(SQLiteDatabase db)
{
}
/**
* 数据库版本号发生变化的时候被调用,更新数据库和数据库版本号
*
* */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
/**
* 复制数据库, 不存在或者强制要求覆盖时覆盖
*
* @param isCover
*/
public static void CopyDB(Context context, boolean willCover) {
System.out.println("CopyDB" + context);
File path = context.getDatabasePath(DB_NAME);
if (!path.exists()) {
// 首先要创建这个文件夹, 如果不存在的话。。。 否则FileOutputStream(path) 报错
new File(path.toString().substring(0, path.toString().lastIndexOf("/"))).mkdirs();
} else if (!willCover) {
return;
}
InputStream is = context.getResources().openRawResource(R.raw.telloc);
FileOutputStream fos;
try {
fos = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
//Log.e(TAG, "create new database");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用
helper = new DataBaseHelper(myContext, DataBaseHelper.DB_NAME);
String areaCity = "";
Cursorcursor = helper.getReadableDatabase().query(
"area_code", //表名
new String[]{"area_code", "city"}, //字段名
"area_code = ? or area_code = ?", //查询前面3位或者4位
new String[]{myNumber.substring(0, 3), myNumber.substring(0, 4)},
null, null, null);
if(cursor.getCount() == 0)
{
areaCity = "";
}
else//有查询出记录,取第一条
{
if(cursor.moveToFirst())
{
areaCity = cursor.getString(cursor.getColumnIndex("city"));
}
}
cursor.close();//使用后记得关闭
return areaCity;
3.创建自己的数据库
String sqlForCreateTable = "CREATE TABLE [my_calls]("+" [_id]INTEGER, "+" [number]INTEGER, "+" [date]INTEGER, "+" [duration]INTEGER, "+" [type]INTEGER, "+" [name]VARCHAR(50), "+" [tel_location]VARCHAR(100))";
Log.e("sqlForCreateTable", sqlForCreateTable);
db.execSQL(sqlForCreateTable);
4.查找系统数据库中的字段,添加到自己的数据库
Cursor cursor = myContext.getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[]{CallLog.Calls._ID, CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.DURATION, CallLog.Calls.TYPE, CallLog.Calls.CACHED_NAME}, null, null, null);
if(cursor != null)//获取查询出来的记录,添加到自己的表中
{
Log.e("cursorCount", "cursor:"+cursor.getCount());
while(cursor.moveToNext())
{
ContentValues cv = new ContentValues();//类似于键值对的方式保存数据
cv.put("_id", cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID)));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
cv.put("number", number);
cv.put("date", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
cv.put("duration", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
cv.put("type", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));
cv.put("name", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
cv.put("number", cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
cv.put("tel_location", getTelLocation(db,number));
db.insert(TABLE_NAME, null, cv);
}
}
cursor.close();
总结:
操作系统的数据库的时候,系统已经将这个操作封装为providor,可以直接使用例如:
myContext.getContentResolver().query(.......);
而操作自己的数据库的时候,需要new一个数据库对象,例如:
helper.getReadableDatabase().query(........);
操作后返回的是一个Cursor对象。这个对象对象包含了查询出来的结果
一般需要对这个cursor中的对象进行读取。注意读取后要关闭这个对象。 |
|