使用本地数据库创建listview具有复制数据库android时出错

凯里山

嗨,我正在尝试创建一个显示sql文件中的数据的ListView,并且始终出现错误“复制数据库时出错”,或者我无法打开数据库

这是我的DBHelper

public class DBHelper extends SQLiteOpenHelper {

//The Android's default system path of your application database.
@SuppressLint("SdCardPath")
private static String DB_PATH = "/data/data/com.mobapp.dblist/databases/";
private static final String DB_NAME = "infodb";
public static final String TBL_GLOSSARY = "main_conference_items";
public static final String KEY_ID = "_id";
public static final String KEY_DAY_ID = "day_id";
public static final String KEY_TITLE = "title";
public static final String KEY_TIME = "time";
private SQLiteDatabase myDataBase;
private final Context myContext;

public DBHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    if(!dbExist)
    {this.getReadableDatabase();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){}
    if(checkDB != null){
        checkDB.close();
    }
 return checkDB != null ? true : false;
}

 private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
 OutputStream myOutput = new FileOutputStream(outFileName);
 byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();

}
public void openDataBase() throws SQLException {
 String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
}
 @Override
public synchronized void close() {
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public Cursor getConferenceItems() {
    Cursor localCursor =
            this.myDataBase.query(TBL_GLOSSARY, new String[] {
                            KEY_ID,
                            KEY_DAY_ID,
                            KEY_TITLE,
                            KEY_TIME},
                    null,
                    null, null, null, null);

    if (localCursor != null)
        localCursor.moveToFirst();
    return localCursor;
}

这是我显示我的列表视图的片段

public class TabP extends Fragment {

DBHelper dbHelper;
private SimpleCursorAdapter dataAdapter;

public static final String TAG = "Tab_Programme";


// The desired columns to be bound
String[] columns = new String[] {
        DataBaseHelper.KEY_ID,
        DataBaseHelper.KEY_DAY_ID,
        DataBaseHelper.KEY_TITLE,
        DataBaseHelper.KEY_TIME
};

// the XML defined views which the data will be bound to
int[] to = new int[] {
        R.id.row_id,
        R.id.list_day,
        R.id.title,
        R.id.desc,
};

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab_programme,container,false);

    initialazeDatabase();
    Cursor cursor = dbHelper.getConferenceItems();


    dataAdapter = new SimpleCursorAdapter( getActivity(), R.layout.text_list_item_layout, cursor, columns, to, 0);

    ListView listView = (ListView) v.findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) {
            // Get the cursor, positioned to the corresponding row in the result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);

            // Get the state's capital from this row in the database.
            String countryCode =
                    cursor.getString(cursor.getColumnIndexOrThrow("title"));
            Toast.makeText(getActivity().getApplicationContext(),
                    countryCode, Toast.LENGTH_SHORT).show();

        }
    });

    return v;
}

public void initialazeDatabase()
{
    dbHelper = new DBHelper(getActivity());

    try {

        dbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        dbHelper.openDataBase();

    } catch (SQLException sqle) {

        throw sqle;

    }

}
}

谁能帮我吗?从昨天开始我一直在尝试这个。太感谢了。

凯里山

我发现了我的错误。代码有效,我的错误是软件包名称。我没有复制粘贴,这就是为什么我得到了错误的包装。现在可以了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章