0

我必须存储一些数据字段和一个 pdf。我创建了两个类,第一个是 DatabaseHandler,它完成了我所有的基本数据库工作,并引用了数据库的一行,我创建了一个创建记录的类 myHelper/具有我需要的每个数据元素的行对象。我面临的问题是我需要在数据库中存储一个 pdf。我已经完成了“values.put(KEY_pdf, row.get_pdf());”它给了我一个错误“无法解析 put() 方法”,并且显而易见的原因是我将类型声明为 String。在我的应用程序中,我想要的是,当我在输入所需的 sem、ye​​ar、branch 值后在我的活动中添加一些按钮时从用户那里,我需要显示 pdf,这就是我为 pdf 创建 get() 方法的原因,但这种逻辑似乎没有奏效,或者我错过了什么。紧急帮助。此外,建议的任何改进都是非常可观的。

myHelper.java

package com.DataBaseHandler;

import java.sql.Blob;

/**
 *
 */

    public class myHelper {

        //private variables
        int _id;
        String _branch;
        int _year;
        int _sem;
        Blob _pdf;

        // Empty constructor
        public myHelper(){

        }
        // constructor
        public myHelper(int id, String _branch, int _year,int _sem,Blob _pdf){
            this._id = id;
            this._branch = _branch;
            this._year = _year;
            this._sem = _sem;
            this._pdf = _pdf;
        }
        // getting ID
        public int getID(){
            return this._id;
        }
        public void setID(int _id){
            _id =  this._id;
        }

        // getting branch
        public String get_branch(){
            return this._branch;
        }
        public void set_branch(String _branch){
            _branch = this._branch;
        }

        // setting year
        public int get_year(){
            return this._year;
        }
        public void set_year(int _year){
            _year = this._year;
        }

        // getting sem
        public int get_sem(){
            return this._sem;
        }
        public void set_sem(int _sem){
            _sem = this._sem;
        }

        // setting pdf
        public Blob get_pdf(){
          return this._pdf;
        }
        public void set_pdf(Blob _pdf){
            _pdf = this._pdf;
        }
}

DataBaseHandler.java
package com.DataBaseHandler;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 *
 */
public class DataBaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "infinity";
    private static final String TABLE_SYLLABUS = "syllabus";
    private static final String KEY_ID = "id";
    private static final String KEY_BRANCH = "branch";
    private static final String KEY_YEAR = "year";
    private static final String KEY_SEM = "sem";
    private static final String KEY_pdf = "pdf";

    public DataBaseHandler(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_SYLLABUS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_BRANCH + " TEXT," + KEY_YEAR + " NUMBER,"
                + KEY_SEM + " NUMBER," + KEY_pdf + " BLOB," + ")";
        sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        // Drop older table if existed
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_SYLLABUS);

        // Create tables again
        onCreate(sqLiteDatabase);
    }
    public void addRow(myHelper row) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_BRANCH, row.get_branch() );
        values.put(KEY_YEAR,row.get_year());
        values.put(KEY_SEM, row.get_sem());
        values.put(KEY_pdf, row.get_pdf());

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

}
4

1 回答 1

0

您必须将 转换Blob为字节数组才能使用put带有byte[]参数的函数:

Blob blob = row.get_pdf();
values.put(KEY_pdf, blob.getBytes(0, blob.length()));
于 2013-06-26T12:58:04.547 回答