我必须存储一些数据字段和一个pdf.I创建了两个类,第一个是DatabaseHandler,它完成我所有的基本数据库工作并引用数据库的一行,我创建了一个类myHelper,它创建了一个一个记录/行对象,我需要每个数据元素。我面临的问题是我需要在数据库中存储一个pdf。我已经完成了“values.put(KEY_pdf,row.get_pdf());”和它给了我一个错误“无法解决方法put()”和明显的原因我已经将类型声明为String.In我的应用程序,我想要的是当我在输入所需的sem,年后在我的活动中添加一些按钮,来自用户的分支值,我需要显示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
}
}
答案 0 :(得分:0)
您必须将Blob
转换为字节数组才能使用put
function with a byte[]
parameter:
Blob blob = row.get_pdf();
values.put(KEY_pdf, blob.getBytes(0, blob.length()));