我想從我的數據庫中檢索我的sqlite數據,並且我想將其填充到listview
中。我遵循教程並實現了代碼。我創建了一個名爲MYCATEGORY.db的數據庫並將其複製到我的資產文件夾中。但我得到異常「從資源文件」複製數據庫的問題。從現有數據庫檢索數據並將其填充到列表視圖
我的主要activity.java
public class MainActivity extends ActionBarActivity {
private DBHelper dbhelper = null;
private Cursor ourCursor = null;
private DBAdapter adapter= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView) findViewById(R.id.listView1);
dbhelper = new DBHelper(this);
dbhelper.createDatabase();
dbhelper .openDataBase();
ourCursor = dbhelper .getCursor();
startManagingCursor(ourCursor);
adapter= new DBAdapter (ourCursor);
myListView.setAdapter(adapter);
}
class DBAdapter extends CursorAdapter {
DBAdapter (Cursor c){
super(MainActivity.this, c);
}
public void bindView(View row, Context ctxt, Cursor c) {
DBHolder holder =(DBHolder)row.getTag();
holder.populateFrom(c,dbhelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row =inflater.inflate(R.layout.row,parent,false);
DBHolder holder =new DBHolder(row);
row.setTag(holder);
return (row);
}
}
static class DBHolder {
private TextView name=null;
DBHolder(View row){
name=(TextView)row.findViewById(R.id.textView1);
}
void populateFrom(Cursor c,DBHelper r){
name.setText(r.getName(c));
}
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.databaseapp/databases/";
private static final String DATABASE_NAME= "MYCATEGORY.db";
private static final String TABLE_NAME = "MYMAINTABLE";
private static final String COLUMN_ID = "cat_id";
private static final String COLUMN_NAME= "cat_name";
private static final int SCHEMA_VERSION = 1;
public SQLiteDatabase dbSqlite;
private final Context myContext;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext =context ;
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {}
void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist =DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExists(){
SQLiteDatabase db=null;
try {
String databasePath =DATABASE_PATH + DATABASE_NAME ;
db = SQLiteDatabase .openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
}
catch(SQLException e) {
Log.e("sqlHelpeer ","database not found");
}
if(db != null){
db.close();
}
return db!=null ? true :false;
}
private void copyDBFromResource(){
InputStream inputstream = null;
OutputStream outputstream = null;
String dbFilePath = DATABASE_PATH +DATABASE_NAME;
try{
inputstream =myContext .getAssets() .open(DATABASE_NAME);
outputstream =new FileOutputStream(dbFilePath);
byte[] buffer =new byte[1024];
int length;
while ((length=inputstream.read(buffer)) > 0){
outputstream.write(buffer,0,length);
}
outputstream.flush();
outputstream.close();
inputstream.close();
}
catch(IOException e){
throw new Error("problem copyingg database from resource file");
}
}
public void openDataBase() throws SQLException{
String myPath =DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close(){
if(dbSqlite != null) {
dbSqlite.close();
}
super.close();
}
public Cursor getCursor(){
SQLiteQueryBuilder queryBulider = new SQLiteQueryBuilder();
queryBulider .setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] {COLUMN_ID,COLUMN_NAME};
Cursor mCursor =queryBulider.query(dbSqlite, asColumnsToReturn, null, null, null,null,"cat_name ASC");
return mCursor;
}
public String getName(Cursor c) {
return(c.getString(1));
}
}
這是我的日誌貓
11-25 14:21:27.095: W/dalvikvm(18936): threadid=1: thread exiting with uncaught exception (group=0x40c89a68)
11-25 14:21:27.095: E/AndroidRuntime(18936): FATAL EXCEPTION: main
11-25 14:21:27.095: E/AndroidRuntime(18936): java.lang.Error: problem copyingg database from resource file
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.example.androiddatabaselist.DBHelper.copyDBFromResource(DBHelper.java:92)
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.example.androiddatabaselist.DBHelper.createDB(DBHelper.java:52)
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.example.androiddatabaselist.DBHelper.createDatabase(DBHelper.java:45)
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.example.androiddatabaselist.MainActivity.onCreate(MainActivity.java:26)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.Activity.performCreate(Activity.java:4470)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.os.Looper.loop(Looper.java:137)
11-25 14:21:27.095: E/AndroidRuntime(18936): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-25 14:21:27.095: E/AndroidRuntime(18936): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:21:27.095: E/AndroidRuntime(18936): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
11-25 14:21:27.095: E/AndroidRuntime(18936): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
11-25 14:21:27.095: E/AndroidRuntime(18936): at dalvik.system.NativeStart.main(Native Method)
檢查:https://github.com/jgilfelt/android-sqlite-asset-helper – 2014-11-25 09:01:55
,而不是拋出新的錯誤,whold你的通過打印e.stacktrace()檢查你的代碼拋出了什麼異常? – Darpan 2014-11-25 09:04:14