android项目如何使用己建好的sqlite数据库
发布网友
发布时间:2022-04-09 02:28
我来回答
共1个回答
热心网友
时间:2022-04-09 03:57
public class DataBaseHelper extends SQLiteOpenHelper{
public SQLiteDatabase myDataBase;
private final Context myContext;
/**
* @param context
* @param name
* @param factory
* @param version
*/
public DataBaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void createDataBase() throws IOException{
init();
}
/**
方法说明
* @creator leixun
* @create-time 2012-7-13 下午1:28:13
* @Title: init
* @Description: TODO(初始化数据库,并打开)
* @return void
* @throws
*/
public void init(){
try{
String databasePath = Environment.getExternalStorageDirectory().getAbsolutePath();
String databaseFilename = databasePath + "/" + "cc_db";
File dir = new File(databasePath);
if(!dir.exists()){ //数据库文件目录不存在
dir.mkdir(); //创建目录
}
if(!(new File(databaseFilename)).exists()){ //数据库文件不存在,则复制自带数据库
System.out.println("jianku");
InputStream is = myContext.getResources().openRawResource(R.raw.cc_db_third);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[1024];
int count = 0;
while((count = is.read(buffer))>0){
fos.write(buffer,0,count);
}
fos.flush();
fos.close();
is.close();
}
this.myDataBase = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);//打开数据库文件
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
}
我自己写过的一个类,在其它地方直接调用成员变量 myDataBase就可以了追问大牛,你的DAO类能也贴出来我看看吗.