SQLite导出/导入时出错

问题描述 投票:-1回答:1

我使用外部源代码导入导出我的SQlite数据库。我有我的ListAdapter和SQliteHelper类。我不明白我应该在错误中替换什么。在这里,我已经展示了我在工作室中找到错误或红色单词的位置,然后我附加了ListAdapter和SQLiteHelper类。

DbAdapter dbAdapter = new DbAdapter(ctx);
        dbAdapter.open();

这里DbAdapter和开放的单词在android工作室中显示为红色


while(cursor.moveToNext()){
    dbAdapter.createQuote(
            cursor.getString(titleColumn),
            cursor.getString(timestampColumn)
    );
}

sqlDb.close();
cursor.close();
dbAdapter.close();

这里的createQuote和close以红色显示


这是我从外部源My DbExportImport Class复制的主要代码

/** Imports the file at IMPORT_FILE **/
protected static boolean importIntoDb(Context ctx){
    if( ! SdIsPresent() ) return false;

    File importFile = IMPORT_FILE;

    if( ! checkDbIsValid(importFile) ) return false;

    try{
        SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
                (importFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);

        Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
                null, null, null, null, null, null, null
        );

        DbAdapter dbAdapter = new DbAdapter(ctx);
        dbAdapter.open();

        final int titleColumn = cursor.getColumnIndexOrThrow("title");
        final int timestampColumn = cursor.getColumnIndexOrThrow("timestamp");

        // Adds all items in cursor to current database
        cursor.moveToPosition(-1);
        while(cursor.moveToNext()){
            dbAdapter.createQuote(
                    cursor.getString(titleColumn),
                    cursor.getString(timestampColumn)
            );
        }

        sqlDb.close();
        cursor.close();
        dbAdapter.close();
    } catch( Exception e ){
        e.printStackTrace();
        return false;
    }

    return true;
}

/** Given an SQLite database file, this checks if the file
 * is a valid SQLite database and that it contains all the
 * columns represented by DbAdapter.ALL_COLUMN_KEYS **/
protected static boolean checkDbIsValid( File db ){
    try{
        SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
                (db.getPath(), null, SQLiteDatabase.OPEN_READONLY);

        Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
                null, null, null, null, null, null, null
        );

        // ALL_COLUMN_KEYS should be an array of keys of essential columns.
        // Throws exception if any column is missing
        for( String s : DbAdapter.ALL_COLUMN_KEYS ){
            cursor.getColumnIndexOrThrow(s);
        }

        sqlDb.close();
        cursor.close();
    } catch( IllegalArgumentException e ) {
        Log.d(TAG, "Database valid but not the right type");
        e.printStackTrace();
        return false;
    } catch( SQLiteException e ) {
        Log.d(TAG, "Database file is invalid.");
        e.printStackTrace();
        return false;
    } catch( Exception e){
        Log.d(TAG, "checkDbIsValid encountered an exception");
        e.printStackTrace();
        return false;
    }

    return true;
}

我的ListAdapter类

public class ListAdapter extends BaseAdapter {

    Context context;
    ArrayList<String> ID;
    ArrayList<String> Name;
    ArrayList<String> PhoneNumber;
    ArrayList<String> Date;


    public ListAdapter(
            Context context2,
            ArrayList<String> id,
            ArrayList<String> name,
            ArrayList<String> phone,
            ArrayList<String> date
    )
    {

        this.context = context2;
        this.ID = id;
        this.Name = name;
        this.PhoneNumber = phone;
        this.Date = date;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return ID.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View child, ViewGroup parent) {

        Holder holder;

        LayoutInflater layoutInflater;

        if (child == null) {
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            child = layoutInflater.inflate(R.layout.items, null);

            holder = new Holder();

            holder.ID_TextView = (TextView) child.findViewById(R.id.textViewID);
            holder.Name_TextView = (TextView) child.findViewById(R.id.textViewNAME);
            holder.PhoneNumberTextView = (TextView) child.findViewById(R.id.textViewPHONE_NUMBER);
            holder.Date_TextView = (TextView) child.findViewById(R.id.textViewDATE);

            child.setTag(holder);

        } else {

            holder = (Holder) child.getTag();
        }
        holder.ID_TextView.setText(ID.get(position));
        holder.Name_TextView.setText(Name.get(position));
        holder.PhoneNumberTextView.setText(PhoneNumber.get(position));
        holder.Date_TextView.setText(Date.get(position));

        return child;
    }

    public class Holder {

        TextView ID_TextView;
        TextView Name_TextView;
        TextView PhoneNumberTextView;
        TextView Date_TextView;

    }

}

我的SQLiteHelper类

public class SQLiteHelper extends SQLiteOpenHelper {

    public static String DATABASE_NAME="AndroidJSonDataBase";

    public static final String TABLE_NAME="AndroidJSonTable";

    public static final String Table_Column_ID="id";

    public static final String Table_Column_1_Name="name";

    public static final String Table_Column_2_PhoneNumber="phone_number"; //info table

    public static final String Table_Column_3_Date="date";

    public static final String Table_Column_4_ImageUri="image_uri";

    public SQLiteHelper(Context context) {

        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase database) {

        String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("
                +Table_Column_ID+" INTEGER PRIMARY KEY, "
                +Table_Column_1_Name+" VARCHAR, "
                +Table_Column_2_PhoneNumber +" VARCHAR, "
                +Table_Column_3_Date+" VARCHAR, "
                +Table_Column_4_ImageUri+" VARCHAR)";

        database.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }


}
java android sqlite android-sqlite
1个回答
0
投票

我认为答案是,既不使用也不适应DbAdapter。

看起来DbAdapter不是用于显示目的的适配器,而是从导入的数据库调整/转换/提取数据以适合本地使用。

ListAdapter是一个适用于显示目的的适配器,不适用于转换

简而言之,方法importIntoDb: -

  • 1调用checkDbIsValid (a)SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase(db.getPath(), null, SQLiteDatabase.OPEN_READONLY); - 将文件作为SQLite数据库打开,如果该文件不是SQLite数据库,则SQLite可以理解该异常被捕获并且该方法返回false。这是好的/适合的,因为它是通用的。 (b)Cursor cursor = sqlDb.query(true, DATABASE_TABLE, null, null, null, null, null, null, null); - 根据DATABASE_TABLE从表中提取所有列和所有行(重复项除外)的Cursor,无论是什么。这要求DATABASE_TABLE(无论何处设置)设置为导入数据库中存在的表。 (c)for( String s : DbAdapter.ALL_COLUMN_KEYS ){ cursor.getColumnIndexOrThrow(s); } - 检查Cursor中的Columns是否全部位于作为DbAdapter类的类变量的String数组中。这需要DbAdapter类。所以这需要省略或调整。 (d)返回truefalse,后者表示数据库无效。
  • 2如果数据库检查为false,则该方法返回false。
  • 3打开数据库(如果出现异常,则返回false)。
  • 4使用所有列和所有行(重复项除外)提取游标。
  • 5创建DbAdapter实例。这需要DbAdapter类。
  • 6设置两个列名称titletimestamp
  • 7使用DbAdaptercreateQuote方法利用从titletimestamp列提取的数据循环游标。因此,只有当所述列存在于源中并且存在DbAdapter类的实例时,这才有效。

....

要适应DbAdapter,您可能需要继续查看已复制的代码,并确定它是否适合您的目的。

© www.soinside.com 2019 - 2024. All rights reserved.