调用setTransactionSuccessful()时提交当前事务,如果不调用此方法会回滚事务。
那么简单自己写就可以了
的问题太多了,我也遇到 了
操作方法:1. 把原数据库包括在项目源码的 res/raw 目录下,然后建立一个DBManager类,代码如下:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 | package com.android.ImportDatabase; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Environment; import android.util.Log; public class DBManager { private final int BUFFER_SIZE = 400000 ; public static final String DB_NAME = "countries.db" ; //保存的数据库文件名 public static final String PACKAGE_NAME = "com.android.ImportDatabase" ; public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME; //在手机里存放数据库的位置 private SQLiteDatabase database; private Context context; DBManager(Context context) { this .context = context; } public void openDatabase() { this .database = this .openDatabase(DB_PATH + "/" + DB_NAME); } private SQLiteDatabase openDatabase(String dbfile) { try { if (!( new File(dbfile).exists())) { //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 InputStream is = this .context.getResources().openRawResource( R.raw.countries); //欲导入的数据库 FileOutputStream fos = new FileOutputStream(dbfile); byte [] buffer = new byte [BUFFER_SIZE]; int count = 0 ; while ((count = is.read(buffer)) > 0 ) { fos.write(buffer, 0 , count); } fos.close(); is.close(); } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null ); return db; } catch (FileNotFoundException e) { Log.e( "Database" , "File not found" ); e.printStackTrace(); } catch (IOException e) { Log.e( "Database" , "IO exception" ); e.printStackTrace(); } return null ; } |
read only 你设置的数据库属性是只可读型的
看报错日志呀
你路径对么,具体报什么错呢
不会报错啊,我做项目的时候就是用的ViewHolder holder = null;后面用的时候实例化就好了。
需要数据库表的键是_id
Context cont = this.getApplicationContext();
String s = cont.getDatabasePath("info.db")+"";
SQLiteDatabase db = SQLiteDatabase.openDatabase(s,null,SQLiteDatabase.OPEN_READONLY);
这样就可以了,试过了
空指针,cursor没初始化(cursor = ?)
你写的SD卡的存储路径不对,改成 String path = Environment.getExternalStorageDirectory().getAbsolutePath();
可能的手机没有获得root权限,要么就是加进去了但是你看不到
建表的话要注意使用单例模式,防止重复建表而报错
desc前面要加空格
getReadDatabase和getWriteDataBase的实际效果是一样的,都是打开可读可写的数据库,但是使用getReadDataBase()时,可能存在特殊情况,比如说磁盘满了,但是你还想往数据库插入东西这个语句就会失效
package com.example.mytest0001;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class CursorAdapterActivity extends Activity {
private ListView lv;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cursor_adapter_activity);
lv = (ListView) findViewById(R.id.lv);
db = SQLiteDatabase.openDatabase(
Environment.getExternalStorageDirectory() + File.separator
+ "info.db", null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor=db.rawQuery("select * from "+Constant.TABLE_NAME, null);
MyCursorAdapter adapter =new MyCursorAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
}
class MyCursorAdapter extends CursorAdapter{
public MyCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
}
/**
*
*/
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return getLayoutInflater().inflate(R.layout.layout1,null );
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
TextView tv_id= (TextView) arg0.findViewById(R.id.tv_id);
TextView tv_name= (TextView) arg0.findViewById(R.id.tv_name);
TextView tv_age= (TextView) arg0.findViewById(R.id.tv_age);
int _id=arg2.getInt(arg2.getColumnIndex(Constant._ID));
String name=arg2.getString(arg2.getColumnIndex(Constant.NAME));
int age=arg2.getInt(arg2.getColumnIndex(Constant.AGE));
tv_id.setText(_id +"");
tv_name.setText(name);
tv_age.setText(age +"");
}
}
}
首先你得打开Android Device Monitor
找到你虚拟机对应应用的位置(我的是在data目录下的data目录里) ,选中你的数据库文件,然后在Android Device Monitor的右上角的位置,点击然后选择导出位置即可导出你想要的文件
(PS: 如果你是手机,那么手机必须是在root了之后,才能看到)