android数据库操作的两种常见方式
2014-09-27 18:18
393 查看
第一,将放在资产目录的数据库复制到系统的目录中,在打开数据库进行操作:
1、建立一个工具类完成把文件拷贝到系统目录,本质就是写一个文件到其他文件对象中。
/**
* 拷贝文件到系统的某个目录
*
* @param is
* 源文件的流
* @param destPath
* 目标路径
* @return
*/
public static File copyFile(InputStream is, String destPath) {
try {
File file = new File(destPath);
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
is.close();
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
1)获得系统文件目录:
final File file = new File(getFilesDir(), "commonnum.db");
2)获得位于资产目录的文件的输入流
InputStream is = getAssets().open("commonnum.db");
getResources()
*读取文件资源:1.读取res/raw下的文件资源,通过以下方式获取输入流来进行写操作
· InputStream is =getResources().openRawResource(R.id.filename);
2.读取assets下的文件资源,通过以下方式获取输入流来进行写操作
· AssetManager am = null;
· am = getAssets();
· InputStream is = am.open("filename");
2、读取拷贝到系统的数据库,并获得数据库引用。然后就可以操作数据库了。
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
注:拷贝到系统的数据库文件的路径为:public static final String path = "/data/data/应用包名/files/数据库文件";
自己建立的数据库是databases文件夹下。
第二自己建立数据库,并操作数据库
首先,继承SQLiteOpenHelper并建立数据库表结构。
其次建立dao类,完成数据库的增删该查。
数据库操作的常用语句:
create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))
drop table leaveinfos
select location from data2 where id=(select outkey from data1 where id=?
select number,mode from blacknumber order by _id desc
select number,mode from blacknumber order by _id desc limit ? offset ?
/**
* 分页查询数据
* @param maxnumber 最多获取多少数据
* @param offset 从第几条开始获取
* @return
*/
public List<BlackNumberInfo> findByPage(int maxnumber , int offset){
//select * from blacknumber order by _id desc limit 5 offset 0
SQLiteDatabase db = helper.getReadableDatabase();
List<BlackNumberInfo> infos = new ArrayList<BlackNumberInfo>();
Cursor cursor = db.rawQuery("select number,mode from blacknumber order by _id desc limit ? offset ?",
new String[]{maxnumber+"",offset+""});
while (cursor.moveToNext()) {
BlackNumberInfo info = new BlackNumberInfo();
info.setNumber(cursor.getString(0));
info.setMode(cursor.getString(1));
infos.add(info);
info = null;
}
cursor.close();
db.close();
return infos;
}
insert into blacknumber (number,mode) values (?,?)
update blacknumber set mode =? where number=?
delete from acceptleaveinfos where name=?
注意:数据库操作完毕要关闭数据库和游标结果集!!!对于需要经常查询的数据用集合缓存到内存中,提高效率。
3、在数据库里自定义uri消息邮箱地址
public class AppLockDao {
private AppLockDBOpenHelper helper;
private Context context;
public static Uri uri = Uri.parse("content://cn.itheima.xxx");//自定义的消息邮箱的地址.
public AppLockDao(Context context) {
helper = new AppLockDBOpenHelper(context);
this.context = context;
}
/**
* 查找 包名是否锁定
*
* @param packname
* @return
*/
public boolean find(String packname) {
boolean result = false;
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from applock where packname=?",
new String[] { packname });
if (cursor.moveToNext()) {
result = true;
}
cursor.close();
db.close();
return result;
}
/**
* 查询所有的锁定的包名信息.
* @return
*/
public List<String> findAll(){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select packname from applock ",
null);
List<String> packnames = new ArrayList<String>();
while (cursor.moveToNext()) {
packnames.add(cursor.getString(0));
}
cursor.close();
db.close();
return packnames;
}
/**
* 添加锁定包名
*
* @param packname
* 包名
*/
public void add(String packname) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("insert into applock (packname) values (?)",
new Object[] { packname });
context.getContentResolver().notifyChange(uri, null);
db.close();
}
/**
* 删除锁定的包名
*
* @param number
*/
public boolean delete(String packname) {
SQLiteDatabase db = helper.getWritableDatabase();
int result = db.delete("applock", "packname=?",
new String[] { packname });
db.close();
if (result == 0) {
return false;
} else {
context.getContentResolver().notifyChange(uri, null);
return true;
}
}
}
1、建立一个工具类完成把文件拷贝到系统目录,本质就是写一个文件到其他文件对象中。
/**
* 拷贝文件到系统的某个目录
*
* @param is
* 源文件的流
* @param destPath
* 目标路径
* @return
*/
public static File copyFile(InputStream is, String destPath) {
try {
File file = new File(destPath);
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
is.close();
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
1)获得系统文件目录:
final File file = new File(getFilesDir(), "commonnum.db");
2)获得位于资产目录的文件的输入流
InputStream is = getAssets().open("commonnum.db");
getResources()
*读取文件资源:1.读取res/raw下的文件资源,通过以下方式获取输入流来进行写操作
· InputStream is =getResources().openRawResource(R.id.filename);
2.读取assets下的文件资源,通过以下方式获取输入流来进行写操作
· AssetManager am = null;
· am = getAssets();
· InputStream is = am.open("filename");
2、读取拷贝到系统的数据库,并获得数据库引用。然后就可以操作数据库了。
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
注:拷贝到系统的数据库文件的路径为:public static final String path = "/data/data/应用包名/files/数据库文件";
自己建立的数据库是databases文件夹下。
第二自己建立数据库,并操作数据库
首先,继承SQLiteOpenHelper并建立数据库表结构。
其次建立dao类,完成数据库的增删该查。
数据库操作的常用语句:
create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))
drop table leaveinfos
select location from data2 where id=(select outkey from data1 where id=?
select number,mode from blacknumber order by _id desc
select number,mode from blacknumber order by _id desc limit ? offset ?
/**
* 分页查询数据
* @param maxnumber 最多获取多少数据
* @param offset 从第几条开始获取
* @return
*/
public List<BlackNumberInfo> findByPage(int maxnumber , int offset){
//select * from blacknumber order by _id desc limit 5 offset 0
SQLiteDatabase db = helper.getReadableDatabase();
List<BlackNumberInfo> infos = new ArrayList<BlackNumberInfo>();
Cursor cursor = db.rawQuery("select number,mode from blacknumber order by _id desc limit ? offset ?",
new String[]{maxnumber+"",offset+""});
while (cursor.moveToNext()) {
BlackNumberInfo info = new BlackNumberInfo();
info.setNumber(cursor.getString(0));
info.setMode(cursor.getString(1));
infos.add(info);
info = null;
}
cursor.close();
db.close();
return infos;
}
insert into blacknumber (number,mode) values (?,?)
update blacknumber set mode =? where number=?
delete from acceptleaveinfos where name=?
注意:数据库操作完毕要关闭数据库和游标结果集!!!对于需要经常查询的数据用集合缓存到内存中,提高效率。
3、在数据库里自定义uri消息邮箱地址
public class AppLockDao {
private AppLockDBOpenHelper helper;
private Context context;
public static Uri uri = Uri.parse("content://cn.itheima.xxx");//自定义的消息邮箱的地址.
public AppLockDao(Context context) {
helper = new AppLockDBOpenHelper(context);
this.context = context;
}
/**
* 查找 包名是否锁定
*
* @param packname
* @return
*/
public boolean find(String packname) {
boolean result = false;
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from applock where packname=?",
new String[] { packname });
if (cursor.moveToNext()) {
result = true;
}
cursor.close();
db.close();
return result;
}
/**
* 查询所有的锁定的包名信息.
* @return
*/
public List<String> findAll(){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select packname from applock ",
null);
List<String> packnames = new ArrayList<String>();
while (cursor.moveToNext()) {
packnames.add(cursor.getString(0));
}
cursor.close();
db.close();
return packnames;
}
/**
* 添加锁定包名
*
* @param packname
* 包名
*/
public void add(String packname) {
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("insert into applock (packname) values (?)",
new Object[] { packname });
context.getContentResolver().notifyChange(uri, null);
db.close();
}
/**
* 删除锁定的包名
*
* @param number
*/
public boolean delete(String packname) {
SQLiteDatabase db = helper.getWritableDatabase();
int result = db.delete("applock", "packname=?",
new String[] { packname });
db.close();
if (result == 0) {
return false;
} else {
context.getContentResolver().notifyChange(uri, null);
return true;
}
}
}
相关文章推荐
- Android数据库两种操作方式介绍: SQLite与ORMLite
- Android常用的两种数据库操作方式总结
- Android开发之使用sqlite3工具操作数据库的两种方式
- Android开发之使用sqlite3工具操作数据库的两种方式
- Android数据库两种操作方式介绍: SQLite与ORMLite
- Android数据库两种操作方式介绍: SQLite与ORMLite
- Android 获取手机IP地址的两种方式及常见问题
- Android开发12——Andorid中操作数据库的insert的两种方法以及nullColumnHack
- Android学习记录(2)—Android中数据库的常见操作
- android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结
- 对数据库进行操作的两种方式
- 在android开发中,经常用到去解析xml文件,常见的解析xml的方式有一下三种:SAX、Pull、Dom解析方式。最近做了一个android版的CSDN阅读器,用到了其中的两种(sax,pull)
- .NET操作postgre数据库的两种方式
- android数据库储方式(一)----SQLite的基本操作
- Android中实现SQLite数据库CRUD操作的两种方式
- android基本方式对数据库的操作
- Android学习记录(2)—Android中数据库的常见操作
- Android开发12――Andorid中操作数据库的insert的两种方法以及nullColumnHack
- (转载)Android两种Tab分页的方式:TabActivity和ActivityGroup以及Android项目几种常见的应用架构
- MyBatis操作数据库的两种方式